public void testComputeForJsSrc() {
RoundFunction roundFunction = new RoundFunction();
JsExpr floatExpr = new JsExpr("FLOAT_JS_CODE", Integer.MAX_VALUE);
assertEquals(new JsExpr("Math.round(FLOAT_JS_CODE)", Integer.MAX_VALUE),
roundFunction.computeForJsSrc(ImmutableList.of(floatExpr)));
JsExpr numDigitsAfterPtExpr = new JsExpr("0", Integer.MAX_VALUE);
assertEquals(new JsExpr("Math.round(FLOAT_JS_CODE)", Integer.MAX_VALUE),
roundFunction.computeForJsSrc(
ImmutableList.of(floatExpr, numDigitsAfterPtExpr)));
numDigitsAfterPtExpr = new JsExpr("4", Integer.MAX_VALUE);
assertEquals(new JsExpr("Math.round(FLOAT_JS_CODE * 10000) / 10000",
Operator.DIVIDE_BY.getPrecedence()),
roundFunction.computeForJsSrc(
ImmutableList.of(floatExpr, numDigitsAfterPtExpr)));
numDigitsAfterPtExpr = new JsExpr("-2", Operator.NEGATIVE.getPrecedence());
assertEquals(new JsExpr("Math.round(FLOAT_JS_CODE / 100) * 100",
Operator.TIMES.getPrecedence()),
roundFunction.computeForJsSrc(
ImmutableList.of(floatExpr, numDigitsAfterPtExpr)));
numDigitsAfterPtExpr = new JsExpr("NUM_DIGITS_JS_CODE", Integer.MAX_VALUE);
assertEquals(new JsExpr("Math.round(FLOAT_JS_CODE * Math.pow(10, NUM_DIGITS_JS_CODE)) /" +
" Math.pow(10, NUM_DIGITS_JS_CODE)",
Operator.DIVIDE_BY.getPrecedence()),
roundFunction.computeForJsSrc(
ImmutableList.of(floatExpr, numDigitsAfterPtExpr)));
}