示例#1
0
 def test_it_really_works(self):
     ctx = Py27EmitterContext().emit(
         Function(('a', 'b'), "add two things together",
             Return(Add(Load('a'), Load('b')))))
     code = ctx.make_code(ctx.last_builder, name="add")
     globals = {}
     name = None
     argdefs = ()
     closure = ()
     # function(code, globals[, name[, argdefs[, closure]]])
     add = types.FunctionType(code, globals, name, argdefs, closure)
     # Now see if it really works
     self.assertEqual(add(1, 2), 3)
     self.assertEqual(add("hello", " world"), "hello world")
     self.assertEqual(add(['foo'], ['bar']), ['foo', 'bar'])
示例#2
0
 def test_Multiply_sanity(self):
     self.assertPerfectCode(
         lambda a, b: a * b,
         Function(('a', 'b'), None, Return(Multiply(Load("a"), Load("b")))))
示例#3
0
 def test_Subtract_sanity(self):
     self.assertPerfectCode(
         lambda a, b: a - b,
         Function(('a', 'b'), None, Return(Subtract(Load("a"), Load("b")))))
示例#4
0
 def test_Add_sanity(self):
     self.assertPerfectCode(
         lambda a, b: a + b,
         Function(('a', 'b'), None, Return(Add(Load("a"), Load("b")))))
示例#5
0
 def test_Const_sanity(self):
     self.assertPerfectCode(
         lambda: "hi",
         Function((), None, Return(Const("hi"))))
示例#6
0
 def test_Neg_sanity(self):
     self.assertPerfectCode(
         lambda a: -a,
         Function(('a',), None, Return(Neg(Load("a")))))
示例#7
0
 def test_Store_sanity(self):
     def fn(): a = 1
     self.assertPerfectCode(
         fn,
         Function((), None, Store("a", Const(1)), Return(Const(None))))