Skip to content

Commit

Permalink
converts DSL tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
vmzakharov committed Jun 21, 2024
1 parent 102b06b commit 97980ee
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 282 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package io.github.vmzakharov.ecdataframe.dsl;

import io.github.vmzakharov.ecdataframe.dsl.value.DecimalValue;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.MathContext;

import static io.github.vmzakharov.ecdataframe.ExpressionTestUtil.evaluateScriptWithContext;
import static io.github.vmzakharov.ecdataframe.ExpressionTestUtil.evaluateToDecimal;
import static org.junit.jupiter.api.Assertions.*;

public class DecimalExpressionTest
{
@Test
public void literals()
{
Assert.assertEquals(BigDecimal.valueOf(579, 4), evaluateToDecimal("toDecimal(123,4) + toDecimal(456,4)"));
assertEquals(BigDecimal.valueOf(579, 4), evaluateToDecimal("toDecimal(123,4) + toDecimal(456,4)"));

Assert.assertEquals(BigDecimal.valueOf(-333, 4), evaluateToDecimal("toDecimal(123,4) - toDecimal(456,4)"));
assertEquals(BigDecimal.valueOf(-333, 4), evaluateToDecimal("toDecimal(123,4) - toDecimal(456,4)"));

Assert.assertEquals(BigDecimal.valueOf(56088, 8), evaluateToDecimal("toDecimal(123,4) * toDecimal(456,4)"));
assertEquals(BigDecimal.valueOf(56088, 8), evaluateToDecimal("toDecimal(123,4) * toDecimal(456,4)"));

Assert.assertEquals(
assertEquals(
BigDecimal.valueOf(123.0).divide(BigDecimal.valueOf(456.0), MathContext.DECIMAL128),
evaluateToDecimal("toDecimal(123, 4) / toDecimal(456, 4)"));
}
Expand All @@ -33,25 +33,25 @@ public void expressions()
context.setVariable("a", new DecimalValue(123456, 2));
context.setVariable("b", new DecimalValue(111, 2));

Assert.assertEquals(
assertEquals(
BigDecimal.valueOf(123567, 2), this.evaluateToDecimalWithContext("a + b", context));

Assert.assertEquals(
assertEquals(
BigDecimal.valueOf(123345, 2), this.evaluateToDecimalWithContext("a - b", context));

Assert.assertEquals(
assertEquals(
BigDecimal.valueOf(13703616, 4), this.evaluateToDecimalWithContext("a * b", context));

Assert.assertEquals(
assertEquals(
BigDecimal.valueOf(123456).divide(BigDecimal.valueOf(111), MathContext.DECIMAL128),
this.evaluateToDecimalWithContext("a / b", context));
}

@Test
public void mixedTypes()
{
Assert.assertEquals(BigDecimal.valueOf(20123, 4), evaluateToDecimal("toDecimal(123,4) + 2"));
Assert.assertEquals(BigDecimal.valueOf(2623, 4), evaluateToDecimal("toDecimal(123,4) + 0.25"));
assertEquals(BigDecimal.valueOf(20123, 4), evaluateToDecimal("toDecimal(123,4) + 2"));
assertEquals(BigDecimal.valueOf(2623, 4), evaluateToDecimal("toDecimal(123,4) + 0.25"));
}

private BigDecimal evaluateToDecimalWithContext(String expression, EvalContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,116 @@
import io.github.vmzakharov.ecdataframe.dsl.value.DateValue;
import io.github.vmzakharov.ecdataframe.dsl.value.StringValue;
import io.github.vmzakharov.ecdataframe.dsl.value.Value;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.*;

public class ObjectValueCompareOpTest
{
@Test
public void stringEq()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.EQ, new StringValue("Foo"), new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, Value.VOID, new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, new StringValue("Foo"), Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.EQ, new StringValue("Foo"), new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, Value.VOID, new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, new StringValue("Foo"), Value.VOID));
}

@Test
public void stringGt()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, new StringValue("Foo"), new StringValue("Bar")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, new StringValue("Bar"), new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, Value.VOID, new StringValue("Foo")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, new StringValue("Foo"), Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, new StringValue("Foo"), new StringValue("Bar")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, new StringValue("Bar"), new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, Value.VOID, new StringValue("Foo")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, new StringValue("Foo"), Value.VOID));
}

@Test
public void stringLt()
{
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, new StringValue("Foo"), new StringValue("Bar")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, new StringValue("Bar"), new StringValue("Foo")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, Value.VOID, new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, new StringValue("Foo"), Value.VOID));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, new StringValue("Foo"), new StringValue("Bar")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, new StringValue("Bar"), new StringValue("Foo")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, Value.VOID, new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, new StringValue("Foo"), Value.VOID));
}

