Exemplo n.º 1
0
 def test_start_if(self):            
     self.fb = _FunctionBuilder("foo",[],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_if(True)
         self.fb.create_statement("return True")
     self.assertEquals(True, self.fb.get_function()())
     
     self.fb = _FunctionBuilder("foo",[],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_if(False)
         self.fb.create_statement("return True")
     self.assertEquals(None, self.fb.get_function()())
Exemplo n.º 2
0
 def test_scope(self):
     glob = { "alpha": 7, "gamma": 9 }
     self.fb = _FunctionBuilder("foo",[],{},glob,{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.create_statement("return alpha + gamma")
     f = self.fb.get_function()
     self.assertEquals(16, f())
Exemplo n.º 3
0
 def test_args(self):
     self.fb = _FunctionBuilder("foo",["alpha","beta"],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.create_statement("return alpha + beta")
     f = self.fb.get_function()
     self.assertEquals(3, f(1,2))
     self.assertEquals("XD", f("X","D"))
Exemplo n.º 4
0
 def test_start_with_both_args(self):
     self.fb = _FunctionBuilder("foo",["context"],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_with([[self.fb.create_statement("context"),self.fb.create_statement("c")]])
         self.fb.create_statement("return c")
     c = MockContext()
     self.assertEquals(c, self.fb.get_function()(c))
     self.assertEquals(["entered","exited"], c.log)
Exemplo n.º 5
0
 def test_keyword_args(self):
     self.fb = _FunctionBuilder("foo",[],{"alpha":1,"beta":2},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.create_statement("return alpha + beta")
     f = self.fb.get_function()
     self.assertEquals(3, f())
     self.assertEquals(6, f(4))
     self.assertEquals(9, f(5,4))
     self.assertEquals(5, f(beta=3,alpha=2))
Exemplo n.º 6
0
 def test_start_else(self):
     self.fb = _FunctionBuilder("foo",["val"],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_if(self.fb.create_statement("val"))
         self.fb.create_statement("return True")
         self.fb.end_block()
         self.fb.start_else()
         self.fb.create_statement("return False")
     f = self.fb.get_function()
     self.assertEquals(True, f(True))
     self.assertEquals(False, f(False))
Exemplo n.º 7
0
 def test_start_with_two_contexts(self):
     self.fb = _FunctionBuilder("foo",["context1","context2"],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_with([[self.fb.create_statement("context1"),None],
                 [self.fb.create_statement("context2"),self.fb.create_statement("c")]])
         self.fb.create_statement("return c")
     c1 = MockContext()
     c2 = MockContext()
     self.assertEquals(c2, self.fb.get_function()(c1,c2))
     self.assertEquals(["entered","exited"], c1.log)
     self.assertEquals(["entered","exited"], c2.log)
Exemplo n.º 8
0
 def test_start_finally(self):
     self.fb = _FunctionBuilder("foo",[],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_try()
         self.fb.create_statement("pass")
         self.fb.end_block()
         self.fb.start_finally()
         self.fb.create_statement("return True")
         self.fb.end_block()    
     self.assertEquals(True, self.fb.get_function()())
     
     self.fb = _FunctionBuilder("foo",[],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)
     with self.fb:
         self.fb.start_try()
         self.fb.create_statement("[][1]")
         self.fb.end_block()
         self.fb.start_finally()
         self.fb.create_statement("return True")
         self.fb.end_block()    
     self.assertEquals(True, self.fb.get_function()())
Exemplo n.º 9
0
 def setUp(self):
     self.fb = _FunctionBuilder("foo",[],{},{},{},self.is_statement,
             self.make_statement, self.make_block, self.make_interface)