Skip to content

Commit

Permalink
#31 Add addition, subtraction, and multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Miista committed May 2, 2024
1 parent ebd7e64 commit 6e2f3bc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Pose/Helpers/ShimHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public static MethodBase GetMethodFromExpression(Expression expression, bool set
var unaryExpression = expression as UnaryExpression ?? throw new Exception($"Cannot cast expression to {nameof(UnaryExpression)}");
instanceOrType = null;
return unaryExpression.Method;
case ExpressionType.Add:
case ExpressionType.Subtract:
case ExpressionType.Multiply:
var binaryExpression = expression as BinaryExpression ?? throw new Exception($"Cannot cast expression to {nameof(BinaryExpression)}");
instanceOrType = null;
return binaryExpression.Method;
default:
throw new NotImplementedException("Unsupported expression");
}
Expand Down
19 changes: 15 additions & 4 deletions src/Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,25 @@ public static void Main(string[] args)
var sut1 = new OverridenOperatorClass();
var operatorShim = Shim.Replace(() => (bool) sut1)
.With(delegate (OverridenOperatorClass c) { return true; });
var dateTimeAddShim = Shim.Replace(() => Is.A<DateTime>() + Is.A<TimeSpan>())
.With(delegate(DateTime dt, TimeSpan ts) { return new DateTime(2004, 01, 01); });
var dateTimeSubtractShim = Shim.Replace(() => Is.A<DateTime>() - Is.A<TimeSpan>())
.With(delegate(DateTime dt, TimeSpan ts) { return new DateTime(1990, 01, 01); });

PoseContext.Isolate(
() =>
{
var sut = new OverridenOperatorClass();
Console.WriteLine($"Result: {(bool)sut}");
Console.WriteLine($"Result: {(bool)sut1}");
}, operatorShim
var dateTime = DateTime.Now;
Console.WriteLine($"Date: {dateTime}");
var ts = TimeSpan.FromSeconds(1);
Console.WriteLine($"Time: {ts}");

var time = dateTime + ts;
Console.WriteLine($"Result1: {time}");

var time2 = dateTime - ts;
Console.WriteLine($"Result2: {time2}");
}, dateTimeAddShim, dateTimeSubtractShim
);
#elif NET6_0
Console.WriteLine("6.0");
Expand Down
76 changes: 76 additions & 0 deletions test/Pose.Tests/OperatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace Pose.Tests
{
public class OperatorTests
{
[Fact]
public void Can_shim_addition_operator()
{
// Arrange
var shimmedValue = new DateTime(2004, 01, 01);
var dateTimeAddShim = Shim.Replace(() => Is.A<DateTime>() + Is.A<TimeSpan>())
.With(delegate(DateTime dt, TimeSpan ts) { return shimmedValue; });

// Act
var result = default(DateTime);
PoseContext.Isolate(
() =>
{
var now = DateTime.Now;
var zeroSeconds = TimeSpan.Zero;

result = now + zeroSeconds;
}, dateTimeAddShim);

// Assert
result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return");
}

[Fact]
public void Can_shim_subtraction_operator()
{
// Arrange
var shimmedValue = new DateTime(2004, 01, 01);
var dateTimeAddShim = Shim.Replace(() => Is.A<DateTime>() - Is.A<TimeSpan>())
.With(delegate(DateTime dt, TimeSpan ts) { return shimmedValue; });

// Act
var result = default(DateTime);
PoseContext.Isolate(
() =>
{
var now = DateTime.Now;
var zeroSeconds = TimeSpan.Zero;

result = now - zeroSeconds;
}, dateTimeAddShim);

// Assert
result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return");
}

[Fact]
public void Can_shim_multiplication_operator()
{
// Arrange
var shimmedValue = int.MaxValue;
var dateTimeAddShim = Shim.Replace(() => Is.A<int>() * Is.A<int>())
.With(delegate(int l, int r) { return shimmedValue; });

// Act
var result = default(int);
PoseContext.Isolate(
() =>
{
result = 1 * 1;
}, dateTimeAddShim);

// Assert
result.Should().Be(shimmedValue, because: "that is the value the shim is configured to return");
}
}
}

0 comments on commit 6e2f3bc

Please sign in to comment.