@Test
public void stringGte()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Foo"), new StringValue("Bar")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Foo"), new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Bar"), new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, Value.VOID, new StringValue("Foo")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Foo"), Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Foo"), new StringValue("Bar")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Foo"), new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Bar"), new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, Value.VOID, new StringValue("Foo")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, new StringValue("Foo"), Value.VOID));
}

@Test
public void stringLte()
{
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Foo"), new StringValue("Bar")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Bar"), new StringValue("Bar")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Bar"), new StringValue("Foo")));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, Value.VOID, new StringValue("Foo")));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Foo"), Value.VOID));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Foo"), new StringValue("Bar")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Bar"), new StringValue("Bar")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Bar"), new StringValue("Foo")));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, Value.VOID, new StringValue("Foo")));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, new StringValue("Foo"), Value.VOID));
}

@Test
public void dateEq()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.EQ, this.dateValue(2020, 9, 1), this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, Value.VOID, this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, this.dateValue(2020, 9, 1), Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.EQ, this.dateValue(2020, 9, 1), this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, Value.VOID, this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.EQ, this.dateValue(2020, 9, 1), Value.VOID));
}

@Test
public void dateGt()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, Value.VOID, this.dateValue(2020, 9, 1)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, this.dateValue(2020, 9, 1), Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, Value.VOID, this.dateValue(2020, 9, 1)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GT, this.dateValue(2020, 9, 1), Value.VOID));
}

@Test
public void dateLt()
{
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, Value.VOID, this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, this.dateValue(2020, 9, 1), Value.VOID));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LT, Value.VOID, this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, this.dateValue(2020, 9, 1), Value.VOID));
}

@Test
public void dateGte()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, Value.VOID, this.dateValue(2020, 9, 1)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 9, 1), Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GTE, Value.VOID, this.dateValue(2020, 9, 1)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, this.dateValue(2020, 9, 1), Value.VOID));
}

@Test
public void dateLte()
{
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 9, 1)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, Value.VOID, this.dateValue(2020, 9, 1)));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 9, 1), Value.VOID));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 8, 17)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 9, 1), this.dateValue(2020, 9, 1)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 8, 17), this.dateValue(2020, 9, 1)));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, Value.VOID, this.dateValue(2020, 9, 1)));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LTE, this.dateValue(2020, 9, 1), Value.VOID));
}

@Test
public void voidValuesComparisons()
{
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.EQ, Value.VOID, Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.EQ, Value.VOID, Value.VOID));

Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, Value.VOID, Value.VOID));
Assert.assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, Value.VOID, Value.VOID));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.GT, Value.VOID, Value.VOID));
assertFalse(ExpressionTestUtil.evaluate(ComparisonOp.LT, Value.VOID, Value.VOID));

Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, Value.VOID, Value.VOID));
Assert.assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, Value.VOID, Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.LTE, Value.VOID, Value.VOID));
assertTrue(ExpressionTestUtil.evaluate(ComparisonOp.GTE, Value.VOID, Value.VOID));
}

private DateValue dateValue(int year, int month, int dayOfMonth)
Expand Down
Loading

0 comments on commit 97980ee

Please sign in to comment.