Files
MathsEngine/MathEngine/EngineTests/Parser Tests/Nodes/NodeFactoryTests.cs
0xJ1M 461a60ff77 Bugfix/improve code coverage (#4)
* Improved coverage

* Finished
2023-09-11 21:48:27 +01:00

123 lines
4.5 KiB
C#

using MathEngine.Parser.Parser;
using MathEngine.Parser.Parser.Nodes;
using MathEngine.Parser.Tokeniser;
using static MathEngine.Parser.Tokeniser.Token;
namespace EngineTests.Parser_Tests.Nodes
{
/// <summary>
/// Class for testing the NodeFactory
/// </summary>
[TestClass]
public class NodeFactoryTests
{
/// <summary>
/// Test the NodeFactory test BinaryNode creation on all defined operations
/// </summary>
[TestMethod]
public void TestNodeFactoryBinaryNodesOnDefinedOperations()
{
NumericNode<decimal> node1 = new(200);
NumericNode<decimal> node2 = new(100);
Token plus = Token.Plus;
Token minus = Token.Minus;
Token multiply = Token.Multiply;
Token divide = Token.Divide;
BaseNode addNode = NodeFactory.CreateBinaryNode(plus, node1, node2);
BaseNode subtractNode = NodeFactory.CreateBinaryNode(minus, node1, node2);
BaseNode multiplyNode = NodeFactory.CreateBinaryNode(multiply, node1, node2);
BaseNode divideNode = NodeFactory.CreateBinaryNode(divide, node1, node2);
}
/// <summary>
/// Test the NodeFactory test BinaryNode creation on exponentiation raises exception
/// </summary>
[TestMethod]
public void TestNodeFactoryBinaryNodesOnExponentiationRaisesException()
{
NumericNode<decimal> node1 = new(200);
NumericNode<decimal> node2 = new(100);
Token exp = new("^", Token.Type.Exponentiation, NumericType.NaN, 0);
try
{
BaseNode expBiinary = NodeFactory.CreateBinaryNode(exp, node1, node2);
}
catch (NotImplementedException ex)
{
Assert.AreEqual(ex.Message, "Exponentiation is not implemented at this time");
}
}
/// <summary>
/// Test the NodeFactory test BinaryNode creation on invalid operation token raises exception
/// </summary>
[TestMethod]
public void TestNodeFactoryBinaryNodesOnInvalidOperationTokenRaisesException()
{
NumericNode<decimal> node1 = new(200);
NumericNode<decimal> node2 = new(100);
Token invalid = new("(", Token.Type.OpenBracket, NumericType.NaN, 0);
try
{
BaseNode expBiinary = NodeFactory.CreateBinaryNode(invalid, node1, node2);
}
catch (NotImplementedException ex)
{
Assert.AreEqual(ex.Message, "Attempted to create a BinaryNode with an invalid operation!");
}
}
/// <summary>
/// Test the NodeFactory test NumericNode creation on all defined types
/// </summary>
[TestMethod]
public void TestNodeFactoryNumericNodesOnDefinedTypes()
{
Token test_token1 = new("100", Token.Type.Numeric, Token.NumericType.Integer, 0);
Token test_token2 = new("100.5", Token.Type.Numeric, Token.NumericType.Decimal, 0);
BaseNode testNode1 = NodeFactory.CreateNumericNode(test_token1);
BaseNode testNode2 = NodeFactory.CreateNumericNode(test_token2);
}
/// <summary>
/// Test the NodeFactory test NumericNode creation on Complex type RaisesException
/// </summary>
[TestMethod]
public void TestNodeFactoryNumericNodesOnComplexTypeRaisesException()
{
try
{
Token comp = new("1+i", Token.Type.Numeric, Token.NumericType.Complex, 0);
BaseNode testNode = NodeFactory.CreateNumericNode(comp);
}
catch (NotImplementedException ex)
{
Assert.AreEqual(ex.Message, "Complex Numbers are not implemented at this time");
}
}
/// <summary>
/// Test the NodeFactory test NumericNode creation on invalid type Raises Exception
/// </summary>
[TestMethod]
public void TestNodeFactoryNumericNodesOnInvalidTypeRaisesException()
{
try
{
Token comp = new("(", Token.Type.CloseBrace, Token.NumericType.NaN, 0);
BaseNode testNode = NodeFactory.CreateNumericNode(comp);
}
catch (InvalidDataException ex)
{
Assert.AreEqual(ex.Message, "Attempted to create a NumericNode with non numeric data!");
}
}
}
}