PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/src/NUnit/framework/Constraints/DirectoryConstraints.cs

#
C# | 68 lines | 36 code | 8 blank | 24 comment | 7 complexity | 83798c389bd255a9ad97bf791d11e25c MD5 | raw file
 1// ****************************************************************
 2// Copyright 2008, Charlie Poole
 3// This is free software licensed under the NUnit license. You may
 4// obtain a copy of the license at http://nunit.org
 5// ****************************************************************
 6
 7using System;
 8using System.Collections;
 9using System.IO;
10
11namespace NUnit.Framework.Constraints
12{
13    /// <summary>
14    /// EmptyDirectoryConstraint is used to test that a directory is empty
15    /// </summary>
16    public class EmptyDirectoryContraint : Constraint
17    {
18        private int files = 0;
19        private int subdirs = 0;
20
21        /// <summary>
22        /// Test whether the constraint is satisfied by a given value
23        /// </summary>
24        /// <param name="actual">The value to be tested</param>
25        /// <returns>True for success, false for failure</returns>
26        public override bool Matches(object actual)
27        {
28            this.actual = actual;
29
30            DirectoryInfo dirInfo = actual as DirectoryInfo;
31            if (dirInfo == null)
32                throw new ArgumentException("The actual value must be a DirectoryInfo", "actual");
33
34            files = dirInfo.GetFiles().Length;
35            subdirs = dirInfo.GetDirectories().Length;
36
37            return files == 0 && subdirs == 0;
38        }
39
40        /// <summary>
41        /// Write the constraint description to a MessageWriter
42        /// </summary>
43        /// <param name="writer">The writer on which the description is displayed</param>
44        public override void WriteDescriptionTo(MessageWriter writer)
45        {
46            writer.Write( "An empty directory" );
47        }
48
49        /// <summary>
50        /// Write the actual value for a failing constraint test to a
51        /// MessageWriter. The default implementation simply writes
52        /// the raw value of actual, leaving it to the writer to
53        /// perform any formatting.
54        /// </summary>
55        /// <param name="writer">The writer on which the actual value is displayed</param>
56        public override void WriteActualValueTo(MessageWriter writer)
57        {
58            DirectoryInfo dir = actual as DirectoryInfo;
59            if (dir == null)
60                base.WriteActualValueTo(writer);
61            else
62            {
63                writer.WriteActualValue(dir);
64                writer.Write(" with {0} files and {1} directories", files, subdirs);
65            }
66        }
67    }
68}