示例#1
0
    def test_string_replace(self):
        context = Context()
        parse(shell % 'x = "a"; x.replace("a", "");').execute(context)
        self.assertEqual(context.root_frame.values["x"], "")

        context = Context()
        parse(shell % 'x = "abc"; x.replace("b", "");').execute(context)
        self.assertEqual(context.root_frame.values["x"], "ac")
示例#2
0
    def test_string_insert(self):
        context = Context()
        parse(shell % 'x = ""; x.insert(0, "a");').execute(context)
        self.assertEqual(context.root_frame.values["x"], "a")

        context = Context()
        parse(shell % 'x = "abc"; x.insert(1, "x");').execute(context)
        self.assertEqual(context.root_frame.values["x"], "axbc")
示例#3
0
    def test_array_pop(self):
        context = Context()
        parse(shell % 'x = [1]; y = x.pop();').execute(context)
        self.assertEqual(context.root_frame.values["x"], [])
        self.assertEqual(context.root_frame.values["y"], 1)

        context = Context()
        parse(shell % 'x = [1, 2, 3]; y = x.pop(0);').execute(context)
        self.assertEqual(context.root_frame.values["x"], [2.0, 3.0])
        self.assertEqual(context.root_frame.values["y"], 1)
示例#4
0
文件: routes.py 项目: ptmcg/recurser
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
示例#5
0
    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)
示例#6
0
    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")
示例#7
0
 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")
示例#8
0
 def test_array_add(self):
     context = Context()
     parse(shell % 'x = ["a"] + ["a"];').execute(context)
     self.assertEqual(context.root_frame.values["x"], ["a", "a"])
示例#9
0
 def test_error_location(self):
     with self.assertRaises(Exception) as pe:
         parse(shell % 'for (i=0; i < 1000; i = i +) { a = 100; }')
     self.assertEqual(pe.exception.loc, 35)
示例#10
0
 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)
示例#11
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)
示例#12
0
 def test_array_append(self):
     context = Context()
     parse(shell % 'x = []; x.append("a");').execute(context)
     self.assertEqual(context.root_frame.values["x"], ["a"])