[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / FileOrDirectoryExistsConstraint.cs
1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
3 // 
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 #if (!SILVERLIGHT && !PORTABLE) || TIZEN
30 using System;
31 using System.IO;
32
33 namespace NUnit.Framework.Constraints
34 {
35     /// <summary>
36     /// FileOrDirectoryExistsConstraint is used to determine if a file or directory exists
37     /// </summary>
38     public class FileOrDirectoryExistsConstraint : Constraint
39     {
40         private bool _ignoreDirectories;
41         private bool _ignoreFiles;
42
43         /// <summary>
44         /// If true, the constraint will only check if files exist, not directories
45         /// </summary>
46         public FileOrDirectoryExistsConstraint IgnoreDirectories
47         {
48             get
49             {
50                 _ignoreDirectories = true;
51                 return this;
52             }
53         }
54
55         /// <summary>
56         /// If true, the constraint will only check if directories exist, not files
57         /// </summary>
58         public FileOrDirectoryExistsConstraint IgnoreFiles
59         {
60             get
61             {
62                 _ignoreFiles = true;
63                 return this;
64             }
65         }
66
67         /// <summary>
68         /// Initializes a new instance of the <see cref="FileOrDirectoryExistsConstraint"/> class that
69         /// will check files and directories.
70         /// </summary>
71         public FileOrDirectoryExistsConstraint(){}
72
73         /// <summary>
74         /// Initializes a new instance of the <see cref="FileOrDirectoryExistsConstraint"/> class that
75         /// will only check files if ignoreDirectories is true.
76         /// </summary>
77         /// <param name="ignoreDirectories">if set to <c>true</c> [ignore directories].</param>
78         public FileOrDirectoryExistsConstraint(bool ignoreDirectories)
79         {
80             _ignoreDirectories = ignoreDirectories;
81         }
82
83 #region Overrides of Constraint
84
85         /// <summary>
86         /// The Description of what this constraint tests, for
87         /// use in messages and in the ConstraintResult.
88         /// </summary>
89         public override string Description
90         {
91             get
92             {
93                 if (_ignoreDirectories)
94                 {
95                     return "file exists";
96                 }
97                 if (_ignoreFiles)
98                 {
99                     return "directory exists";
100                 }
101                 return "file or directory exists"; 
102             }
103         }
104
105         /// <summary>
106         /// Applies the constraint to an actual value, returning a ConstraintResult.
107         /// </summary>
108         /// <param name="actual">The value to be tested</param>
109         /// <returns>A ConstraintResult</returns>
110         public override ConstraintResult ApplyTo<TActual>(TActual actual)
111         {
112             if(actual == null)
113                 throw new ArgumentNullException("actual", "The actual value must be a non-null string" + ErrorSubstring);
114
115             if(actual is string)
116             {
117                 return CheckString(actual);
118             }
119
120             var fileInfo = actual as FileInfo;
121             if (!_ignoreFiles && fileInfo != null)
122             {
123                 return new ConstraintResult(this, actual, fileInfo.Exists);
124             }
125
126             var directoryInfo = actual as DirectoryInfo;
127             if (!_ignoreDirectories && directoryInfo != null)
128             {
129                 return new ConstraintResult(this, actual, directoryInfo.Exists);
130             }
131             throw new ArgumentException("The actual value must be a string" + ErrorSubstring, "actual");
132         }
133
134         private ConstraintResult CheckString<TActual>(TActual actual)
135         {
136             var str = actual as string;
137             if (String.IsNullOrEmpty(str))
138                 throw new ArgumentException("The actual value cannot be an empty string", "actual");
139
140             var fileInfo = new FileInfo(str);
141             if (_ignoreDirectories && !_ignoreFiles)
142             {
143                 return new ConstraintResult(this, actual, fileInfo.Exists);
144             }
145             var directoryInfo = new DirectoryInfo(str);
146             if (_ignoreFiles && !_ignoreDirectories)
147             {
148                 return new ConstraintResult(this, actual, directoryInfo.Exists);
149             }
150             return new ConstraintResult(this, actual, fileInfo.Exists || directoryInfo.Exists);
151         }
152
153         private string ErrorSubstring
154         {
155             get
156             {
157                 if (_ignoreDirectories)
158                 {
159                     return " or FileInfo";
160                 }
161                 if (_ignoreFiles)
162                 {
163                     return " or DirectoryInfo";
164                 }
165                 return ", FileInfo or DirectoryInfo";
166             }
167         }
168
169 #endregion
170          
171     }
172 }
173 #endif