Exemplo n.º 1
0
 def test_get_referenced_varpaths(self):
     ex = ExprEvaluator('comp.x[0] = 10*(3.2+ a.a1d[3]* 1.1*a.a1d[2 ])', self.top.a)
     self.assertEqual(ex.get_referenced_varpaths(), set(['comp.x','a.a1d']))
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.get_referenced_varpaths(), set(['comp.contlist']))
     ex.scope = self.top.comp
     ex.text = 'comp.contlist[1]'
     self.assertEqual(ex.get_referenced_varpaths(), set(['comp.contlist']))
Exemplo n.º 2
0
 def test_resolve(self):
     ex = ExprEvaluator('comp.x[0] = 10*(3.2+ a.a1d[3]* 1.1*a.a1d[2 ])', self.top)
     self.assertEqual(ex.check_resolve(), True)
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.check_resolve(), True)
     ex.scope = self.top.comp
     ex.text = 'contlist[1]'
     self.assertEqual(ex.check_resolve(), True)
     ex.text = 'contlist[1]-foo.flambe'
     self.assertEqual(ex.check_resolve(), False)
 def test_resolve(self):
     ex = ExprEvaluator('comp.x[0] = 10*(3.2+ a.a1d[3]* 1.1*a.a1d[2 ])', self.top)
     self.assertEqual(ex.check_resolve(), True)
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.check_resolve(), True)
     ex.scope = self.top.comp
     ex.text = 'contlist[1]'
     self.assertEqual(ex.check_resolve(), True)
     ex.text = 'contlist[1]-foo.flambe'
     self.assertEqual(ex.check_resolve(), False)
 def test_get_referenced_varpaths(self):
     ex = ExprEvaluator('comp.x[0] = 10*(3.2+ a.a1d[3]* 1.1*a.a1d[2 ])',
                        self.top.a)
     self.assertEqual(ex.get_referenced_varpaths(), set(['comp.x',
                                                         'a.a1d']))
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.get_referenced_varpaths(), set(['comp.contlist']))
     ex.scope = self.top.comp
     ex.text = 'comp.contlist[1]'
     self.assertEqual(ex.get_referenced_varpaths(), set(['comp.contlist']))
Exemplo n.º 5
0
 def test_slice(self):
     ex = ExprEvaluator('a1d[1::2]', self.top.a)
     self.assertTrue(all(array([2., 4., 6.]) == ex.evaluate()))
     ex.text = 'a1d[2:4]'
     self.assertTrue(all(array([3., 4.]) == ex.evaluate()))
     ex.text = 'a1d[2:]'
     self.assertTrue(all(array([3., 4., 5., 6.]) == ex.evaluate()))
     ex.text = 'a1d[::-1]'
     self.assertTrue(all(array([6., 5., 4., 3., 2., 1.]) == ex.evaluate()))
     ex.text = 'a1d[:2]'
     self.assertTrue(all(array([1., 2.]) == ex.evaluate()))
Exemplo n.º 6
0
 def test_slice(self):
     ex = ExprEvaluator('a1d[1::2]', self.top.a)
     self.assertTrue(all(numpy.array([2.,4.,6.]) == ex.evaluate()))
     ex.text = 'a1d[2:4]'
     self.assertTrue(all(numpy.array([3.,4.]) == ex.evaluate()))
     ex.text = 'a1d[2:]'
     self.assertTrue(all(numpy.array([3.,4.,5.,6.]) == ex.evaluate()))
     ex.text = 'a1d[::-1]'
     self.assertTrue(all(numpy.array([6.,5.,4.,3.,2.,1.]) == ex.evaluate()))
     ex.text = 'a1d[:2]'
     self.assertTrue(all(numpy.array([1.,2.]) == ex.evaluate()))
Exemplo n.º 7
0
 def test_get_referenced_compnames(self):
     ex = ExprEvaluator('comp.x[0] = 10*(3.2+ a.a1d[3]* 1.1*a.a1d[2 ].foobar)', self.top.a)
     self.assertEqual(ex.get_referenced_compnames(), set(['comp', 'a']))
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.get_referenced_compnames(), set(['comp']))
     ex.scope = self.top.comp
     ex.text = 'comp.contlist[1]'
     self.assertEqual(ex.get_referenced_compnames(), set(['comp']))
     ex.text = 'comp.contlist[1].foo'
     self.assertEqual(ex.get_referenced_compnames(), set(['comp']))
     ex.text = 'contlist[1].foo'
     self.assertEqual(ex.get_referenced_compnames(), set())
     ex.text = 'asm2.comp3.contlist[1].foo'
     self.assertEqual(ex.get_referenced_compnames(), set(['asm2']))
 def test_get_referenced_compnames(self):
     ex = ExprEvaluator('comp.x[0] = 10*(3.2+ a.a1d[3]* 1.1*a.a1d[2 ].foobar)', self.top.a)
     self.assertEqual(ex.get_referenced_compnames(), set(['comp', 'a']))
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.get_referenced_compnames(), set(['comp']))
     ex.scope = self.top.comp
     ex.text = 'comp.contlist[1]'
     self.assertEqual(ex.get_referenced_compnames(), set(['comp']))
     ex.text = 'comp.contlist[1].foo'
     self.assertEqual(ex.get_referenced_compnames(), set(['comp']))
     ex.text = 'contlist[1].foo'
     self.assertEqual(ex.get_referenced_compnames(), set())
     ex.text = 'asm2.comp3.contlist[1].foo'
     self.assertEqual(ex.get_referenced_compnames(), set(['asm2']))
