Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a more consistent unit testing approach #143

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
479 changes: 185 additions & 294 deletions Source/Expressive.Tests/ExpressionTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Additive;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Moq;
using System;

namespace Expressive.Tests.Expressions.Binary.Additive
{
[TestClass]
[TestFixture]
public class AddExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new AddExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)1),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)2),
MockExpression.ThatEvaluatesTo(1),
MockExpression.ThatEvaluatesTo(2),
new Context(ExpressiveOptions.None));

Assert.AreEqual(3, expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestEvaluateWithDifferentSizedArrays()
{
var expression = new AddExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new[] { 1, 2, 3 }),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new[] { 1 }),
MockExpression.ThatEvaluatesTo(new[] { 1, 2, 3 }),
MockExpression.ThatEvaluatesTo(new[] { 1 }),
new Context(ExpressiveOptions.None));

Assert.IsNull(expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestEvaluateWithEmptyLeftArray()
{
#pragma warning disable CA1825 // Avoid zero-length array allocations. - Array.Empty does not exist in net 4.5
var expression = new AddExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new int[0]),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new[] { 1, 2, 3 }),
MockExpression.ThatEvaluatesTo(Array.Empty<int>()),
MockExpression.ThatEvaluatesTo(new[] { 1, 2, 3 }),
new Context(ExpressiveOptions.None));
#pragma warning restore CA1825 // Avoid zero-length array allocations.

var result = (object[])expression.Evaluate(null);
Assert.IsTrue(result.Length == 3);
Expand All @@ -50,12 +49,12 @@ public void TestEvaluateWithEmptyLeftArray()
Assert.AreEqual(result[2], null);
}

[TestMethod]
[Test]
public void TestEvaluateWithOneSidedArray()
{
var expression = new AddExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new[] { 1, 2, 3 }),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)2),
MockExpression.ThatEvaluatesTo(new[] { 1, 2, 3 }),
MockExpression.ThatEvaluatesTo(2),
new Context(ExpressiveOptions.None));

var result = (object[])expression.Evaluate(null);
Expand All @@ -65,12 +64,12 @@ public void TestEvaluateWithOneSidedArray()
Assert.AreEqual(result[2], 5);
}

[TestMethod]
[Test]
public void TestEvaluateWithSameSizedArrays()
{
var expression = new AddExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new[] { 1, 2, 3 }),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)new[] { 1, 2, 3 }),
MockExpression.ThatEvaluatesTo(new[] { 1, 2, 3 }),
MockExpression.ThatEvaluatesTo(new[] { 1, 2, 3 }),
new Context(ExpressiveOptions.None));

var result = (object[])expression.Evaluate(null);
Expand All @@ -80,12 +79,12 @@ public void TestEvaluateWithSameSizedArrays()
Assert.AreEqual(result[2], 6);
}

[TestMethod]
[Test]
public void TestStringAddition()
{
var expression = new AddExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)"1"),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)2),
MockExpression.ThatEvaluatesTo("1"),
MockExpression.ThatEvaluatesTo(2),
new Context(ExpressiveOptions.None));

Assert.AreEqual("12", expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Additive;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Expressive.Expressions.Binary.Additive;
using NUnit.Framework;

namespace Expressive.Tests.Expressions.Binary.Additive
{
[TestClass]
[TestFixture]
public class SubtractExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new SubtractExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)1),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)2),
MockExpression.ThatEvaluatesTo(1),
MockExpression.ThatEvaluatesTo(2),
new Context(ExpressiveOptions.None));

Assert.AreEqual(-1, expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Bitwise;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Expressive.Expressions.Binary.Bitwise;
using NUnit.Framework;

namespace Expressive.Tests.Expressions.Binary.Bitwise
{
[TestClass]
[TestFixture]
public class BitwiseAndExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new BitwiseAndExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0x1001),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0x0001),
MockExpression.ThatEvaluatesTo(0x1001),
MockExpression.ThatEvaluatesTo(0x0001),
new Context(ExpressiveOptions.None));

Assert.AreEqual(0x0001, expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Bitwise;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Expressive.Expressions.Binary.Bitwise;
using NUnit.Framework;

namespace Expressive.Tests.Expressions.Binary.Bitwise
{
[TestClass]
[TestFixture]
public class BitwiseExclusiveOrExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new BitwiseExclusiveOrExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)1111),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0001),
MockExpression.ThatEvaluatesTo(1111),
MockExpression.ThatEvaluatesTo(0001),
new Context(ExpressiveOptions.None));

