# 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 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: 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


from threading import Thread
from tests.util.asserts import assert_that, equals
from tests.util.test import test
from tests.util.pipe import Pipe
    assert_that(pipe.read(3), equals("abc"))
    assert_that(pipe.read(2), equals("de"))


@test
def Can_read_in_one_thread_and_write_in_another():
    pipe = Pipe()

    def writer(p):
        for i in range(1):
            p.write(str(i))
            p.write("\n")
        p.close()
    thread1 = Thread(target=writer, args=(pipe,))
    thread1.start()
    for i in range(1):
        x = pipe.read(len(str(i))+1)
        assert_that(x, equals(str(i)+"\n"))
    thread1.join()


@test
def Readline_reads_up_to_a_newline():
    pipe = Pipe()
    pipe.write("ab")
    pipe.write("c\nd")
    pipe.write("e")
    pipe.write("\nghi")
    pipe.close()
    assert_that(pipe.readline(), equals("abc\n"))

# 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 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: 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: tests/util/pipe_tests.py
from threading import Thread
from tests.util.asserts import assert_that, equals
from tests.util.test import test
from tests.util.pipe import Pipe

    assert_that(pipe.read(3), equals("abc"))
    assert_that(pipe.read(2), equals("de"))


@test
def Can_read_in_one_thread_and_write_in_another():
    pipe = Pipe()

    def writer(p):
        for i in range(1):
            p.write(str(i))
            p.write("\n")
        p.close()
    thread1 = Thread(target=writer, args=(pipe,))
    thread1.start()
    for i in range(1):
        x = pipe.read(len(str(i))+1)
        assert_that(x, equals(str(i)+"\n"))
    thread1.join()


@test
def Readline_reads_up_to_a_newline():
    pipe = Pipe()
    pipe.write("ab")
    pipe.write("c\nd")
    pipe.write("e")
    pipe.write("\nghi")
    pipe.close()
    assert_that(pipe.readline(), equals("abc\n"))

    assert_that(pipe.readline(), equals("de\n"))