# Copyright 2006-2007 Grant T. Olson.
# See LICENSE for terms.
import unittest
from compyler.test.util import eval_code, eval_raises_code

stmts = [

# try - no problem
    ("""

try:
    print 1,2,3
except:
    print "ERROR"


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


# try with raise
("""

try:
    print 1 + "a"
except:
    print "ERROR"


""","""ERROR\n"""),
    
# try with raise and code after.
("""

print "BEFORE ERROR"

try:
    print 1 + "a"
except:
    print "ERROR"

print "AFTER ERROR"

""","""BEFORE ERROR\nERROR\nAFTER ERROR\n"""),

# try with raise and typed catch
("""

try:
    print 1 + "a"
except ValueError:
    print "THIS ERROR DOESN'T GET THROWN"
except TypeError:
    print "THIS ERROR DOES GET THROWN"
except Exception:
    print "ERROR IS DEFAULT, SHOULDN'T GET HERE"


""","""THIS ERROR DOES GET THROWN\n"""),
]

raiseStmts = [
("""

try:
    print 1 + "a"
except ValueError:
    print "GOT HERE"


""",TypeError),

("""

try:
    print 1 + "a"
finally:
    print "GOT HERE"


""",TypeError),
]


class test_opcodes_try(unittest.TestCase):
    def test_try_statements(self):
        for stmt in stmts:
            self.failUnless(eval_code(stmt[0],stmt[1],"exec",title='test_opcodes_try'))

    def test_raise_statements(self):
        return
        for stmt in raiseStmts:
            self.failUnlessRaises(stmt[1],eval_raises_code,stmt[0],"exec")
                
if __name__ == "__main__":
    unittest.main()

