Skip to content

Commit

Permalink
refactor(CalcResultTests): Add tests on arithmetic operations and use…
Browse files Browse the repository at this point in the history
… operators

Add simple integration test
  • Loading branch information
Vovanda committed Jul 28, 2020
1 parent 1bbc749 commit 4162caf
Showing 1 changed file with 179 additions and 4 deletions.
183 changes: 179 additions & 4 deletions csharp/Platform.Numbers.Tests/CalcResultTests.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,193 @@
using Xunit;
using System.Collections.Generic;
using Xunit;

namespace Platform.Numbers.Tests
{
public static class CalcResultTests
{
[Fact]
public static void UsingOperatorsTests()
public static void SimpleIntegration_Test()
{
//arrange
int expected = (5 + 6) * (3 + 9 / 3);
expected++;
expected++;
expected--;

//act
var result = ((CalcResult<int>)5 + 6) * (3 + 9 / 3);
var result = ((CalcResult<int>)5 + 6) * (3 + 9 / (CalcResult<int>)3);
result.Increment();
result++;
result--;

//assert
Assert.Equal(expected, result.Value);
}

[Theory]
[MemberData(nameof(Data))]
public static void ArithmeticOperation_Test(ArithmeticOpetation op, int a, int b)
{
//arrange
int expected = a;
var result = new CalcResult<int>(a);

//act
if (op == ArithmeticOpetation.Add)
{
result.Add(b);
expected += b;
}

if (op == ArithmeticOpetation.Subtract)
{
result.Subtract(b);
expected -= b;
}

if (op == ArithmeticOpetation.Multiply)
{
result.Multiply(b);
expected *= b;
}

if (op == ArithmeticOpetation.Divide)
{
result.Divide(b);
expected /= b;
}

//assert
int expected = (5 + 6) * (3 + 9 / 3);
Assert.Equal(expected, result.Value);
}

[Theory]
[MemberData(nameof(Data))]
public static void ArithmeticOperation_Test2(ArithmeticOpetation op, int a, int b)
{
//arrange
int expected = a;
var result = new CalcResult<int>(a);
var _b = new CalcResult<int>(b);
//act
if (op == ArithmeticOpetation.Add)
{
result.Add(_b);
expected += b;
}

if (op == ArithmeticOpetation.Subtract)
{
result.Subtract(_b);
expected -= b;
}

if (op == ArithmeticOpetation.Multiply)
{
result.Multiply(_b);
expected *= b;
}

if (op == ArithmeticOpetation.Divide)
{
result.Divide(_b);
expected /= b;
}

//assert
Assert.Equal(expected, result.Value);
}
public static IEnumerable<object[]> Data()
{
yield return new object[] {ArithmeticOpetation.Add, 40, 2};

yield return new object[] { ArithmeticOpetation.Subtract, 44, 2};

yield return new object[] { ArithmeticOpetation.Multiply, 21, 2 };

yield return new object[] { ArithmeticOpetation.Divide, 84, 2 };
}

[Theory]
[MemberData(nameof(Data))]
public static void Operators_Test(ArithmeticOpetation op, int a, int b)
{
//arrange
int expected = a;
var result = new CalcResult<int>(a);
//act
if (op == ArithmeticOpetation.Add)
{
result += b;
expected += b;
}

if (op == ArithmeticOpetation.Subtract)
{
result -= b;
expected -= b;
}

if (op == ArithmeticOpetation.Multiply)
{
result *= b;
expected *= b;
}

if (op == ArithmeticOpetation.Divide)
{
result /= b;
expected /= b;
}

//assert
Assert.Equal(expected, result.Value);
}

[Theory]
[MemberData(nameof(Data))]
public static void Operators_Test2(ArithmeticOpetation op, int a, int b)
{
//arrange
int expected = a;
var result = new CalcResult<int>(a);
var _b = new CalcResult<int>(b);

//act
if (op == ArithmeticOpetation.Add)
{
result += _b;
expected += b;
}

if (op == ArithmeticOpetation.Subtract)
{
result -= _b;
expected -= b;
}

if (op == ArithmeticOpetation.Multiply)
{
result *= _b;
expected *= b;
}

if (op == ArithmeticOpetation.Divide)
{
result /= _b;
expected /= b;
}

//assert
Assert.Equal(expected, result.Value);
}

public enum ArithmeticOpetation
{
Add,
Subtract,
Multiply,
Divide
}
}
}

0 comments on commit 4162caf

Please sign in to comment.