Skip to content

Commit

Permalink
fix: adds support to for the NEG Unary Operator in VisitorPartialEval…
Browse files Browse the repository at this point in the history
…uator (#3193)
  • Loading branch information
stone-wall authored and monperrus committed Dec 15, 2019
1 parent 9db3f61 commit 5e23a7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {
case NOT:
res.setValue(!(Boolean) object);
break;
case NEG:
res.setValue(convert(operator.getType(),
-1 * ((Number) object).longValue()));
break;
default:
throw new RuntimeException("unsupported operator " + operator.getKind());
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/eval/EvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ public void testVisitorPartialEvaluator_binary() {
}
}

@Test
public void testVisitorPartialEvaluator_unary() {

{ // NEG urnary operator
Launcher launcher = new Launcher();
CtCodeElement el =
launcher.getFactory().Code().createCodeSnippetExpression("-(100+1)").compile();
VisitorPartialEvaluator eval = new VisitorPartialEvaluator();
CtElement element = eval.evaluate(el);
assertEquals("-101", element.toString());
}
}

@Test
public void testVisitorPartialEvaluator_if() {
Launcher launcher = new Launcher();
Expand Down

0 comments on commit 5e23a7e

Please sign in to comment.