|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using System.Reflection; |
| 4 | +using NUnit.Framework; |
| 5 | +using Rubberduck.UnitTesting; |
| 6 | + |
| 7 | +namespace RubberduckTests.UnitTesting |
| 8 | +{ |
| 9 | + [TestFixture] |
| 10 | + [NonParallelizable] |
| 11 | + [Category("PermissiveAsserts")] |
| 12 | + public class PermissiveAssertTests |
| 13 | + { |
| 14 | + private AssertCompletedEventArgs _args; |
| 15 | + |
| 16 | + [SetUp] |
| 17 | + public void Initialize() |
| 18 | + { |
| 19 | + AssertHandler.OnAssertCompleted += AssertHandler_OnAssertCompleted; |
| 20 | + } |
| 21 | + |
| 22 | + private void AssertHandler_OnAssertCompleted(object sender, AssertCompletedEventArgs e) |
| 23 | + { |
| 24 | + _args = e; |
| 25 | + } |
| 26 | + |
| 27 | + [TearDown] |
| 28 | + public void Cleanup() |
| 29 | + { |
| 30 | + _args = null; |
| 31 | + AssertHandler.OnAssertCompleted -= AssertHandler_OnAssertCompleted; |
| 32 | + } |
| 33 | + |
| 34 | + // Note: nulls are basically equivalent to an empty variant. Therefore, |
| 35 | + // comparisons to a initialized variable should be as if it was the |
| 36 | + // default value of the given type (e.g. 0 for numeric data types, empty |
| 37 | + // string for string data type and so on). To compare a value against a |
| 38 | + // VBA's Null, we must use DBNull instead. |
| 39 | + |
| 40 | + [Test] |
| 41 | + [TestCase(true, -1)] |
| 42 | + [TestCase(true, "-1")] |
| 43 | + [TestCase(true, "true")] |
| 44 | + [TestCase(0, "0E1")] |
| 45 | + [TestCase(0e1, "0")] |
| 46 | + [TestCase("", null)] |
| 47 | + [TestCase(0, null)] |
| 48 | + [TestCase(null, 0)] |
| 49 | + [TestCase(null, "")] |
| 50 | + [TestCase("123", 123)] |
| 51 | + [TestCase(456, "456")] |
| 52 | + [TestCase("abc", "abc")] |
| 53 | + [TestCase("abc", "ABC")] |
| 54 | + public void PermissiveAreEqual(object left, object right) |
| 55 | + { |
| 56 | + var assert = new PermissiveAssertClass(); |
| 57 | + assert.AreEqual(left, right); |
| 58 | + |
| 59 | + Assert.AreEqual(TestOutcome.Succeeded, _args.Outcome); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + [TestCase(true, 1)] |
| 64 | + [TestCase(true, 0)] |
| 65 | + [TestCase(true, "0")] |
| 66 | + [TestCase(true, "false")] |
| 67 | + [TestCase(123, "abc")] |
| 68 | + [TestCase("abc","def")] |
| 69 | + [TestCase("ABC", "def")] |
| 70 | + public void PermissiveAreNotEqual(object left, object right) |
| 71 | + { |
| 72 | + var assert = new PermissiveAssertClass(); |
| 73 | + assert.AreNotEqual(left, right); |
| 74 | + |
| 75 | + Assert.AreEqual(TestOutcome.Succeeded, _args.Outcome); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void PermissiveAreEqualStrings() |
| 80 | + { |
| 81 | + PermissiveAreEqual(string.Empty, ""); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + [TestCase("", false)] |
| 86 | + [TestCase("", true)] |
| 87 | + [TestCase(0, false)] |
| 88 | + [TestCase(0, true)] |
| 89 | + public void PermissiveAreNotEqualToNull(object x, bool invert) |
| 90 | + { |
| 91 | + if (invert) |
| 92 | + { |
| 93 | + PermissiveAreNotEqual(DBNull.Value, x); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + PermissiveAreNotEqual(x, DBNull.Value); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + [TestCase("", false)] |
| 103 | + [TestCase("", true)] |
| 104 | + [TestCase(0, false)] |
| 105 | + [TestCase(0, true)] |
| 106 | + public void PermissiveAreNotEqualToMissing(object x, bool invert) |
| 107 | + { |
| 108 | + if (invert) |
| 109 | + { |
| 110 | + PermissiveAreNotEqual(Missing.Value, x); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + PermissiveAreNotEqual(x, Missing.Value); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + [TestCase("0", "0", true)] |
| 120 | + [TestCase("1", "1", true)] |
| 121 | + [TestCase("1", "0", false)] |
| 122 | + [TestCase("1.1", "1.1", true)] |
| 123 | + [TestCase("1.1", "2.2", false)] |
| 124 | + public void PermissiveCompareDecimal(string x, string y, bool shouldEqual) |
| 125 | + { |
| 126 | + var dx = decimal.Parse(x, CultureInfo.InvariantCulture); |
| 127 | + var dy = decimal.Parse(y, CultureInfo.InvariantCulture); |
| 128 | + |
| 129 | + if (shouldEqual) |
| 130 | + { |
| 131 | + PermissiveAreEqual(dx, dy); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + PermissiveAreNotEqual(dx, dy); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments