using MathEngine.Parser.Parser; using MathEngine.Parser.Parser.Nodes; using MathEngine.Parser.Tokeniser; using static MathEngine.Parser.Tokeniser.Token; namespace EngineTests.Parser_Tests.Nodes { /// /// Class for testing the NodeFactory /// [TestClass] public class NodeFactoryTests { /// /// Test the NodeFactory test BinaryNode creation on all defined operations /// [TestMethod] public void TestNodeFactoryBinaryNodesOnDefinedOperations() { NumericNode node1 = new(200); NumericNode 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); } /// /// Test the NodeFactory test BinaryNode creation on exponentiation raises exception /// [TestMethod] public void TestNodeFactoryBinaryNodesOnExponentiationRaisesException() { NumericNode node1 = new(200); NumericNode 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"); } } /// /// Test the NodeFactory test BinaryNode creation on invalid operation token raises exception /// [TestMethod] public void TestNodeFactoryBinaryNodesOnInvalidOperationTokenRaisesException() { NumericNode node1 = new(200); NumericNode 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!"); } } /// /// Test the NodeFactory test NumericNode creation on all defined types /// [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); } /// /// Test the NodeFactory test NumericNode creation on Complex type RaisesException /// [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"); } } /// /// Test the NodeFactory test NumericNode creation on invalid type Raises Exception /// [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!"); } } } }