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