Skip to content

Commit

Permalink
Add tests to handle null variables in script engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrail committed Apr 3, 2021
1 parent 9af6453 commit af35d90
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,27 @@ public void testEngineBindings() throws IOException, ScriptException {
engine.put("a", "a");
engine.put("b", "b");
engine.put("c", "c");
engine.put("actuallyNull", null);

// Ensure that stuff we just stuck in bindings made it to a global
engine.eval(new FileReader("testsrc/assert.js"));
engine.eval("assertEquals(string, 'Hello');\n"
+ "assertEquals(integer, 123);\n"
+ "assertEquals(a, 'a');\n"
+ "assertEquals(b, 'b');\n"
+ "assertEquals(c, 'c');\n"
+ "assertNull(actuallyNull);\n"
+ "string = 'Goodbye';\n"
+ "assertEquals(string, 'Goodbye');");
assertEquals(engine.get("string"), "Goodbye");

Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
// Make sure that null works the way we expect
assertTrue(engineBindings.containsKey("actuallyNull"));
assertNull(engineBindings.get("actuallyNull"));

// Make sure we can delete
engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("string");
engineBindings.remove("string");
// This will throw because string is undefined
assertThrows(ScriptException.class, () -> {
engine.eval("let failing = string + '123';");
Expand Down

0 comments on commit af35d90

Please sign in to comment.