mirror of
https://github.com/0xJ1M/MathsEngine.git
synced 2026-06-05 04:00:07 +00:00
115 lines
3.9 KiB
C#
115 lines
3.9 KiB
C#
using Xunit;
|
|
using MathEngine.Tokenizer;
|
|
|
|
using static MathEngine.Tokenizer.Token;
|
|
using Xunit.Sdk;
|
|
using System;
|
|
|
|
namespace EngineTests.Tokenizer
|
|
{
|
|
/// <summary>
|
|
/// Class for testing the Tokenizer
|
|
/// </summary>
|
|
public class TokenizerTests
|
|
{
|
|
/// <summary>
|
|
/// Test the Tokenizer on an empty string
|
|
/// </summary>
|
|
[Fact]
|
|
public void TestTokenizeEmptystringReturnsEmptyList()
|
|
{
|
|
string testString = "";
|
|
Token one = new("1", TokenType.Numeric);
|
|
List<Token> expectedValue = new()
|
|
{
|
|
one,
|
|
Token.Plus,
|
|
one
|
|
};
|
|
List<Token> returnedValue = ExpressionTokenizer.Tokenize(testString);
|
|
|
|
Assert.Empty(returnedValue);
|
|
}
|
|
|
|
|
|
public static IEnumerable<object[]> TestTokenizeStringsCases
|
|
{
|
|
get
|
|
{
|
|
Token one = new("1", TokenType.Numeric);
|
|
Token decimal_token = new("123.456", TokenType.Numeric);
|
|
Token point_five = new(".5", TokenType.Numeric);
|
|
List<Token> basic_expression = new List<Token> { one, Token.Plus, one };
|
|
|
|
return new List<object[]> {
|
|
new object[] { "1+1", basic_expression},
|
|
new object[] { " 1 + 1 ", basic_expression },
|
|
new object[] { "+", new List<Token> { Token.Plus} },
|
|
new object[] { "-", new List<Token> { Token.Minus} },
|
|
new object[] { "*", new List<Token> { Token.Multiply} },
|
|
new object[] { "/", new List<Token> { Token.Divide} },
|
|
new object[] { "^", new List<Token> { Token.Exponentiation} },
|
|
new object[] { "(", new List<Token> { Token.LeftParenthesis} },
|
|
new object[] { ")", new List<Token> { Token.RightParenthesis} },
|
|
new object[] { "123.456", new List<Token> { decimal_token } },
|
|
new object[] { ".5", new List<Token> { point_five } },
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the Tokenizer on strings
|
|
/// </summary>
|
|
[Theory]
|
|
[MemberData(nameof(TestTokenizeStringsCases))]
|
|
public void TestTokenizeStrings(string testString, object expectedResult)
|
|
{
|
|
List<Token> returnedValue = ExpressionTokenizer.Tokenize(testString.AsSpan());
|
|
|
|
Assert.Equal(expectedResult, returnedValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the Tokenizer on a basic string, but with significant ammounts of whitespace
|
|
/// </summary>
|
|
[Fact]
|
|
public void TestTokenizeNumberWithMultipleDecimalPointsIsInvalid()
|
|
{
|
|
string test_expression = "123.456.789";
|
|
|
|
var ex = Assert.Throws<TokenizerException>(() => ExpressionTokenizer.Tokenize(test_expression.AsSpan()));
|
|
|
|
Assert.Equal("Syntax error: The number 123.456.789 has multiple decimal point when at most one is allowed.", ex.Message);
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Test the Tokenizer with all operators
|
|
/// </summary>
|
|
[Fact]
|
|
public void TestTokenizeStringWithAllOperators()
|
|
{
|
|
//Arrange
|
|
string testString = "1+2-3*4/5";
|
|
Token one = new("1", TokenType.Numeric);
|
|
Token two = new("2", TokenType.Numeric);
|
|
Token three = new("3", TokenType.Numeric);
|
|
Token four = new("4", TokenType.Numeric);
|
|
Token five = new("5", TokenType.Numeric);
|
|
List<Token> expectedValue = new()
|
|
{
|
|
one,
|
|
Token.Plus,
|
|
two,
|
|
Token.Minus,
|
|
three,
|
|
Token.Multiply,
|
|
four,
|
|
Token.Divide,
|
|
five
|
|
};
|
|
List<Token> returnedValue = ExpressionTokenizer.Tokenize(testString);
|
|
}
|
|
}
|
|
} |