# Path: tests/util/asserts.py
# def assert_that(obj, matcher):
#     assert_implements(obj, object)
#     assert_implements(matcher, Matcher)
#     if not matcher.matches(obj):
#         raise AssertionError(matcher.description())
# 
# def assert_fails(error, fn, *args):
#     try:
#         fn(*args)
#         fail("Should throw")
#     except Exception as e:
#         assert_that(str(e), equals(error))
# 
# def equals(expected):
#     class EqualsMatcher:
# 
#         def __init__(self, expected):
#             self.expected = expected
# 
#         def matches(self, actual):
#             self.actual = actual
#             return actual == expected
# 
#         def description(self):
#             ret = (
#                 repr(self.actual)
#                 + " does not equal "
#                 + repr(self.expected)
#                 + " as expected."
#             )
#             if len(ret) > 80:
#                 ret = """Values are not equal (actual then expected):
# %s
# %s
# """ % ( repr(self.actual), repr(self.expected))
#             return ret
#     return EqualsMatcher(expected)
# 
# Path: tests/util/test.py
# def test(t):
#     return TestFunctionType(t)
# 
# Path: pycell/lexer.py
# def lex(chars_iter):
#     chars = PeekableStream(chars_iter)
#     while chars.next is not None:
#         c = chars.move_next()
#         if c in " \n":              pass           # Ignore white space
#         elif c in "(){},;=:":       yield (c, "")  # Special characters
#         elif c in "+-*/":           yield ("operation", c)
#         elif c in ("'", '"'):       yield ("string", _scan_string(c, chars))
#         elif re.match("[.0-9]", c): yield ("number", _scan(c, chars, "[.0-9]"))
#         elif re.match("[_a-zA-Z]", c):
#             yield ("symbol", _scan(c, chars, "[_a-zA-Z0-9]"))
#         elif c == "\t":
#             raise Exception("Tab characters are not allowed in Cell.")
#         else:
#             raise Exception("Unrecognised character: '" + c + "'.")
# 
# Path: pycell/parser.py
# def parse(tokens_iterator):
#     parser = Parser(PeekableStream(tokens_iterator), ";")
#     while parser.tokens.next is not None:
#         p = parser.next_expression(None)
#         if p is not None:
#             yield p
#         parser.tokens.move_next()
# 
# Path: pycell/eval_.py
# def eval_expr(expr, env):
#     typ = expr[0]
#     if typ == "number":
#         return ("number", float(expr[1]))
#     elif typ == "string":
#         return ("string", expr[1])
#     elif typ == "none":
#         return ("none",)
#     elif typ == "operation":
#         return _operation(expr, env)
#     elif typ == "symbol":
#         name = expr[1]
#         ret = env.get(name)
#         if ret is None:
#             raise Exception("Unknown symbol '%s'." % name)
#         else:
#             return ret
#     elif typ == "assignment":
#         var_name = expr[1][1]
#         if var_name in env.items:
#             raise Exception("Not allowed to re-assign symbol '%s'." % var_name)
#         val = eval_expr(expr[2], env)
#         env.set(var_name, val)
#         return val
#     elif typ == "call":
#         return _function_call(expr, env)
#     elif typ == "function":
#         return ("function", expr[1], expr[2], Env(env))
#     else:
#         raise Exception("Unknown expression type: " + str(expr))
# 
# def eval_list(exprs, env):
#     ret = ("none",)
#     for expr in eval_iter(exprs, env):
#         ret = expr
#     return ret
# 
# Path: pycell/env.py
# class Env:
#     def __init__(self, parent=None, stdin=None, stdout=None, stderr=None):
#         """
#         Supply _either_ std{in,out,err} _or_ parent, not both.
#         """
#         self.stdin = stdin
#         self.stdout = stdout
#         self.stderr = stderr
#         self.parent = parent
#         if parent is not None:
#             assert stdin is None
#             assert stdout is None
#             assert stderr is None
#             self.stdin = parent.stdin
#             self.stdout = parent.stdout
#             self.stderr = parent.stderr
#         self.items = {}
# 
#     def get(self, name):
#         if name in self.items:
#             return self.items[name]
#         elif self.parent is not None:
#             return self.parent.get(name)
#         else:
#             return None
# 
#     def set(self, name, value):
#         self.items[name] = value
# 
#     def contains(self, name):
#         return name in self.items
# 
#     def __str__(self):
#         ret = ""
#         for k, v in self.items.items():
#             ret += "%s=%s\n" % (k, v)
#         ret += ".\n" + str(self.parent)
#         return ret


from tests.util.asserts import assert_that, assert_fails, equals
from tests.util.test import test
from pycell.lexer import lex
from pycell.parser import parse
from pycell.eval_ import eval_expr, eval_list
from pycell.env import Env
    return eval_list(parse(lex(inp)), env)


def assert_prog_fails(program, error, env=None):
    assert_fails(error, evald, program, env)


# --- Evaluating ---


@test
def Evaluating_an_empty_program_gives_none():
    assert_that(evald(""), equals(("none",)))


@test
def Evaluating_a_primitive_returns_itself():
    assert_that(evald("3;"), equals(("number", 3)))
    assert_that(evald("3.1;"), equals(("number", 3.1)))
    assert_that(evald("'foo';"), equals(("string", "foo")))


@test
def Arithmetic_expressions_come_out_correct():
    assert_that(evald("3 + 4;"), equals(("number", 7)))
    assert_that(evald("3 - 4;"), equals(("number", -1)))
    assert_that(evald("3 * 4;"), equals(("number", 12)))
    assert_that(evald("3 / 4;"), equals(("number", 0.75)))



