def test_merge_statements(self):

        code = (
            "from enthought.block_canvas.debug.my_operator import add, mul\n" "a = mul(1.0,2.0)\n" "b = add(1.0,3.0)\n"
        )
        model = ExecutionModel.from_code(code)
        model.merge_statements(ids=[model.statements[0].uuid, model.statements[1].uuid])
    def test_code_from_local_def(self):

        code = "def foo(a, b):\n" "\treturn b, a\n" "x,y = foo(1,2)\n"

        desired = "\ndef foo(a, b): \n    return b, a\n\nx, y = foo(1, 2)"
        model = ExecutionModel.from_code(code)
        self.assertEqual(desired, model.code)
    def setUp(self):
        unittest.TestCase.setUp(self)

        self.code = (
            "from enthought.block_canvas.debug.my_operator import mul, add\n"
            "a = mul(1,2)\n"
            "b = mul(5,6)\n"
            "c = add(a,b)"
        )
        self.model = ExecutionModel.from_code(self.code)
        self.controller = BlockGraphController(execution_model=self.model)
        self.file_path = "temp.pickle"
    def test_change_binding(self):
        """ Does a change in the binding update the code from the ExecutionModel
            correctly?
        """

        code = "from enthought.block_canvas.debug.my_operator import add\n" "a = add(1, 2)\n"

        model = ExecutionModel.from_code(code)
        func_call = model.statements[0]
        func_call.inputs[0].binding = "2"

        desired = "from enthought.block_canvas.debug.my_operator import add\n\n" "a = add(2, 2)"
        self.assertEqual(desired, model.code)
 def setUp(self):
     self.exec_code = (
         "from enthought.block_canvas.debug.my_operator import add, mul\n"
         "c = mul(a,b)\n"
         "d = add(c,b)\n"
         "f = mul(d,e)\n"
     )
     self.exec_model = ExecutionModel.from_code(self.exec_code)
     self.inputs = {}
     for stmt in self.exec_model.statements:
         for iv in stmt.inputs:
             self.inputs[iv.binding] = iv
     self.simple_context = dict(a=2, b=4, e=5)
    def test_local_def_code(self):

        code = (
            "from enthought.block_canvas.debug.my_operator import mul\n"
            "def foo(a):\n"
            "\tb = a\n"
            "\treturn b\n"
            "a = mul(1.0, 2.0)\n"
            "b = foo(a)\n"
        )

        desired = "def foo(a): \n    b = a\n    return b\n"
        model = ExecutionModel.from_code(code)
        assert model.code.find(desired) > 0
示例#7
0
    def __init__(self, code=None, shared_context=None, *args, **kwargs):
        super(Experiment, self).__init__(*args, **kwargs)

        if code is None:
            self.exec_model = ExecutionModel()
        else:
            self.exec_model = ExecutionModel.from_code(code)

        self.controller = BlockGraphController(execution_model = self.exec_model)
        self.canvas = BlockCanvas(graph_controller=self.controller)
        self.controller.canvas = self.canvas

        self._shared_context = shared_context
        self._local_context = DataContext(name = self._LOCAL_NAME_TEMPLATE())
        self._update_exec_context()
    def test_restricted(self):
        code = (
            "from enthought.block_canvas.debug.my_operator import add, mul\n"
            "c = mul(a,b)\n"
            "d = add(c,b)\n"
            "f = mul(d,e)\n"
        )

        model = ExecutionModel.from_code(code)

        not_really_restricted = model.restricted()
        self.assertEqual(not_really_restricted.sorted_statements, model.sorted_statements)

        cstmt, dstmt, fstmt = model.statements
        self.assertEqual(model.restricted(inputs=["e"]).sorted_statements, [fstmt])
        self.assertEqual(model.restricted(outputs=["d"]).sorted_statements, [cstmt, dstmt])
        self.assertEqual(model.restricted(inputs=["b"], outputs=["c"]).sorted_statements, [cstmt])
    def test_imported_func_multi_line(self):
        """ Right now, we can't ensure the order of the import statements nor
            can we consolidate imports from the same locations. """

        code = (
            "from enthought.block_canvas.debug.my_operator import add, mul\n"
            "a = add(1.0,2.0)\n"
            "b = add(a,3.0)\n"
            "c = mul(b,b)\n"
        )
        desired = ["a = add(1.0, 2.0)", "b = add(a, 3.0)", "c = mul(b, b)"]
        model = ExecutionModel.from_code(code)
        codelines = model.code.split("\n")
        for line in desired:
            assert line in codelines

        import_line = codelines[0]
        assert import_line.find("add") and import_line.find("mul")
示例#10
0
 def test_imported_func_code(self):
     code = "from enthought.block_canvas.debug.my_operator import mul\n" "c = mul(a, b)\n"
     desired = "from enthought.block_canvas.debug.my_operator import mul\n\nc = mul(a, b)"
     model = ExecutionModel.from_code(code)
     self.assertEqual(model.code, desired)
示例#11
0
    def test_import_func_with_const(self):

        code = "from enthought.block_canvas.debug.my_operator import mul\n" "a = mul(1.0, 2.0)\n"
        desired = "from enthought.block_canvas.debug.my_operator import mul\n\na = mul(1.0, 2.0)"
        model = ExecutionModel.from_code(code)
        self.assertEqual(model.code, desired)