# Path: tests/util/pipe.py
# class Pipe:
#     """
#     In-memory file-like object that may be read and written by co-operating
#     threads.
#     Note: no readline() etc. is provided - only read and write.
#     Note: read() will block waiting for more to be read, until close() is
#     called.
#     """
#     def __init__(self):
#         self.buffer = Queue()
#         self.closed = False
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         self.close()
# 
#     def write(self, s):
#         for ch in s:
#             self.buffer.put(ch)
# 
#     def pending(self):
#         return not self.buffer.empty()
# 
#     def read(self, size=None):
#         ret = ""
#         count = 0
#         while size is None or len(ret) < size:
#             if self.buffer.empty() and self.closed:
#                 break
#             if count > 50:
#                 raise Exception("Timed out waiting for read that never ended.")
#             count += 1
#             ret += self.buffer.get(timeout=0.1)
#         return ret
# 
#     def readline(self):
#         ret = ""
#         count = 0
#         c = "x"
#         while c not in ("\n", ""):
#             if self.buffer.empty() and self.closed:
#                 break
#             if count > 50:
#                 raise Exception("Timed out waiting for read that never ended.")
#             count += 1
#             c = self.buffer.get(timeout=0.1)
#             ret += c
#         return ret
# 
#     def flush(self):
#         pass
# 
#     def close(self):
#         self.closed = True
# 
# Path: pycell/repl.py
# def repl(stdin, stdout, stderr):
#     env = Env(parent=None, stdin=stdin, stdout=stdout, stderr=stderr)
#     pycell.library.import_(env)
#     while True:
#         try:
#             p = Prompt(stdout)
#             for value in eval_iter(
#                     parse(lex(p.handle_chars(chars_in_file(stdin)))), env):
#                 p.value(value)
#             break
#         except Exception as e:
#             stderr.write(str(e))
#             stderr.write("\n")
#     stdout.write("\n")
#     stdout.flush()
# 
# Path: tests/util/system_test.py

# 
# Path: tests/util/all_examples.py
# def all_sessions():
#     return all_examples(".cellsession")


from tests.util.pipe import Pipe
from threading import Thread
from pycell.repl import repl
from tests.util.system_test import system_test
from tests.util.all_examples import all_sessions





def _validate_line(exp_line, strin, strout, strerr):
    expprompt = exp_line[:3]
    if expprompt in (">>>", "..."):
        prompt = strout.read(4)
        if prompt != exp_line[:4]:
            raise Exception(
                "Prompt was '%s' but we expected '%s'." % (
                    prompt, exp_line[:4])
            )
        strin.write(exp_line[4:])
    else:
        output = strout.readline()
        if output != exp_line:
            raise Exception("Output was '%s' but we expected '%s'" % (
                output, exp_line)
            )


def _validate_session(f):

# Path: tests/util/pipe.py
# class Pipe:
#     """
#     In-memory file-like object that may be read and written by co-operating
#     threads.
#     Note: no readline() etc. is provided - only read and write.
#     Note: read() will block waiting for more to be read, until close() is
#     called.
#     """
#     def __init__(self):
#         self.buffer = Queue()
#         self.closed = False
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         self.close()
# 
#     def write(self, s):
#         for ch in s:
#             self.buffer.put(ch)
# 
#     def pending(self):
#         return not self.buffer.empty()
# 
#     def read(self, size=None):
#         ret = ""
#         count = 0
#         while size is None or len(ret) < size:
#             if self.buffer.empty() and self.closed:
#                 break
#             if count > 50:
#                 raise Exception("Timed out waiting for read that never ended.")
#             count += 1
#             ret += self.buffer.get(timeout=0.1)
#         return ret
# 
#     def readline(self):
#         ret = ""
#         count = 0
#         c = "x"
#         while c not in ("\n", ""):
#             if self.buffer.empty() and self.closed:
#                 break
#             if count > 50:
#                 raise Exception("Timed out waiting for read that never ended.")
#             count += 1
#             c = self.buffer.get(timeout=0.1)
#             ret += c
#         return ret
# 
#     def flush(self):
#         pass
# 
#     def close(self):
#         self.closed = True
# 
# Path: pycell/repl.py
# def repl(stdin, stdout, stderr):
#     env = Env(parent=None, stdin=stdin, stdout=stdout, stderr=stderr)
#     pycell.library.import_(env)
#     while True:
#         try:
#             p = Prompt(stdout)
#             for value in eval_iter(
#                     parse(lex(p.handle_chars(chars_in_file(stdin)))), env):
#                 p.value(value)
#             break
#         except Exception as e:
#             stderr.write(str(e))
#             stderr.write("\n")
#     stdout.write("\n")
#     stdout.flush()
# 
# Path: tests/util/system_test.py

# 
# Path: tests/util/all_examples.py
# def all_sessions():
#     return all_examples(".cellsession")


# Path: tests/repl_tests.py
from tests.util.pipe import Pipe
from threading import Thread
from pycell.repl import repl
from tests.util.system_test import system_test
from tests.util.all_examples import all_sessions






def _validate_line(exp_line, strin, strout, strerr):
    expprompt = exp_line[:3]
    if expprompt in (">>>", "..."):
        prompt = strout.read(4)
        if prompt != exp_line[:4]:
            raise Exception(
                "Prompt was '%s' but we expected '%s'." % (
                    prompt, exp_line[:4])
            )
        strin.write(exp_line[4:])
    else:
        output = strout.readline()
        if output != exp_line:
            raise Exception("Output was '%s' but we expected '%s'" % (
                output, exp_line)
            )


def _validate_session(f):

    with Pipe() as strin, Pipe() as strout: