diff --git a/test/Pose.Tests/OperatorTests.cs b/test/Pose.Tests/OperatorTests.cs index c2eb3d1..8a50268 100644 --- a/test/Pose.Tests/OperatorTests.cs +++ b/test/Pose.Tests/OperatorTests.cs @@ -35,7 +35,7 @@ internal class OperatorsClass public static bool operator true(OperatorsClass l) => false; public static bool operator false(OperatorsClass r) => true; public static explicit operator int(OperatorsClass c) => int.MinValue; - public static implicit operator double(OperatorsClass c) => double.MinValue; + public static implicit operator double(OperatorsClass c) => 42.0; } public class Arithmetic @@ -48,66 +48,57 @@ public void Can_shim_addition_operator() var shim = Shim.Replace(() => Is.A() + Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left + right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left + right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left + right, because: "the implementation has been shimmed"); } [Fact] - public void Can_shim_addition_operator_for_DateTime() + public void Can_shim_addition_operator_for_TimeSpan() { // Arrange var shimmedValue = TimeSpan.FromSeconds(2); var shim = Shim.Replace(() => Is.A() + Is.A()) - .With(delegate(TimeSpan dt, TimeSpan ts) { return shimmedValue; }); + .With(delegate(TimeSpan l, TimeSpan r) { return shimmedValue; }); - // Act + var now = TimeSpan.Zero; + var zeroSeconds = TimeSpan.Zero; var result = default(TimeSpan); - PoseContext.Isolate( - () => - { - var now = TimeSpan.Zero; - var zeroSeconds = TimeSpan.Zero; - - result = now + zeroSeconds; - }, shim); + + // Act + PoseContext.Isolate(() => result = now + zeroSeconds, shim); // Assert result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(now + zeroSeconds, because: "the implementation has been shimmed"); } [Fact] - public void Can_shim_subtraction_operator_for_DateTime() + public void Can_shim_subtraction_operator_for_TimeSpan() { // Arrange var shimmedValue = TimeSpan.FromDays(2); var shim = Shim.Replace(() => Is.A() - Is.A()) .With(delegate(TimeSpan dt, TimeSpan ts) { return shimmedValue; }); - // Act + var now = TimeSpan.Zero; + var zeroSeconds = TimeSpan.Zero; var result = default(TimeSpan); - PoseContext.Isolate( - () => - { - var now = TimeSpan.Zero; - var zeroSeconds = TimeSpan.Zero; - - result = now - zeroSeconds; - }, shim); + + // Act + PoseContext.Isolate(() => result = now - zeroSeconds, shim); // Assert result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(now - zeroSeconds, because: "the implementation has been shimmed"); } [Fact] @@ -118,20 +109,17 @@ public void Can_shim_subtraction_operator() var shim = Shim.Replace(() => Is.A() - Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left - right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left - right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left - right, because: "the implementation has been shimmed"); } [Fact] @@ -142,20 +130,17 @@ public void Can_shim_multiplication_operator() var shim = Shim.Replace(() => Is.A() * Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left * right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left * right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left * right, because: "the implementation has been shimmed"); } [Fact] @@ -166,20 +151,17 @@ public void Can_shim_division_operator() var shim = Shim.Replace(() => Is.A() / Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left / right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left / right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left / right, because: "the implementation has been shimmed"); } [Fact] @@ -190,20 +172,17 @@ public void Can_shim_modulus_operator() var shim = Shim.Replace(() => Is.A() % Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left % right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left % right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left % right, because: "the implementation has been shimmed"); } [Fact] @@ -214,19 +193,16 @@ public void Can_shim_bitwise_complement_operator() var shim = Shim.Replace(() => ~Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); - // Act + var sut = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); - - result = ~sut; - }, shim); + + // Act + PoseContext.Isolate(() => result = ~sut, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(~sut, because: "the implementation has been shimmed"); } [Fact(Skip = "How to get the operator method from expression?")] @@ -237,15 +213,30 @@ public void Can_shim_true_operator() var shim = Shim.Replace(() => Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); - // Act + var sut = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); + + // Act + PoseContext.Isolate(() => result = sut ? shimmedValue : null, shim); + + // Assert + result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); + result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + } + + [Fact(Skip = "How to get the operator method from expression?")] + public void Can_shim_false_operator() + { + // Arrange + var shimmedValue = new OperatorsClass { Value = "Hello, World" }; + var shim = Shim.Replace(() => Is.A()) + .With(delegate(OperatorsClass l) { return shimmedValue; }); - result = sut ? shimmedValue : null; - }, shim); + var sut = new OperatorsClass(); + var result = default(OperatorsClass); + + // Act + PoseContext.Isolate(() => result = sut ? null : shimmedValue, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); @@ -260,19 +251,16 @@ public void Can_shim_unary_plus_operator() var shim = Shim.Replace(() => +Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); - // Act + var sut = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); - - result = +sut; - }, shim); + + // Act + PoseContext.Isolate(() => result = +sut, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(+sut, because: "the implementation has been shimmed"); } [Fact] @@ -283,19 +271,16 @@ public void Can_shim_unary_minus_operator() var shim = Shim.Replace(() => -Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); - // Act + var sut = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); - - result = -sut; - }, shim); + + // Act + PoseContext.Isolate(() => result = -sut, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(-sut, because: "the implementation has been shimmed"); } } @@ -309,20 +294,17 @@ public void Can_shim_left_shift_operator() var shim = Shim.Replace(() => Is.A() << Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left << right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left << right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left << right, because: "the implementation has been shimmed"); } [Fact] @@ -333,20 +315,17 @@ public void Can_shim_right_shift_operator() var shim = Shim.Replace(() => Is.A() >> Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left >> right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left >> right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left >> right, because: "the implementation has been shimmed"); } } @@ -374,6 +353,11 @@ public void Can_shim_equal_operator() // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + + // Verify actual implementation + var left = new OperatorsClass(); + var right = new OperatorsClass(); + (left == right).Should().BeNull(because: "that is the actual implementation"); } [Fact] @@ -398,6 +382,11 @@ public void Can_shim_not_equal_operator() // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + + // Verify actual implementation + var left = new OperatorsClass(); + var right = new OperatorsClass(); + (left != right).Should().BeNull(because: "that is the actual implementation"); } [Fact] @@ -422,6 +411,11 @@ public void Can_shim_less_than_operator() // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + + // Verify actual implementation + var left = new OperatorsClass(); + var right = new OperatorsClass(); + (left < right).Should().BeNull(because: "that is the actual implementation"); } [Fact] @@ -446,6 +440,11 @@ public void Can_shim_greater_than_operator() // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + + // Verify actual implementation + var left = new OperatorsClass(); + var right = new OperatorsClass(); + (left > right).Should().BeNull(because: "that is the actual implementation"); } [Fact] @@ -470,6 +469,11 @@ public void Can_shim_less_than_or_equal_to_operator() // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + + // Verify actual implementation + var left = new OperatorsClass(); + var right = new OperatorsClass(); + (left >= right).Should().BeNull(because: "that is the actual implementation"); } [Fact] @@ -494,8 +498,12 @@ public void Can_shim_greater_than_or_equal_to_operator() // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + + // Verify actual implementation + var left = new OperatorsClass(); + var right = new OperatorsClass(); + (left <= right).Should().BeNull(because: "that is the actual implementation"); } - } public class Conversion @@ -508,18 +516,15 @@ public void Can_shim_explicit_cast_operator() var shim = Shim.Replace(() => (int) Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); - // Act + var sut = new OperatorsClass(); var result = int.MinValue; - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); - - result = (int) sut; // Explicit cast here - }, shim); + + // Act + PoseContext.Isolate(() => result = (int) sut, shim); // Assert result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe((int)sut, because: "the implementation has been shimmed"); } [Fact] @@ -531,18 +536,15 @@ public void Can_shim_implicit_cast_operator() var shim = Shim.Replace(() => (double) Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); + var sut = new OperatorsClass(); + var result = 42.0; + // Act - var result = double.MinValue; - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); + PoseContext.Isolate(() => result = sut, shim); - result = sut; // Implicit cast here - }, shim); - // Assert result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe((double)sut, because: "the implementation has been shimmed"); } } @@ -556,19 +558,16 @@ public void Can_shim_logical_negation_operator() var shim = Shim.Replace(() => ~Is.A()) .With(delegate(OperatorsClass l) { return shimmedValue; }); - // Act + var sut = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var sut = new OperatorsClass(); - - result = ~sut; - }, shim); + + // Act + PoseContext.Isolate(() => result = ~sut, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(~sut, because: "the implementation has been shimmed"); } [Fact] @@ -579,20 +578,17 @@ public void Can_shim_logical_AND_operator() var shim = Shim.Replace(() => Is.A() & Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left & right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left & right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left & result, because: "the implementation has been shimmed"); } [Fact] @@ -603,20 +599,17 @@ public void Can_shim_logical_exclusive_OR_operator() var shim = Shim.Replace(() => Is.A() ^ Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left ^ right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left ^ right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left ^ right, because: "the implementation has been shimmed"); } [Fact] @@ -627,20 +620,17 @@ public void Can_shim_logical_OR_operator() var shim = Shim.Replace(() => Is.A() | Is.A()) .With(delegate(OperatorsClass l, OperatorsClass r) { return shimmedValue; }); - // Act + var left = new OperatorsClass(); + var right = new OperatorsClass(); var result = default(OperatorsClass); - PoseContext.Isolate( - () => - { - var left = new OperatorsClass(); - var right = new OperatorsClass(); - - result = left | right; - }, shim); + + // Act + PoseContext.Isolate(() => result = left | right, shim); // Assert result.Should().NotBeNull(because: "the shim is configured to return a non-null value"); result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return"); + result.Should().NotBe(left | right, because: "the implementation has been shimmed"); } } }