# Copyright 2006-2007 Grant T. Olson.
# See LICENSE for terms.
from  compyler.codeObjectAssembler import codeObjectAssembler
from pyasm.x86cpToMemory import CpToMemory
import unittest, cStringIO, sys

execTests = [
    ("""
a = 1
b = 2

print a
print b
print a + b

""",""" 1\n 2\n 3\n"""),]
             



class test_exec(unittest.TestCase):
    def eval_code(self, code):
        x = codeObjectAssembler()
        co = compile(code,"","exec")
        assem = x.assembleCodeObject(co)
        mem = CpToMemory(assem)
        mem.MakeMemory()
        mem.BindPythonFunctions(globals())
            
        results = cStringIO.StringIO();
        _stdout,sys.stdout = sys.stdout,results

        try:
            main()
            results.seek(0)
            return results.read()
        finally:
            sys.stdout = _stdout
            
    def test_exec(self):
        for test in execTests:
            in_,out = test[0],test[1]
            self.failUnlessEqual(self.eval_code(in_),out)
            
if __name__ == "__main__":
    unittest.main()    
