public void ExactTypeFails() { expectedMessage = " Expected: <System.Int32>" + Environment.NewLine + " But was: <System.String>" + Environment.NewLine; EnsuranceHelper.Expect("Hello", Is.TypeOf(typeof(Int32))); }
public void StartsWithTests() { string phrase = "Hello World!"; string[] greetings = new string[] { "Hello!", "Hi!", "Hola!" }; // Classic syntax EnsureBase <Ensure> .Strings.StartsWith("Hello", phrase); // Helper syntax Ensure.That(phrase, Text.StartsWith("Hello")); // Only available using new syntax Ensure.That(phrase, Text.DoesNotStartWith("Hi!")); Ensure.That(phrase, Text.StartsWith("HeLLo").IgnoreCase); Ensure.That(phrase, Text.DoesNotStartWith("HI").IgnoreCase); Ensure.That(greetings, Text.All.StartsWith("h").IgnoreCase); // Inherited syntax EnsuranceHelper.Expect(phrase, StartsWith("Hello")); // Only available using new syntax EnsuranceHelper.Expect(phrase, Not.StartsWith("Hi!")); EnsuranceHelper.Expect(phrase, StartsWith("HeLLo").IgnoreCase); EnsuranceHelper.Expect(phrase, Not.StartsWith("HI").IgnoreCase); EnsuranceHelper.Expect(greetings, All.StartsWith("h").IgnoreCase); }
public void EqualityTests() { int[] i3 = new int[] { 1, 2, 3 }; double[] d3 = new double[] { 1.0, 2.0, 3.0 }; int[] iunequal = new int[] { 1, 3, 2 }; // Classic Syntax Ensure.AreEqual(4, 2 + 2); Ensure.AreEqual(i3, d3); Ensure.AreNotEqual(5, 2 + 2); Ensure.AreNotEqual(i3, iunequal); // Helper syntax Ensure.That(2 + 2, Is.EqualTo(4)); Ensure.That(2 + 2 == 4); Ensure.That(i3, Is.EqualTo(d3)); Ensure.That(2 + 2, Is.Not.EqualTo(5)); Ensure.That(i3, Is.Not.EqualTo(iunequal)); // Inherited syntax EnsuranceHelper.Expect(2 + 2, EqualTo(4)); EnsuranceHelper.Expect(2 + 2 == 4); EnsuranceHelper.Expect(i3, EqualTo(d3)); EnsuranceHelper.Expect(2 + 2, Not.EqualTo(5)); EnsuranceHelper.Expect(i3, Not.EqualTo(iunequal)); }
public void EqualIgnoringCaseTests() { string phrase = "Hello World!"; // Classic syntax EnsureBase <Ensure> .Strings.AreEqualIgnoringCase("hello world!", phrase); // Helper syntax Ensure.That(phrase, Is.EqualTo("hello world!").IgnoreCase); //Only available using new syntax Ensure.That(phrase, Is.Not.EqualTo("goodbye world!").IgnoreCase); Ensure.That(new string[] { "Hello", "World" }, Is.EqualTo(new object[] { "HELLO", "WORLD" }).IgnoreCase); Ensure.That(new string[] { "HELLO", "Hello", "hello" }, Is.All.EqualTo("hello").IgnoreCase); // Inherited syntax EnsuranceHelper.Expect(phrase, EqualTo("hello world!").IgnoreCase); //Only available using new syntax EnsuranceHelper.Expect(phrase, Not.EqualTo("goodbye world!").IgnoreCase); EnsuranceHelper.Expect(new string[] { "Hello", "World" }, EqualTo(new object[] { "HELLO", "WORLD" }).IgnoreCase); EnsuranceHelper.Expect(new string[] { "HELLO", "Hello", "hello" }, All.EqualTo("hello").IgnoreCase); }
public void AndOperator() { // The & operator is only available in the new syntax Ensure.That(7, Is.GreaterThan(5) & Is.LessThan(10)); // Inherited syntax EnsuranceHelper.Expect(7, GreaterThan(5) & LessThan(10)); }
public void CollectionEquivalenceTests() { int[] ints1to5 = new int[] { 1, 2, 3, 4, 5 }; int[] twothrees = new int[] { 1, 2, 3, 3, 4, 5 }; int[] twofours = new int[] { 1, 2, 3, 4, 4, 5 }; // Classic syntax Ensure.AreEquivalent(new int[] { 2, 1, 4, 3, 5 }, ints1to5); Ensure.AreNotEquivalent(new int[] { 2, 2, 4, 3, 5 }, ints1to5); Ensure.AreNotEquivalent(new int[] { 2, 4, 3, 5 }, ints1to5); Ensure.AreNotEquivalent(new int[] { 2, 2, 1, 1, 4, 3, 5 }, ints1to5); Ensure.AreNotEquivalent(twothrees, twofours); // Helper syntax Ensure.That(new int[] { 2, 1, 4, 3, 5 }, Is.EquivalentTo(ints1to5)); Ensure.That(new int[] { 2, 2, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5)); Ensure.That(new int[] { 2, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5)); Ensure.That(new int[] { 2, 2, 1, 1, 4, 3, 5 }, Is.Not.EquivalentTo(ints1to5)); // Inherited syntax EnsuranceHelper.Expect(new int[] { 2, 1, 4, 3, 5 }, EquivalentTo(ints1to5)); EnsuranceHelper.Expect(new int[] { 2, 2, 4, 3, 5 }, Not.EquivalentTo(ints1to5)); EnsuranceHelper.Expect(new int[] { 2, 4, 3, 5 }, Not.EquivalentTo(ints1to5)); EnsuranceHelper.Expect(new int[] { 2, 2, 1, 1, 4, 3, 5 }, Not.EquivalentTo(ints1to5)); }
public void RegularExpressionTests() { string phrase = "Now is the time for all good men to come to the aid of their country."; string[] quotes = new string[] { "Never say never", "It's never too late", "Nevermore!" }; // Classic syntax EnsureBase <Ensure> .Strings.IsMatch("all good men", phrase); EnsureBase <Ensure> .Strings.IsMatch("Now.*come", phrase); // Helper syntax Ensure.That(phrase, Text.Matches("all good men")); Ensure.That(phrase, Text.Matches("Now.*come")); // Only available using new syntax Ensure.That(phrase, Text.DoesNotMatch("all.*men.*good")); Ensure.That(phrase, Text.Matches("ALL").IgnoreCase); Ensure.That(quotes, Text.All.Matches("never").IgnoreCase); // Inherited syntax EnsuranceHelper.Expect(phrase, Matches("all good men")); EnsuranceHelper.Expect(phrase, Matches("Now.*come")); // Only available using new syntax EnsuranceHelper.Expect(phrase, Not.Matches("all.*men.*good")); EnsuranceHelper.Expect(phrase, Matches("ALL").IgnoreCase); EnsuranceHelper.Expect(quotes, All.Matches("never").IgnoreCase); }
public void SubstringTests() { string phrase = "Hello World!"; string[] array = new string[] { "abc", "bad", "dba" }; // Classic Syntax EnsureBase <Ensure> .Strings.Contains("World", phrase); // Helper syntax Ensure.That(phrase, Text.Contains("World")); // Only available using new syntax Ensure.That(phrase, Text.DoesNotContain("goodbye")); Ensure.That(phrase, Text.Contains("WORLD").IgnoreCase); Ensure.That(phrase, Text.DoesNotContain("BYE").IgnoreCase); Ensure.That(array, Text.All.Contains("b")); // Inherited syntax EnsuranceHelper.Expect(phrase, Contains("World")); // Only available using new syntax EnsuranceHelper.Expect(phrase, Not.Contains("goodbye")); EnsuranceHelper.Expect(phrase, Contains("WORLD").IgnoreCase); EnsuranceHelper.Expect(phrase, Not.Contains("BYE").IgnoreCase); EnsuranceHelper.Expect(array, All.Contains("b")); }
public void NotOperator() { // The ! operator is only available in the new syntax Ensure.That(42, !Is.Null); // Inherited syntax EnsuranceHelper.Expect(42, !Null); }
public void EqualityTestsWithTolerance() { // CLassic syntax Ensure.AreEqual(5.0d, 4.99d, 0.05d); Ensure.AreEqual(5.0f, 4.99f, 0.05f); // Helper syntax Ensure.That(4.99d, Is.EqualTo(5.0d).Within(0.05d)); Ensure.That(4.0d, Is.Not.EqualTo(5.0d).Within(0.5d)); Ensure.That(4.99f, Is.EqualTo(5.0f).Within(0.05f)); Ensure.That(4.99m, Is.EqualTo(5.0m).Within(0.05m)); Ensure.That(3999999999u, Is.EqualTo(4000000000u).Within(5u)); Ensure.That(499, Is.EqualTo(500).Within(5)); Ensure.That(4999999999L, Is.EqualTo(5000000000L).Within(5L)); Ensure.That(5999999999ul, Is.EqualTo(6000000000ul).Within(5ul)); // Inherited syntax EnsuranceHelper.Expect(4.99d, EqualTo(5.0d).Within(0.05d)); EnsuranceHelper.Expect(4.0d, Not.EqualTo(5.0d).Within(0.5d)); EnsuranceHelper.Expect(4.99f, EqualTo(5.0f).Within(0.05f)); EnsuranceHelper.Expect(4.99m, EqualTo(5.0m).Within(0.05m)); EnsuranceHelper.Expect(499u, EqualTo(500u).Within(5u)); EnsuranceHelper.Expect(499, EqualTo(500).Within(5)); EnsuranceHelper.Expect(4999999999L, EqualTo(5000000000L).Within(5L)); EnsuranceHelper.Expect(5999999999ul, EqualTo(6000000000ul).Within(5ul)); }
public void IsInstanceOfTypeFails() { expectedMessage = " Expected: instance of <System.Int32>" + Environment.NewLine + " But was: <System.String>" + Environment.NewLine; EnsuranceHelper.Expect("abc123", Is.InstanceOfType(typeof(Int32))); }
public void FloatIsWrittenToNineDigits() { WriteValue(0.33333333333333f); int digits = writer.ToString().Length - 3; // 0.dddddddddf EnsuranceHelper.Expect(digits, Is.EqualTo(9)); EnsuranceHelper.Expect(writer.ToString().Length, Is.EqualTo(12)); }
public void ArraysDeclaredAsDifferentTypes() { string[] array1 = { "one", "two", "three" }; object[] array2 = { "one", "three", "two" }; AreNotEqual(array1, array2); EnsuranceHelper.Expect(array1, Is.Not.EqualTo(array2)); EnsuranceHelper.Expect(array2, Is.Not.EqualTo(array1)); }
public void SameLengthDifferentContent() { string[] array1 = { "one", "two", "three" }; string[] array2 = { "one", "two", "ten" }; AreNotEqual(array1, array2); AreNotEqual(array2, array1); EnsuranceHelper.Expect(array1, Is.Not.EqualTo(array2)); EnsuranceHelper.Expect(array2, Is.Not.EqualTo(array1)); }
public void DifferentLengthArrays() { string[] array1 = { "one", "two", "three" }; string[] array2 = { "one", "two", "three", "four", "five" }; AreNotEqual(array1, array2); AreNotEqual(array2, array1); EnsuranceHelper.Expect(array1, Is.Not.EqualTo(array2)); EnsuranceHelper.Expect(array2, Is.Not.EqualTo(array1)); }
public void IsNotAssignableFrom() { int[] array10 = new int[10]; int[,] array2 = new int[2, 2]; Ensure.IsNotAssignableFrom(array2.GetType(), array10); Ensure.IsNotAssignableFrom(array2.GetType(), array10, "Type Failure Message"); Ensure.IsNotAssignableFrom(array2.GetType(), array10, "Type Failure Message", null); EnsuranceHelper.Expect(array10, Is.Not.AssignableFrom(array2.GetType())); }
public void IsNotAssignableFromFails() { int[] array10 = new int[10]; int[] array2 = new int[2]; expectedMessage = " Expected: not Type assignable from <System.Int32[]>" + Environment.NewLine + " But was: <System.Int32[]>" + Environment.NewLine; EnsuranceHelper.Expect(array10, Is.Not.AssignableFrom(array2.GetType())); }
public void ArraysHaveDifferentRanks() { int[] expected = new int[] { 1, 2, 3, 4 }; int[,] actual = new int[, ] { { 1, 2 }, { 3, 4 } }; expectedMessage = " Expected is <System.Int32[4]>, actual is <System.Int32[2,2]>" + Environment.NewLine; EnsuranceHelper.Expect(actual, Is.EqualTo(expected)); }
public void ActualArrayIsLonger() { int[] expected = new int[] { 1, 2, 3 }; int[] actual = new int[] { 1, 2, 3, 4, 5, 6, 7 }; expectedMessage = " Expected is <System.Int32[3]>, actual is <System.Int32[7]>" + Environment.NewLine + " Values differ at index [3]" + Environment.NewLine + " Extra: < 4, 5, 6... >"; EnsuranceHelper.Expect(actual, Is.EqualTo(expected)); }
public void IsFalse() { // Classic syntax Ensure.IsFalse(2 + 2 == 5); // Helper syntax Ensure.That(2 + 2 == 5, Is.False); // Inherited syntax EnsuranceHelper.Expect(2 + 2 == 5, False); }
public void IsNotNull() { // Classic syntax Ensure.IsNotNull(42); // Helper syntax Ensure.That(42, Is.Not.Null); // Inherited syntax EnsuranceHelper.Expect(42, Not.Null); }
public void JaggedArrayComparedToSimpleArray() { int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[][] actual = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 0, 7 }, new int[] { 8, 9 } }; expectedMessage = " Expected is <System.Int32[9]>, actual is <System.Int32[3][]>" + Environment.NewLine + " Values differ at index [0]" + Environment.NewLine + TextMessageWriter.Pfx_Expected + "1" + Environment.NewLine + TextMessageWriter.Pfx_Actual + "< 1, 2, 3 >" + Environment.NewLine; EnsuranceHelper.Expect(actual, Is.EqualTo(expected)); }
public void FailureOnSingleDimensionedArrays() { int[] expected = new int[] { 1, 2, 3 }; int[] actual = new int[] { 1, 5, 3 }; expectedMessage = " Expected and actual are both <System.Int32[3]>" + Environment.NewLine + " Values differ at index [1]" + Environment.NewLine + TextMessageWriter.Pfx_Expected + "2" + Environment.NewLine + TextMessageWriter.Pfx_Actual + "5" + Environment.NewLine; EnsuranceHelper.Expect(actual, Is.EqualTo(expected)); }
public void IsTrue() { // Classic syntax Ensure.IsTrue(2 + 2 == 4); // Helper syntax Ensure.That(2 + 2 == 4, Is.True); Ensure.That(2 + 2 == 4); // Inherited syntax EnsuranceHelper.Expect(2 + 2 == 4, True); EnsuranceHelper.Expect(2 + 2 == 4); }
public void IsNull() { object actual = null; // Classic syntax Ensure.IsNull(actual); // Helper syntax Ensure.That(actual, Is.Null); // Inherited syntax EnsuranceHelper.Expect(actual, Null); }
public void InstanceOfTypeTests() { // Classic syntax Ensure.IsInstanceOfType(typeof(string), "Hello"); Ensure.IsNotInstanceOfType(typeof(string), 5); // Helper syntax Ensure.That("Hello", Is.InstanceOfType(typeof(string))); Ensure.That(5, Is.Not.InstanceOfType(typeof(string))); // Inherited syntax EnsuranceHelper.Expect("Hello", InstanceOfType(typeof(string))); EnsuranceHelper.Expect(5, Not.InstanceOfType(typeof(string))); }
public void ArraysDeclaredAsDifferentTypes() { string[] array1 = { "one", "two", "three" }; object[] array2 = { "one", "three", "two" }; expectedMessage = " Expected is <System.Object[3]>, actual is <System.String[3]>" + Environment.NewLine + " Values differ at index [1]" + Environment.NewLine + " Expected string length 5 but was 3. Strings differ at index 1." + Environment.NewLine + " Expected: \"three\"" + Environment.NewLine + " But was: \"two\"" + Environment.NewLine + " ------------^" + Environment.NewLine; EnsuranceHelper.Expect(array1, Is.EqualTo(array2)); }
public void EmptyStringTests() { // Classic syntax Ensure.IsEmpty(""); Ensure.IsNotEmpty("Hello!"); // Helper syntax Ensure.That("", Is.Empty); Ensure.That("Hello!", Is.Not.Empty); // Inherited syntax EnsuranceHelper.Expect("", Empty); EnsuranceHelper.Expect("Hello!", Not.Empty); }
public void EmptyCollectionTests() { // Classic syntax Ensure.IsEmpty(new bool[0]); Ensure.IsNotEmpty(new int[] { 1, 2, 3 }); // Helper syntax Ensure.That(new bool[0], Is.Empty); Ensure.That(new int[] { 1, 2, 3 }, Is.Not.Empty); // Inherited syntax EnsuranceHelper.Expect(new bool[0], Empty); EnsuranceHelper.Expect(new int[] { 1, 2, 3 }, Not.Empty); }
public void AssignableFromTypeTests() { // Classic syntax Ensure.IsAssignableFrom(typeof(string), "Hello"); Ensure.IsNotAssignableFrom(typeof(string), 5); // Helper syntax Ensure.That("Hello", Is.AssignableFrom(typeof(string))); Ensure.That(5, Is.Not.AssignableFrom(typeof(string))); // Inherited syntax EnsuranceHelper.Expect("Hello", AssignableFrom(typeof(string))); EnsuranceHelper.Expect(5, Not.AssignableFrom(typeof(string))); }