From bbe7ed491da657835805c64ee57890d5d6f211d2 Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Sat, 21 Oct 2023 17:42:57 +0200 Subject: [PATCH] Bulk up the test suite for FormulaManager. Introduces a couple of tests for the FormulaManager test suite in order to cover all the methods the class exposes. This means it is no longer necessary to suppress the "unused" warnings. --- .../MobArena/formula/FormulaManagerIT.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/test/java/com/garbagemule/MobArena/formula/FormulaManagerIT.java b/src/test/java/com/garbagemule/MobArena/formula/FormulaManagerIT.java index a2c3d779..069c9f71 100644 --- a/src/test/java/com/garbagemule/MobArena/formula/FormulaManagerIT.java +++ b/src/test/java/com/garbagemule/MobArena/formula/FormulaManagerIT.java @@ -129,6 +129,32 @@ public void test() { } + /** + * With custom constants, we are manipulating the internal + * state of the manager, so we need to use a local subject. + */ + public static class CustomConstants { + + FormulaManager subject; + + @Before + public void setup() { + subject = FormulaManager.createDefault(); + } + + @Test + public void resolveRegisteredCustomConstant() { + subject.registerConstant("pie", 3.14); + + Formula formula = subject.parse("pie * 2"); + double result = formula.evaluate(arena); + + double expected = 6.28; + assertThat(result, equalTo(expected)); + } + + } + @RunWith(Parameterized.class) public static class DefaultVariables { @@ -262,6 +288,54 @@ public void test() { } + /** + * With custom operators, we are manipulating the internal + * state of the manager, so we need to use a local subject. + */ + public static class CustomOperators { + + FormulaManager subject; + + @Before + public void setup() { + subject = FormulaManager.createDefault(); + } + + @Test + public void resolveRegisteredCustomUnaryOperator() { + subject.registerUnaryOperator("_", 1, a -> a - 1); + + Formula formula = subject.parse("_(1 + 1)"); + double result = formula.evaluate(arena); + + double expected = 1; + assertThat(result, equalTo(expected)); + } + + @Test + public void resolveRegisteredCustomBinaryOperator1() { + subject.registerBinaryOperator("?", 1, true, (a, b) -> (a >= 0) ? a : b); + + Formula formula = subject.parse("5 ? 6"); + double result = formula.evaluate(arena); + + double expected = 5; + assertThat(result, equalTo(expected)); + } + + @Test + public void resolveRegisteredCustomBinaryOperator2() { + subject.registerBinaryOperator("?", 1, true, (a, b) -> (a >= 0) ? a : b); + + Formula formula = subject.parse("(5 - 10) ? (3 + 3)"); + double result = formula.evaluate(arena); + + double expected = 6; + assertThat(result, equalTo(expected)); + } + + } + @RunWith(Parameterized.class) public static class DefaultUnaryFunctions {