public void testNull() throws Exception { try { JavascriptCompiler.compile(null); fail(); } catch (NullPointerException expected) { // expected exception } }
void doTestInvalidVariable(String variable) { try { JavascriptCompiler.compile(variable); fail("\"" + variable + " should have failed to compile"); } catch (ParseException expected) { // expected } }
public void testInvalidLexer() throws Exception { try { JavascriptCompiler.compile("\n ."); fail(); } catch (ParseException pe) { assertTrue(pe.getMessage().contains("unexpected character '.' on line (2) position (1)")); } }
public void testVariableNormalization() throws Exception { // multiple double quotes Expression x = JavascriptCompiler.compile("foo[\"a\"][\"b\"]"); assertEquals("foo['a']['b']", x.variables[0]); // single and double in the same var x = JavascriptCompiler.compile("foo['a'][\"b\"]"); assertEquals("foo['a']['b']", x.variables[0]); // escapes remain the same in single quoted strings x = JavascriptCompiler.compile("foo['\\\\\\'\"']"); assertEquals("foo['\\\\\\'\"']", x.variables[0]); // single quotes are escaped x = JavascriptCompiler.compile("foo[\"'\"]"); assertEquals("foo['\\'']", x.variables[0]); // double quotes are unescaped x = JavascriptCompiler.compile("foo[\"\\\"\"]"); assertEquals("foo['\"']", x.variables[0]); // backslash escapes are kept the same x = JavascriptCompiler.compile("foo['\\\\'][\"\\\\\"]"); assertEquals("foo['\\\\']['\\\\']", x.variables[0]); }
public void testEmpty() { try { JavascriptCompiler.compile(""); fail(); } catch (ParseException expected) { // expected exception } try { JavascriptCompiler.compile("()"); fail(); } catch (ParseException expected) { // expected exception } try { JavascriptCompiler.compile(" \r\n \n \t"); fail(); } catch (ParseException expected) { // expected exception } }
public void testWrongArity() throws Exception { try { JavascriptCompiler.compile("tan()"); fail(); } catch (ParseException expected) { assertEquals( "Invalid expression 'tan()': Expected (1) arguments for function call (tan), but found (0).", expected.getMessage()); assertEquals(expected.getErrorOffset(), 0); } try { JavascriptCompiler.compile("tan(1, 1)"); fail(); } catch (ParseException expected) { assertTrue(expected.getMessage().contains("arguments for function call")); } try { JavascriptCompiler.compile(" tan()"); fail(); } catch (ParseException expected) { assertEquals( "Invalid expression ' tan()': Expected (1) arguments for function call (tan), but found (0).", expected.getMessage()); assertEquals(expected.getErrorOffset(), 1); } try { JavascriptCompiler.compile("1 + tan()"); fail(); } catch (ParseException expected) { assertEquals( "Invalid expression '1 + tan()': Expected (1) arguments for function call (tan), but found (0).", expected.getMessage()); assertEquals(expected.getErrorOffset(), 4); } }
public void testInvalidCompiles() throws Exception { try { JavascriptCompiler.compile("100 100"); fail(); } catch (ParseException expected) { // expected exception } try { JavascriptCompiler.compile("7*/-8"); fail(); } catch (ParseException expected) { // expected exception } try { JavascriptCompiler.compile("0y1234"); fail(); } catch (ParseException expected) { // expected exception } try { JavascriptCompiler.compile("500EE"); fail(); } catch (ParseException expected) { // expected exception } try { JavascriptCompiler.compile("500.5EE"); fail(); } catch (ParseException expected) { // expected exception } }
void doTestValidVariable(String variable, String output) throws Exception { Expression e = JavascriptCompiler.compile(variable); assertNotNull(e); assertEquals(1, e.variables.length); assertEquals(output, e.variables[0]); }
public void testValidCompiles() throws Exception { assertNotNull(JavascriptCompiler.compile("100")); assertNotNull(JavascriptCompiler.compile("valid0+100")); assertNotNull(JavascriptCompiler.compile("valid0+\n100")); assertNotNull(JavascriptCompiler.compile("logn(2, 20+10-5.0)")); }