Skip to content

Commit

Permalink
Fix isInf() overload method and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkwarren committed Sep 11, 2023
1 parent 63d2c4b commit 937f9dd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,12 @@ private static Overload isInf() {
value -> value.convertToNative(Double.TYPE).isInfinite() ? BoolT.True : BoolT.False,
(lhs, rhs) -> {
Double value = lhs.convertToNative(Double.TYPE);
return value.isInfinite(rhs.intValue()) ? BoolT.True : BoolT.False;
long sign = rhs.intValue();
if (sign == 0) {
return value.isInfinite() ? BoolT.True : BoolT.False;
}
double expectedValue = (sign > 0) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
return value == expectedValue ? BoolT.True : BoolT.False;
},
null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package build.buf.protovalidate.internal.celext;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.junit.Test;
import org.projectnessie.cel.Ast;
import org.projectnessie.cel.Env;
import org.projectnessie.cel.Library;
import org.projectnessie.cel.Program;
import org.projectnessie.cel.interpreter.Activation;

public class CustomOverloadTest {

private final Env env = Env.newEnv(Library.Lib(new ValidateLibrary()));

@Test
public void testIsInf() {
Map<String, Boolean> testCases =
ImmutableMap.<String, Boolean>builder()
.put("0.0.isInf()", false)
.put("(1.0/0.0).isInf()", true)
.put("(1.0/0.0).isInf(0)", true)
.put("(1.0/0.0).isInf(1)", true)
.put("(1.0/0.0).isInf(-1)", false)
.put("(-1.0/0.0).isInf()", true)
.put("(-1.0/0.0).isInf(0)", true)
.put("(-1.0/0.0).isInf(1)", false)
.put("(-1.0/0.0).isInf(-1)", true)
.build();
for (Map.Entry<String, Boolean> testCase : testCases.entrySet()) {
Program.EvalResult result = eval(testCase.getKey());
assertThat(result.getVal().booleanValue()).isEqualTo(testCase.getValue());
}
}

@Test
public void testIsNan() {
Map<String, Boolean> testCases =
ImmutableMap.<String, Boolean>builder()
.put("0.0.isNan()", false)
.put("(0.0/0.0).isNan()", true)
.put("(1.0/0.0).isNan()", false)
.build();
for (Map.Entry<String, Boolean> testCase : testCases.entrySet()) {
Program.EvalResult result = eval(testCase.getKey());
assertThat(result.getVal().booleanValue()).isEqualTo(testCase.getValue());
}
}

private Program.EvalResult eval(String source) {
return eval(source, Activation.emptyActivation());
}

private Program.EvalResult eval(String source, Object vars) {
Env.AstIssuesTuple parsed = env.parse(source);
assertThat(parsed.hasIssues()).isFalse();
Ast ast = parsed.getAst();
return env.program(ast).eval(vars);
}
}

0 comments on commit 937f9dd

Please sign in to comment.