Skip to content

Commit

Permalink
Bulk up the test suite for FormulaManager.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
garbagemule committed Oct 22, 2023
1 parent 58f1423 commit bbe7ed4
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit bbe7ed4

Please sign in to comment.