Skip to content

Instantly share code, notes, and snippets.

@afish
Created May 15, 2016
Embed
What would you like to do?
Tests for SimpleObject for YAUL compiler.
using System;
using Compiler;
using FluentAssertions;
using NUnit.Framework;
namespace CompilerTests
{
[TestFixture]
public class SimpleObjectTests
{
[Test]
public void ShouldAddIntegers()
{
var first = new SimpleObject(3);
var second = new SimpleObject(5);
var expected = new SimpleObject(8);
Assert.That(first + second, Is.EqualTo(expected));
}
[Test]
public void ShouldAddStrings()
{
var first = new SimpleObject("aa");
var second = new SimpleObject("bbb");
var expected = new SimpleObject("aabbb");
Assert.That(first + second, Is.EqualTo(expected));
}
[Test]
public void ShouldAddArrays()
{
var first = new SimpleObject(new[] { new SimpleObject(1), new SimpleObject("AAA") });
var second = new SimpleObject(new[] { new SimpleObject("wa"), new SimpleObject("23") });
var expected = new SimpleObject(new[] { new SimpleObject(1), new SimpleObject("AAA"), new SimpleObject("wa"), new SimpleObject("23") });
(first + second).Should().Be(expected);
}
[Test]
public void ShouldSubIntegers()
{
var first = new SimpleObject(3);
var second = new SimpleObject(5);
var expected = new SimpleObject(-2);
Assert.That(first - second, Is.EqualTo(expected));
}
[Test]
public void ShouldMulIntegers()
{
var first = new SimpleObject(3);
var second = new SimpleObject(5);
var expected = new SimpleObject(15);
Assert.That(first * second, Is.EqualTo(expected));
}
[Test]
public void ShouldDivIntegers()
{
var first = new SimpleObject(3);
var second = new SimpleObject(5);
var expected = new SimpleObject(0);
Assert.That(first / second, Is.EqualTo(expected));
}
[Test]
public void ShouldModIntegers()
{
var first = new SimpleObject(3);
var second = new SimpleObject(5);
var expected = new SimpleObject(3);
Assert.That(first % second, Is.EqualTo(expected));
}
[Test]
[TestCase(3, 5, true)]
[TestCase(5, 5, false)]
[TestCase(6, 5, false)]
[TestCase("aaaa", "bb", true)]
[TestCase("bcaa", "bb", false)]
[TestCase("bb", "bb", false)]
public void LessOperator(object a, object b, bool result)
{
var first = new SimpleObject(a);
var second = new SimpleObject(b);
Assert.That(first < second, Is.EqualTo(result));
}
[Test]
[TestCase(3, 5, false)]
[TestCase(5, 5, false)]
[TestCase(6, 5, true)]
[TestCase("aaaa", "bb", false)]
[TestCase("bcaa", "bb", true)]
[TestCase("bb", "bb", false)]
public void GreaterOperator(object a, object b, bool result)
{
var first = new SimpleObject(a);
var second = new SimpleObject(b);
Assert.That(first > second, Is.EqualTo(result));
}
[Test]
[TestCase(3, 5, true)]
[TestCase(5, 5, true)]
[TestCase(6, 5, false)]
[TestCase("aaaa", "bb", true)]
[TestCase("bcaa", "bb", false)]
[TestCase("bb", "bb", true)]
public void LessOrEqualOperator(object a, object b, bool result)
{
var first = new SimpleObject(a);
var second = new SimpleObject(b);
Assert.That(first <= second, Is.EqualTo(result));
}
[Test]
[TestCase(3, 5, false)]
[TestCase(5, 5, true)]
[TestCase(6, 5, true)]
[TestCase("aaaa", "bb", false)]
[TestCase("bcaa", "bb", true)]
[TestCase("bb", "bb", true)]
public void GreaterOrEqualOperator(object a, object b, bool result)
{
var first = new SimpleObject(a);
var second = new SimpleObject(b);
Assert.That(first >= second, Is.EqualTo(result));
}
[Test]
[TestCase(3, 5, false)]
[TestCase(5, 5, true)]
[TestCase(6, 5, false)]
[TestCase("aaaa", "bb", false)]
[TestCase("bcaa", "bb", false)]
[TestCase("bb", "bb", true)]
[TestCase("2", "bb", false)]
[TestCase("2", 2, false)]
public void EqualOperator(object a, object b, bool result)
{
var first = new SimpleObject(a);
var second = new SimpleObject(b);
Assert.That(first == second, Is.EqualTo(result));
}
[Test]
[TestCase(3, 5, true)]
[TestCase(5, 5, false)]
[TestCase(6, 5, true)]
[TestCase("aaaa", "bb", true)]
[TestCase("bcaa", "bb", true)]
[TestCase("bb", "bb", false)]
[TestCase("2", "bb", true)]
[TestCase("2", 2, true)]
public void NotEqualOperator(object a, object b, bool result)
{
var first = new SimpleObject(a);
var second = new SimpleObject(b);
Assert.That(first != second, Is.EqualTo(result));
}
[Test]
public void EqualOperatorForArrays()
{
var first = new SimpleObject(new[] { new SimpleObject(3) });
var second = new SimpleObject(new[] { new SimpleObject(3) });
Assert.That(first == second, Is.True);
}
[Test]
public void NotEqualOperatorForArrays()
{
var first = new SimpleObject(new[] { new SimpleObject(3) });
var second = new SimpleObject(new[] { new SimpleObject(4), new SimpleObject(6) });
Assert.That(first != second, Is.True);
}
[Test]
[TestCase(3,true)]
[TestCase(0,false)]
[TestCase("aa", true)]
[TestCase("",false)]
public void BoolOperator(object a, bool result)
{
var first = new SimpleObject(a);
Assert.That((bool)first, Is.EqualTo(result));
}
[Test]
public void BoolOperatorForNonEmptyArray()
{
var first = new SimpleObject(new []{new SimpleObject("aaa")});
Assert.That((bool)first, Is.True);
}
[Test]
public void BoolOperatorEmptyArray()
{
var first = new SimpleObject(new SimpleObject[0]);
Assert.That((bool)first, Is.False);
}
[Test]
[TestCase(3, false)]
[TestCase(0, true)]
[TestCase("aa", false)]
[TestCase("", true)]
public void NegationOperator(object a, bool result)
{
var first = new SimpleObject(a);
Assert.That(!first, Is.EqualTo(result));
}
[Test]
public void NegationOperatorForNonEmptyArray()
{
var first = new SimpleObject(new[] { new SimpleObject("aaa") });
Assert.That(!first, Is.False);
}
[Test]
public void NegationOperatorEmptyArray()
{
var first = new SimpleObject(new SimpleObject[0]);
Assert.That(!first, Is.True);
}
[Test]
public void ShouldReturnArrayElement()
{
var first = new SimpleObject(1);
var second = new SimpleObject(2);
var @object = new SimpleObject(new[]{first, second});
@object[0].Should().BeSameAs(first);
@object[1].Should().BeSameAs(second);
SimpleObject.GetElement(@object, new SimpleObject(0)).Should().BeSameAs(first);
SimpleObject.GetElement(@object, new SimpleObject(1)).Should().BeSameAs(second);
}
[Test]
public void ShouldSaveArrayElement()
{
var first = new SimpleObject(1);
var second = new SimpleObject("1");
var @object = new SimpleObject(new[] { new SimpleObject(2) });
@object[0] = first;
@object[0].Should().BeSameAs(first);
SimpleObject.SetElement(@object, new SimpleObject(0), second);
@object[0].Should().BeSameAs(second);
}
[Test, ExpectedException(typeof(InvalidOperationException))]
public void ShouldThrowWhenNotArray()
{
var @object = new SimpleObject(1);
var result = @object[0];
}
[Test]
public void ShouldCompareLengths()
{
var first = new SimpleObject(new []{new SimpleObject(1)});
var second = new SimpleObject(new[] { new SimpleObject(1), new SimpleObject(1) });
Assert.That(first < second);
Assert.That(second > first);
}
[Test]
[TestCase(1, 2)]
[TestCase("1", "2")]
public void ShouldCompareArrayElements(object firstArg, object secondArg)
{
var first = new SimpleObject(new[] { new SimpleObject(firstArg), new SimpleObject(firstArg) });
var second = new SimpleObject(new[] { new SimpleObject(firstArg), new SimpleObject(secondArg) });
Assert.That(first < second);
Assert.That(second > first);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment