public void ThenItShouldCorrectlyParseComparizonStatements() { foreach (var comparisons in new[] { new { FormulaText = "|1|<|2|", ExpectedResult = (object)(1 < 2) }, new { FormulaText = "|8|-|3|<=|9|-|5|", ExpectedResult = (object)(8 - 3 <= 9 - 5) }, new { FormulaText = "|-|6.6666|>|3.0|", ExpectedResult = (object)(-6.6666M > 3.0M) }, new { FormulaText = "|6.6666|>=|3|", ExpectedResult = (object)(6.6666M >= 3) }, new { FormulaText = "|10|-|3.3333|<|25|*|-5|", ExpectedResult = (object)(10 - 3.3333M < 25 * -5) }, new { FormulaText = "|2|+|3|+|6|>=|10|-|12|", ExpectedResult = (object)(2 + 3 + 6 >= 10 - 12) }, new { FormulaText = "|2|+|3|*|6|<|10|/|2|", ExpectedResult = (object)(2 + 3 * 6 < 10 / 2) }, new { FormulaText = "|2|*|3|>|60|/|12|*|3|-|100|", ExpectedResult = (object)(2 * 3 > 60 / 12 * 3 - 100) }, new { FormulaText = "|f|(|)|>=|3|", ExpectedResult = (object)(StockFunctions.f() >= 3) }, new { FormulaText = "|5.456|<|foo|(|1|)|+|3|", ExpectedResult = (object)(5.456M < StockFunctions.foo(1) + 3) } }) { foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(comparisons.FormulaText)) { var formula = _testee.Build(formulaTextWithSpaces); Assert.AreEqual( comparisons.ExpectedResult, formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })), $"Formula text: {formulaTextWithSpaces}"); } } }
public void ThenItShouldCorrectlyParseMultiplyingStatements() { foreach (var invocation in new[] { new { FormulaText = "|2|*|3|", ExpectedResult = (object)(2 * 3) }, new { FormulaText = "|8|/|3|", ExpectedResult = (object)(8 / 3) }, new { FormulaText = "|6.6666|*|3.0|", ExpectedResult = (object)(6.6666M * 3.0M) }, new { FormulaText = "|6.6666|*|3|", ExpectedResult = (object)(6.6666M * 3) }, new { FormulaText = "|10|/|3.3333|", ExpectedResult = (object)(10 / 3.3333M) }, new { FormulaText = "|2|*|3|*|6|*|10|/|12|", ExpectedResult = (object)(2 * 3 * 6 * 10 / 12) }, new { FormulaText = "|f|(|)|*|3|", ExpectedResult = (object)(StockFunctions.f() * 3) }, new { FormulaText = "|5.456|/|foo|(|1|)|*|3|", ExpectedResult = (object)(5.456M / StockFunctions.foo(1) * 3) } }) { foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(invocation.FormulaText)) { var formula = _testee.Build(formulaTextWithSpaces); Assert.AreEqual( invocation.ExpectedResult, formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })), $"Formula text: {formulaTextWithSpaces}"); } } }
public void ThenItShouldCorrectlyParseAddingStatements() { foreach (var invocation in new[] { new { FormulaText = "|2|+|3|", ExpectedResult = (object)(2 + 3) }, new { FormulaText = "|8|-|3|", ExpectedResult = (object)(8 - 3) }, new { FormulaText = "|-|6.6666|+|3.0|", ExpectedResult = (object)(-6.6666M + 3.0M) }, new { FormulaText = "|6.6666|+|3|", ExpectedResult = (object)(6.6666M + 3) }, new { FormulaText = "|10|-|3.3333|", ExpectedResult = (object)(10 - 3.3333M) }, new { FormulaText = "|2|+|3|+|6|+|10|-|12|", ExpectedResult = (object)(2 + 3 + 6 + 10 - 12) }, new { FormulaText = "|2|+|3|*|6|+|10|/|2|", ExpectedResult = (object)(2 + 3 * 6 + 10 / 2) }, new { FormulaText = "|2|*|3|+|60|/|12|*|3|-|100|", ExpectedResult = (object)(2 * 3 + 60 / 12 * 3 - 100) }, new { FormulaText = "|f|(|)|+|3|", ExpectedResult = (object)(StockFunctions.f() + 3) }, new { FormulaText = "|5.456|-|foo|(|1|)|+|3|", ExpectedResult = (object)(5.456M - StockFunctions.foo(1) + 3) } }) { foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(invocation.FormulaText)) { var formula = _testee.Build(formulaTextWithSpaces); Assert.AreEqual( invocation.ExpectedResult, formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })), $"Formula text: {formulaTextWithSpaces}"); } } }
public void ThenItShouldCorrectlyParseStatementsWithNegation() { foreach (var formula in new[] { new { Text = "|-|1|", ExpectedResult = (object)-1 }, new { Text = "|-|[I]|", ExpectedResult = (object)-DefaultPropertyHolder.I }, new { Text = "|-|f(|)|", ExpectedResult = (object)-StockFunctions.f() }, new { Text = "|-|10|-|5| + 6", ExpectedResult = (object)(-10 - 5 + 6) }, new { Text = "|-|(|10|+|f|(|)|)|/|3|", ExpectedResult = (object)(-(10 + StockFunctions.f()) / 3) }, new { Text = "|If|(|-|foo|(|1|)|<|0|,|f|(|)|,|-|f|(|)|)|", ExpectedResult = (object)(-StockFunctions.foo(1) < 0 ? StockFunctions.f() : -StockFunctions.f()) } }) { foreach (var formulaText in InsertSpacesIntoFormula(formula.Text)) { var builtFormula = _testee.Build(formulaText); Assert.AreEqual( formula.ExpectedResult, builtFormula.CalculateCellValue(DefaultPropertyHolder, builtFormula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })), $"Formula text: {formulaText}, Expression built: {builtFormula}"); } } }
public void ThenItShouldCorrectlyParseFunctionCalls() { foreach (var functionCall in new[] { // exact parameter types match new { FormulaText = "|f|(|)|", ExpectedResult = (object)StockFunctions.f() }, new { FormulaText = "|foo|(|1|)|", ExpectedResult = (object)StockFunctions.foo(1) }, new { FormulaText = "|bar|(|'qwerty'|,|[Book]|)|", ExpectedResult = (object)StockFunctions.bar("qwerty", DefaultPropertyHolder.Book) }, new { FormulaText = "|MakeFoo|(|bar|(|'asdf'|,|[Book]|)|,|foo|(|100|)|,|'2018-12-10 10:47:03Z'|)|", ExpectedResult = (object)StockFunctions.MakeFoo(StockFunctions.bar("asdf", DefaultPropertyHolder.Book), StockFunctions.foo(100), DateTime.Parse("2018-12-10 10:47:03Z")) }, // implicit conversions new { FormulaText = "|foo|(|1.567|)|", ExpectedResult = (object)StockFunctions.foo((int)1.567M) }, new { FormulaText = "|MakeFoo|(|bar|(|'asdf'|,|[Book]|)|,|f|(|)|,|'2018-12-10 10:47:03Z'|)|", ExpectedResult = (object)StockFunctions.MakeFoo(StockFunctions.bar("asdf", DefaultPropertyHolder.Book), StockFunctions.f(), DateTime.Parse("2018-12-10 10:47:03Z")) }, }) { foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(functionCall.FormulaText)) { var formula = _testee.Build(formulaTextWithSpaces); Assert.AreEqual( functionCall.ExpectedResult, formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })), $"Formula text: [{formulaTextWithSpaces}]"); } } }