public void ReusableConstraintsTest() { ReusableConstraint myConstraintNotNull = Is.Not.Null; ReusableConstraint myConstraintNotEmpty = Is.Not.Empty; ReusableConstraint myConstraint = myConstraintNotNull.Resolve() & myConstraintNotEmpty.Resolve(); Assert.That("Not a null and empty", myConstraint); }
void Run(string testSuiteLabel, string testCaseName, IATestCase <T, F> testCase, ReusableConstraint expectedConstraint) { TestTools.Log(testCaseName + ":", writeLine: false); var testCaseLabel = $"{testSuiteLabel}, testCase={testCaseName}"; var resolvedConstraint = expectedConstraint.Resolve(); if (resolvedConstraint is SkipTestConstraint) { TestTools.Log(resolvedConstraint); return; } var instance = field.IsStatic ? default : CloneInstancePrototype <T>(instanceType); var origValue = GetOrigValue(field); var expectedExceptionType = TestTools.ThrowsConstraintExceptionType(resolvedConstraint); ConstraintResult constraintResult; if (expectedExceptionType is null || expectedExceptionType == typeof(IncompatibleFieldTypeException)) { constraintResult = TestTools.AssertThat(() => { Assert.AreNotEqual(origValue, testValue, "{0}: expected !Equals(origValue, testValue) (indicates static field didn't get reset properly)", testCaseLabel); var value = testCase.Get(ref instance); // The ?.ToString() is a trick to ensure that value is fully evaluated from the ref value. _ = value?.ToString(); Assert.AreEqual(TryConvert(origValue), value, "{0}: expected Equals(origValue, value)", testCaseLabel); testCase.Set(ref instance, testValue); var newValue = field.GetValue(instance); Assert.AreEqual(testValue, TryConvert(newValue), "{0}: expected Equals(testValue, field.GetValue(instance))", testCaseLabel); TestTools.Log($"{field.Name}: {origValue} => {testCase.Get(ref instance)}"); testCase.Set(ref instance, value); // reset field value if (field.FieldType.IsInstanceOfType(newValue) is false) { throw new IncompatibleFieldTypeException($"expected field.GetValue(instance) is {field.FieldType.Name} " + "(runtime sometimes allows setting fields to values of incompatible types without any above checks failing/throwing)"); } }, expectedConstraint, testCaseLabel); }