Bugfix/improve code coverage (#4)

* Improved coverage

* Finished
This commit is contained in:
0xJ1M
2023-09-11 21:48:27 +01:00
committed by GitHub
parent c6b21dcf28
commit 461a60ff77
6 changed files with 296 additions and 45 deletions

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EngineTests.Parser_Tests.Nodes
{
internal class BaseNodeTests
{
}
}

View File

@@ -0,0 +1,122 @@
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!");
}
}
}
}

View File

@@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EngineTests.Parser_Tests
{
/// <summary>
/// Class for testing the TreeNodes
/// </summary>
[TestClass]
public class NodesTests
{
/// <summary>
/// Test the Parser on a basic List of tokens
/// </summary>
[TestMethod]
public void TestParserBasicExpression()
{
}
}
}

View File

@@ -0,0 +1,140 @@
using MathEngine.Parser.Parser;
using MathEngine.Parser.Parser.Nodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EngineTests.Parser_Tests.Nodes
{
/// <summary>
/// Class for testing the TreeNodes
/// </summary>
[TestClass]
public class NumericNodeTests
{
/// <summary>
/// Test two NumericNodes with same generic type can be added
/// /// </summary>
[TestMethod]
public void TestNumericNodeAdd()
{
NumericNode<decimal> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
BaseNode result = testNode1 + testNode2;
Assert.AreEqual(result.ToString(), "200");
}
/// <summary>
/// Test two NumericNodes being added of different generic types raises exception
/// </summary>
[TestMethod]
public void TestNumericNodeAddDifferentTypesRaisesException()
{
NumericNode<int> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
try
{
BaseNode result = testNode1 + testNode2;
}
catch (InvalidOperationException ex)
{
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
}
}
/// <summary>
/// Test two NumericNodes with same generic type can be subtracted
/// /// </summary>
[TestMethod]
public void TestNumericNodeSubtract()
{
NumericNode<decimal> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
BaseNode result = testNode1 - testNode2;
Assert.AreEqual(result.ToString(), "0");
}
/// <summary>
/// Test two NumericNodes being subtracted of different generic types raises exception
/// </summary>
[TestMethod]
public void TestNumericNodeSubtractDifferentTypesRaisesException()
{
NumericNode<int> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
try
{
BaseNode result = testNode1 - testNode2;
}
catch (InvalidOperationException ex)
{
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
}
}
/// <summary>
/// Test two NumericNodes with same generic type can be multiplied
/// </summary>
[TestMethod]
public void TestNumericNodeMultiply()
{
NumericNode<decimal> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
BaseNode result = testNode1 * testNode2;
Assert.AreEqual(result.ToString(), "10000");
}
/// <summary>
/// Test two NumericNodes being multiplied of different generic types raises exception
/// </summary>
[TestMethod]
public void TestNumericNodeMultiplyDifferentTypesRaisesException()
{
NumericNode<int> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
try
{
BaseNode result = testNode1 * testNode2;
}
catch (InvalidOperationException ex)
{
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
}
}
/// <summary>
/// Test two NumericNodes with same generic type can be divided
/// /// </summary>
[TestMethod]
public void TestNumericNodeDivide()
{
NumericNode<decimal> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
BaseNode result = testNode1 / testNode2;
Assert.AreEqual(result.ToString(), "1");
}
/// <summary>
/// Test two NumericNodes being divided of different generic types raises exception
/// </summary>
[TestMethod]
public void TestNumericNodeDividedDifferentTypesRaisesException()
{
NumericNode<int> testNode1 = new(100);
NumericNode<decimal> testNode2 = new(100);
try
{
BaseNode result = testNode1 / testNode2;
}
catch (InvalidOperationException ex)
{
Assert.AreEqual(ex.Message, "Attempted Invalid operation");
}
}
}
}

View File

@@ -8,6 +8,7 @@ namespace EngineTests
[TestClass]
public class ParserTests
{
/// <summary>
/// Test the Parser on a basic List of tokens
/// </summary>