PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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