1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
29 #if (!SILVERLIGHT && !PORTABLE) || TIZEN
33 namespace NUnit.Framework.Constraints
36 /// FileOrDirectoryExistsConstraint is used to determine if a file or directory exists
38 public class FileOrDirectoryExistsConstraint : Constraint
40 private bool _ignoreDirectories;
41 private bool _ignoreFiles;
44 /// If true, the constraint will only check if files exist, not directories
46 public FileOrDirectoryExistsConstraint IgnoreDirectories
50 _ignoreDirectories = true;
56 /// If true, the constraint will only check if directories exist, not files
58 public FileOrDirectoryExistsConstraint IgnoreFiles
68 /// Initializes a new instance of the <see cref="FileOrDirectoryExistsConstraint"/> class that
69 /// will check files and directories.
71 public FileOrDirectoryExistsConstraint(){}
74 /// Initializes a new instance of the <see cref="FileOrDirectoryExistsConstraint"/> class that
75 /// will only check files if ignoreDirectories is true.
77 /// <param name="ignoreDirectories">if set to <c>true</c> [ignore directories].</param>
78 public FileOrDirectoryExistsConstraint(bool ignoreDirectories)
80 _ignoreDirectories = ignoreDirectories;
83 #region Overrides of Constraint
86 /// The Description of what this constraint tests, for
87 /// use in messages and in the ConstraintResult.
89 public override string Description
93 if (_ignoreDirectories)
99 return "directory exists";
101 return "file or directory exists";
106 /// Applies the constraint to an actual value, returning a ConstraintResult.
108 /// <param name="actual">The value to be tested</param>
109 /// <returns>A ConstraintResult</returns>
110 public override ConstraintResult ApplyTo<TActual>(TActual actual)
113 throw new ArgumentNullException("actual", "The actual value must be a non-null string" + ErrorSubstring);
117 return CheckString(actual);
120 var fileInfo = actual as FileInfo;
121 if (!_ignoreFiles && fileInfo != null)
123 return new ConstraintResult(this, actual, fileInfo.Exists);
126 var directoryInfo = actual as DirectoryInfo;
127 if (!_ignoreDirectories && directoryInfo != null)
129 return new ConstraintResult(this, actual, directoryInfo.Exists);
131 throw new ArgumentException("The actual value must be a string" + ErrorSubstring, "actual");
134 private ConstraintResult CheckString<TActual>(TActual actual)
136 var str = actual as string;
137 if (String.IsNullOrEmpty(str))
138 throw new ArgumentException("The actual value cannot be an empty string", "actual");
140 var fileInfo = new FileInfo(str);
141 if (_ignoreDirectories && !_ignoreFiles)
143 return new ConstraintResult(this, actual, fileInfo.Exists);
145 var directoryInfo = new DirectoryInfo(str);
146 if (_ignoreFiles && !_ignoreDirectories)
148 return new ConstraintResult(this, actual, directoryInfo.Exists);
150 return new ConstraintResult(this, actual, fileInfo.Exists || directoryInfo.Exists);
153 private string ErrorSubstring
157 if (_ignoreDirectories)
159 return " or FileInfo";
163 return " or DirectoryInfo";
165 return ", FileInfo or DirectoryInfo";