Assert.AreEqual(1110, expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Bitwise;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Expressive.Expressions.Binary.Bitwise;
using NUnit.Framework;

namespace Expressive.Tests.Expressions.Binary.Bitwise
{
[TestClass]
[TestFixture]
public class BitwiseOrExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new BitwiseOrExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)1001),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0001),
MockExpression.ThatEvaluatesTo(1001),
MockExpression.ThatEvaluatesTo(0001),
new Context(ExpressiveOptions.None));

Assert.AreEqual(1001, expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Bitwise;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Expressive.Expressions.Binary.Bitwise;
using NUnit.Framework;

namespace Expressive.Tests.Expressions.Binary.Bitwise
{
[TestClass]
[TestFixture]
public class LeftShiftExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new LeftShiftExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0x1001),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0x0001),
MockExpression.ThatEvaluatesTo(0x1001),
MockExpression.ThatEvaluatesTo(0x0001),
new Context(ExpressiveOptions.None));

Assert.AreEqual(0x1001 << 0x0001, expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Bitwise;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Expressive.Expressions.Binary.Bitwise;
using NUnit.Framework;

namespace Expressive.Tests.Expressions.Binary.Bitwise
{
[TestClass]
[TestFixture]
public class RightShiftExpressionTests
{
[TestMethod]
[Test]
public void TestEvaluate()
{
var expression = new RightShiftExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0x1001),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)0x0001),
MockExpression.ThatEvaluatesTo(0x1001),
MockExpression.ThatEvaluatesTo(0x0001),
new Context(ExpressiveOptions.None));

Assert.AreEqual(0x1001 >> 0x0001, expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Conditional;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Moq;

namespace Expressive.Tests.Expressions.Binary.Conditional
{
[TestClass]
[TestFixture]
public class NullCoalescingExpressionTests
{
[TestMethod]
[Test]
public void TestNotNullEvaluate()
{
var expression = new NullCoalescingExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)"Non null"),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)"Never used"),
MockExpression.ThatEvaluatesTo("Non null"),
MockExpression.ThatEvaluatesTo("Never used"),
new Context(ExpressiveOptions.None));

Assert.AreEqual("Non null", expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestNullEvaluate()
{
var expression = new NullCoalescingExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)null),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)"Now used"),
MockExpression.ThatEvaluatesTo(null),
MockExpression.ThatEvaluatesTo("Now used"),
new Context(ExpressiveOptions.None));

Assert.AreEqual("Now used", expression.Evaluate(null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
using System.Collections.Generic;
using Expressive.Expressions;
using Expressive.Expressions.Binary.Logical;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Moq;

namespace Expressive.Tests.Expressions.Binary.Logical
{
[TestClass]
[TestFixture]
public class AndExpressionTests
{
[TestMethod]
[Test]
public void TestBothTrueEvaluate()
{
var expression = new AndExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) true),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) true),
MockExpression.ThatEvaluatesTo(true),
MockExpression.ThatEvaluatesTo(true),
new Context(ExpressiveOptions.None));

Assert.AreEqual(true, expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestLeftTrueEvaluate()
{
var expression = new AndExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) true),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) false),
MockExpression.ThatEvaluatesTo(true),
MockExpression.ThatEvaluatesTo(false),
new Context(ExpressiveOptions.None));

Assert.AreEqual(false, expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestNeitherTrueEvaluate()
{
var expression = new AndExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) false),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) false),
MockExpression.ThatEvaluatesTo(false),
MockExpression.ThatEvaluatesTo(false),
new Context(ExpressiveOptions.None));

Assert.AreEqual(false, expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestRightTrueEvaluate()
{
var expression = new AndExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) false),
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object) true),
MockExpression.ThatEvaluatesTo(false),
MockExpression.ThatEvaluatesTo(true),
new Context(ExpressiveOptions.None));

Assert.AreEqual(false, expression.Evaluate(null));
}

[TestMethod]
[Test]
public void TestShortCircuit()
{
var rightHandMock = new Mock<IExpression>();

var expression = new AndExpression(
Mock.Of<IExpression>(e => e.Evaluate(It.IsAny<IDictionary<string, object>>()) == (object)false),
MockExpression.ThatEvaluatesTo(false),
rightHandMock.Object,
new Context(ExpressiveOptions.None));

Expand Down
Loading
Loading