Files
MathsEngine/MathEngine/EngineTests/Parser Tests/Tokeniser/TokenIserTests.cs
2023-09-08 17:56:24 +01:00

119 lines
3.8 KiB
C#

using MathEngine.Parser.Tokeniser;
namespace EngineTests
{
/// <summary>
/// Class for testing the Tokeniser
/// </summary>
[TestClass]
public class TokeniserTests
{
/// <summary>
/// Test the tokeniser on an empty string
/// </summary>
[TestMethod]
public void TestTokeniseEmptystringReturnsEmptyList()
{
//Arrange
string testString = "";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
one
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.AreEqual(returnedValue.Count, 0);
}
/// <summary>
/// Test the tokeniser on a basic string
/// </summary>
[TestMethod]
public void TestTokeniseBasicString()
{
//Arrange
string testString = "1+1";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
one
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.IsTrue(expectedValue.SequenceEqual(returnedValue));
}
/// <summary>
/// Test the tokeniser on a basic string, but with significant ammounts of whitespace
/// </summary>
[TestMethod]
public void TestTokeniseBasicStringWithWhiteSpace()
{
//Arrange
string testString = " 1 + 1 ";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
one
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.IsTrue(expectedValue.SequenceEqual(returnedValue));
}
/// <summary>
/// Test the tokeniser on a string which contains a number which is not formatted correctly
/// </summary>
[TestMethod]
public void TestTokeniseStringWithInvalidNumbr()
{
//Arrange
string testString = "1+11.2.5";
//Act and Assert
Assert.ThrowsException<Exception>(() => Tokeniser.Tokenise(testString));
}
/// <summary>
/// Test the tokeniser with all operators
/// </summary>
[TestMethod]
public void TestTokeniseStringWithAllOperators()
{
//Arrange
string testString = "1+2-3*4/5";
Token one = new("1", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token two = new("2", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token three = new("3", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token four = new("4", Token.Type.Numeric, Token.NumericType.Decimal, 0);
Token five = new("5", Token.Type.Numeric, Token.NumericType.Decimal, 0);
List<Token> expectedValue = new()
{
one,
Token.Plus,
two,
Token.Minus,
three,
Token.Multiply,
four,
Token.Divide,
five
};
//Act
List<Token> returnedValue = Tokeniser.Tokenise(testString);
//Assert
Assert.IsTrue(expectedValue.SequenceEqual(returnedValue));
}
}
}