/src/NUnit/framework/Constraints/BasicConstraints.cs
C# | 118 lines | 52 code | 13 blank | 53 comment | 11 complexity | 346a13d101de5a176ba298bc462cad95 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// **************************************************************** 6using System; 7 8namespace NUnit.Framework.Constraints 9{ 10 /// <summary> 11 /// BasicConstraint is the abstract base for constraints that 12 /// perform a simple comparison to a constant value. 13 /// </summary> 14 public abstract class BasicConstraint : Constraint 15 { 16 private object expected; 17 private string description; 18 19 /// <summary> 20 /// Initializes a new instance of the <see cref="T:BasicConstraint"/> class. 21 /// </summary> 22 /// <param name="expected">The expected.</param> 23 /// <param name="description">The description.</param> 24 public BasicConstraint(object expected, string description) 25 { 26 this.expected = expected; 27 this.description = description; 28 } 29 30 /// <summary> 31 /// Test whether the constraint is satisfied by a given value 32 /// </summary> 33 /// <param name="actual">The value to be tested</param> 34 /// <returns>True for success, false for failure</returns> 35 public override bool Matches(object actual) 36 { 37 this.actual = actual; 38 39 if (actual == null && expected == null) 40 return true; 41 42 if (actual == null || expected == null) 43 return false; 44 45 return expected.Equals(actual); 46 } 47 48 /// <summary> 49 /// Write the constraint description to a MessageWriter 50 /// </summary> 51 /// <param name="writer">The writer on which the description is displayed</param> 52 public override void WriteDescriptionTo(MessageWriter writer) 53 { 54 writer.Write(description); 55 } 56 } 57 58 /// <summary> 59 /// NullConstraint tests that the actual value is null 60 /// </summary> 61 public class NullConstraint : BasicConstraint 62 { 63 /// <summary> 64 /// Initializes a new instance of the <see cref="T:NullConstraint"/> class. 65 /// </summary> 66 public NullConstraint() : base(null, "null") { } 67 } 68 69 /// <summary> 70 /// TrueConstraint tests that the actual value is true 71 /// </summary> 72 public class TrueConstraint : BasicConstraint 73 { 74 /// <summary> 75 /// Initializes a new instance of the <see cref="T:TrueConstraint"/> class. 76 /// </summary> 77 public TrueConstraint() : base(true, "True") { } 78 } 79 80 /// <summary> 81 /// FalseConstraint tests that the actual value is false 82 /// </summary> 83 public class FalseConstraint : BasicConstraint 84 { 85 /// <summary> 86 /// Initializes a new instance of the <see cref="T:FalseConstraint"/> class. 87 /// </summary> 88 public FalseConstraint() : base(false, "False") { } 89 } 90 91 /// <summary> 92 /// NaNConstraint tests that the actual value is a double or float NaN 93 /// </summary> 94 public class NaNConstraint : Constraint 95 { 96 /// <summary> 97 /// Test that the actual value is an NaN 98 /// </summary> 99 /// <param name="actual"></param> 100 /// <returns></returns> 101 public override bool Matches(object actual) 102 { 103 this.actual = actual; 104 105 return actual is double && double.IsNaN((double)actual) 106 || actual is float && float.IsNaN((float)actual); 107 } 108 109 /// <summary> 110 /// Write the constraint description to a specified writer 111 /// </summary> 112 /// <param name="writer"></param> 113 public override void WriteDescriptionTo(MessageWriter writer) 114 { 115 writer.Write("NaN"); 116 } 117 } 118}