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'])
def test_Multiply_sanity(self): self.assertPerfectCode( lambda a, b: a * b, Function(('a', 'b'), None, Return(Multiply(Load("a"), Load("b")))))
def test_Subtract_sanity(self): self.assertPerfectCode( lambda a, b: a - b, Function(('a', 'b'), None, Return(Subtract(Load("a"), Load("b")))))
def test_Add_sanity(self): self.assertPerfectCode( lambda a, b: a + b, Function(('a', 'b'), None, Return(Add(Load("a"), Load("b")))))
def test_Const_sanity(self): self.assertPerfectCode( lambda: "hi", Function((), None, Return(Const("hi"))))
def test_Neg_sanity(self): self.assertPerfectCode( lambda a: -a, Function(('a',), None, Return(Neg(Load("a")))))
def test_Store_sanity(self): def fn(): a = 1 self.assertPerfectCode( fn, Function((), None, Store("a", Const(1)), Return(Const(None))))