# Path: tests/util/asserts.py
# def assert_that(obj, matcher):
#     assert_implements(obj, object)
#     assert_implements(matcher, Matcher)
#     if not matcher.matches(obj):
#         raise AssertionError(matcher.description())
# 
# def assert_fails(error, fn, *args):
#     try:
#         fn(*args)
#         fail("Should throw")
#     except Exception as e:
#         assert_that(str(e), equals(error))
# 
# def equals(expected):
#     class EqualsMatcher:
# 
#         def __init__(self, expected):
#             self.expected = expected
# 
#         def matches(self, actual):
#             self.actual = actual
#             return actual == expected
# 
#         def description(self):
#             ret = (
#                 repr(self.actual)
#                 + " does not equal "
#                 + repr(self.expected)
#                 + " as expected."
#             )
#             if len(ret) > 80:
#                 ret = """Values are not equal (actual then expected):
# %s
# %s
# """ % ( repr(self.actual), repr(self.expected))
#             return ret
#     return EqualsMatcher(expected)
# 
# Path: tests/util/test.py
# def test(t):
#     return TestFunctionType(t)
# 
# Path: pycell/lexer.py
# def lex(chars_iter):
#     chars = PeekableStream(chars_iter)
#     while chars.next is not None:
#         c = chars.move_next()
#         if c in " \n":              pass           # Ignore white space
#         elif c in "(){},;=:":       yield (c, "")  # Special characters
#         elif c in "+-*/":           yield ("operation", c)
#         elif c in ("'", '"'):       yield ("string", _scan_string(c, chars))
#         elif re.match("[.0-9]", c): yield ("number", _scan(c, chars, "[.0-9]"))
#         elif re.match("[_a-zA-Z]", c):
#             yield ("symbol", _scan(c, chars, "[_a-zA-Z0-9]"))
#         elif c == "\t":
#             raise Exception("Tab characters are not allowed in Cell.")
#         else:
#             raise Exception("Unrecognised character: '" + c + "'.")
# 
# Path: pycell/parser.py
# def parse(tokens_iterator):
#     parser = Parser(PeekableStream(tokens_iterator), ";")
#     while parser.tokens.next is not None:
#         p = parser.next_expression(None)
#         if p is not None:
#             yield p
#         parser.tokens.move_next()
# 
# Path: pycell/eval_.py
# def eval_expr(expr, env):
#     typ = expr[0]
#     if typ == "number":
#         return ("number", float(expr[1]))
#     elif typ == "string":
#         return ("string", expr[1])
#     elif typ == "none":
#         return ("none",)
#     elif typ == "operation":
#         return _operation(expr, env)
#     elif typ == "symbol":
#         name = expr[1]
#         ret = env.get(name)
#         if ret is None:
#             raise Exception("Unknown symbol '%s'." % name)
#         else:
#             return ret
#     elif typ == "assignment":
#         var_name = expr[1][1]
#         if var_name in env.items:
#             raise Exception("Not allowed to re-assign symbol '%s'." % var_name)
#         val = eval_expr(expr[2], env)
#         env.set(var_name, val)
#         return val
#     elif typ == "call":
#         return _function_call(expr, env)
#     elif typ == "function":
#         return ("function", expr[1], expr[2], Env(env))
#     else:
#         raise Exception("Unknown expression type: " + str(expr))
# 
# def eval_list(exprs, env):
#     ret = ("none",)
#     for expr in eval_iter(exprs, env):
#         ret = expr
#     return ret
# 
# Path: pycell/env.py
# class Env:
#     def __init__(self, parent=None, stdin=None, stdout=None, stderr=None):
#         """
#         Supply _either_ std{in,out,err} _or_ parent, not both.
#         """
#         self.stdin = stdin
#         self.stdout = stdout
#         self.stderr = stderr
#         self.parent = parent
#         if parent is not None:
#             assert stdin is None
#             assert stdout is None
#             assert stderr is None
#             self.stdin = parent.stdin
#             self.stdout = parent.stdout
#             self.stderr = parent.stderr
#         self.items = {}
# 
#     def get(self, name):
#         if name in self.items:
#             return self.items[name]
#         elif self.parent is not None:
#             return self.parent.get(name)
#         else:
#             return None
# 
#     def set(self, name, value):
#         self.items[name] = value
# 
#     def contains(self, name):
#         return name in self.items
# 
#     def __str__(self):
#         ret = ""
#         for k, v in self.items.items():
#             ret += "%s=%s\n" % (k, v)
#         ret += ".\n" + str(self.parent)
#         return ret


# Path: tests/eval_tests.py
from tests.util.asserts import assert_that, assert_fails, equals
from tests.util.test import test
from pycell.lexer import lex
from pycell.parser import parse
from pycell.eval_ import eval_expr, eval_list
from pycell.env import Env

    return eval_list(parse(lex(inp)), env)


def assert_prog_fails(program, error, env=None):
    assert_fails(error, evald, program, env)


# --- Evaluating ---


@test
def Evaluating_an_empty_program_gives_none():
    assert_that(evald(""), equals(("none",)))


@test
def Evaluating_a_primitive_returns_itself():
    assert_that(evald("3;"), equals(("number", 3)))
    assert_that(evald("3.1;"), equals(("number", 3.1)))
    assert_that(evald("'foo';"), equals(("string", "foo")))


@test
def Arithmetic_expressions_come_out_correct():
    assert_that(evald("3 + 4;"), equals(("number", 7)))
    assert_that(evald("3 - 4;"), equals(("number", -1)))
    assert_that(evald("3 * 4;"), equals(("number", 12)))
    assert_that(evald("3 / 4;"), equals(("number", 0.75)))



@test