Exemplo n.º 9
0
    def test_reparse_on_scope_change(self):
        self.top.comp.x = 99.5
        self.top.comp.y = -3.14

        ex = ExprEvaluator('comp.x', self.top)
        self.assertEqual(99.5, ex.evaluate())
        self.assertEqual(new_text(ex), "scope.get('comp.x')")

        ex.scope = self.top.a
        try:
            ex.set(0.5)
        except AttributeError as err:
            self.assertEqual(str(err), "a: object has no attribute 'comp.x'")
        else:
            self.fail("AttributeError expected")
        self.assertEqual(new_text(ex), "scope.get('comp.x')")
        self.assertEqual(99.5,
                         ex.evaluate(self.top))  # set scope back to self.top
        self.assertEqual(new_text(ex), "scope.get('comp.x')")

        ex.text = 'comp.y'
        try:
            ex.evaluate(self.top.a)
        except AttributeError as err:
            self.assertEqual(
                str(err), "can't evaluate expression 'comp.y':"
                " a: 'A' object has no attribute 'comp'")
        else:
            self.fail("AttributeError expected")
        ex.scope = self.top
        ex.set(11.1)
        self.assertEqual(11.1, self.top.comp.y)
        self.assertEqual(new_text(ex), "scope.get('comp.y')")
Exemplo n.º 10
0
    def test_reparse_on_scope_change(self):
        self.top.comp.x = 99.5
        self.top.comp.y = -3.14

        ex = ExprEvaluator('comp.x', self.top)
        self.assertEqual(99.5, ex.evaluate())
        self.assertEqual(new_text(ex), "scope.get('comp.x')")

        ex.scope = self.top.a
        try:
            ex.set(0.5)
        except AttributeError as err:
            self.assertEqual(str(err), "a: object has no attribute 'comp.x'")
        else:
            self.fail("AttributeError expected")
        self.assertEqual(new_text(ex), "scope.get('comp.x')")
        self.assertEqual(99.5, ex.evaluate(self.top)) # set scope back to self.top
        self.assertEqual(new_text(ex), "scope.get('comp.x')")

        ex.text = 'comp.y'
        try:
            ex.evaluate(self.top.a)
        except AttributeError as err:
            self.assertEqual(str(err), "can't evaluate expression 'comp.y':"
                             " a: 'A' object has no attribute 'comp'")
        else:
            self.fail("AttributeError expected")
        ex.scope = self.top
        ex.set(11.1)
        self.assertEqual(11.1, self.top.comp.y)
        self.assertEqual(new_text(ex), "scope.get('comp.y')")
Exemplo n.º 11
0
 def test_no_scope(self):
     ex = ExprEvaluator('abs(-3)+int(2.3)+math.floor(5.4)')
     self.assertEqual(ex.evaluate(), 10.0)
     
     ex.text = 'comp.x'
     try:
         ex.evaluate()
     except Exception, err:
         self.assertEqual(str(err), "can't evaluate expression 'comp.x': expression has no scope")
Exemplo n.º 12
0
    def test_no_scope(self):
        ex = ExprEvaluator('abs(-3)+int(2.3)+math.floor(5.4)')
        self.assertEqual(ex.evaluate(), 10.0)

        ex.text = 'comp.x'
        try:
            ex.evaluate()
        except Exception, err:
            self.assertEqual(str(err), "can't evaluate expression 'comp.x':"
                                       " 'NoneType' object has no attribute 'get'")
Exemplo n.º 13
0
    def test_no_scope(self):
        ex = ExprEvaluator('abs(-3)+int(2.3)+math.floor(5.4)')
        self.assertEqual(ex.evaluate(), 10.0)

        ex.text = 'comp.x'
        try:
            ex.evaluate()
        except Exception, err:
            self.assertEqual(str(err), "can't evaluate expression 'comp.x':"
                             " 'NoneType' object has no attribute 'get'")
Exemplo n.º 14
0
    def test_no_scope(self):
        ex = ExprEvaluator('abs(-3)+int(2.3)+math.floor(5.4)')
        self.assertEqual(ex.evaluate(), 10.0)

        ex.text = 'comp.x'
        try:
            ex.evaluate()
        except Exception, err:
            self.assertEqual(
                str(err),
                "can't evaluate expression 'comp.x': expression has no scope")
Exemplo n.º 15
0
 def test_assignee(self):
     ex = ExprEvaluator('a1d[3]*a1d[2 ]', self.top.a)
     self.assertEqual(ex.is_valid_assignee(), False)
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.is_valid_assignee(), True)
Exemplo n.º 16
0
 def test_assignee(self):
     ex = ExprEvaluator('a1d[3]*a1d[2 ]', self.top.a)
     self.assertEqual(ex.is_valid_assignee(), False)
     ex.text = 'comp.contlist[1].a2d[2][1]'
     self.assertEqual(ex.is_valid_assignee(), True)