def test_string_append(self): context = Context() parse(shell % 'x = "a";\nx = x + x;').execute(context) self.assertEqual(context.root_frame.values["x"], "aa") context = Context() parse(shell % 'x = "a" + "b" + "c";').execute(context) self.assertEqual(context.root_frame.values["x"], "abc") with self.assertRaises(ParseException): context = Context() parse(shell % 'x = "a" + 1;').execute(context) self.assertEqual(context.root_frame.values["x"], "abc")
def test_comments(self): context = Context() parse(shell % 'x = 1000; x = x * -0.1; x = x + 10; x = x - 1;').execute(context) self.assertEqual(context.root_frame.values["x"], -91.0) context = Context() parse(shell % 'x = 1000; x = x * -0.1; /* x = x + 10; */ x = x - 1;').execute(context) self.assertEqual(context.root_frame.values["x"], -101.0) context = Context() parse(shell % 'x = 1000; x = x * -0.1; # x = x + 10; \n x = x - 1;').execute(context) self.assertEqual(context.root_frame.values["x"], -101.0) context = Context() parse(shell % 'x = 1000; x = x * -0.1; // x = x + 10; \n x = x - 1;').execute(context) self.assertEqual(context.root_frame.values["x"], -101.0)
def index(): data = request.get_json() res = {} context = Context() try: parse(data["code"]).execute(context) except ParseException as err: res["error"] = {"lineno": err.lineno, "col": err.col, "message": str(err)} except StackException as err: res["error"] = {"lineno": 1, "col": 0, "message": "Stack limit exceeded."} except IterationException as err: res["error"] = {"lineno": 1, "col": 0, "message": "Iteration limit exceeded."} else: res["nodes"] = build_nodes(context.root_frame) return res
def test_array_reference(self): context = Context() parse(shell % 'x = ["a"] + ["a"]; x = x[1];').execute(context) self.assertEqual(context.root_frame.values["x"], "a")
def test_array_add(self): context = Context() parse(shell % 'x = ["a"] + ["a"];').execute(context) self.assertEqual(context.root_frame.values["x"], ["a", "a"])
def test_if_then_else(self): context = Context() parse(shell % 'x = 1000; if (x < 10) { x = x * 100; } else { x = x / 100; }').execute(context) self.assertEqual(context.root_frame.values["x"], 10.0)
def test_evaluate_numbers(self): context = Context() parse(shell % 'x = 1000; x = x * -0.1;').execute(context) self.assertEqual(context.root_frame.values["x"], -100.0)
def test_array_append(self): context = Context() parse(shell % 'x = []; x.append("a");').execute(context) self.assertEqual(context.root_frame.values["x"], ["a"])