# Path: xclib/prosody_io.py
# class prosody_io:
#     @classmethod
#     def read_request(cls, infd, outfd):
#         # "for line in sys.stdin:" would be more concise but adds unwanted buffering
#         while True:
#             line = infd.readline()
#             if not line:
#                 break
#             line = line.rstrip("\r\n")
#             yield tuple(line.split(':', 3))
# 
#     @classmethod
#     def write_response(cls, flag, outfd):
#         if isinstance(flag, str):
#             # Hack for interactive 'roster' command used by tests/online_test.py
#             answer = flag
#         else:
#             answer = '0'
#             if flag:
#                 answer = '1'
#         outfd.write(answer+"\n")
#         outfd.flush()
# 
# Path: xclib/tests/iostub.py
# class iostub:
#     def stub_stdin(testcase_inst, inputs, ioclass=io.StringIO):
#         stdin = sys.stdin
# 
#         def cleanup():
#             sys.stdin = stdin
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdin = ioclass(inputs)
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdin.buffer = sys.stdin
# 
#     def stub_stdout(testcase_inst, ioclass=io.StringIO):
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
# 
#     def stub_stdouts(testcase_inst, ioclass=io.StringIO):
#         stderr = sys.stderr
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stderr = stderr
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stderr = ioclass()
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
#             sys.stderr.buffer = sys.stderr
# 
# Path: xclib/check.py
# def assertEqual(a, b):
#     if type(a) == type(b):
#         assertSimilar(a, b)
#     else:
#         raise AssertionError('Assertion failed: Type mismatch %r (%s) != %r (%s)' % (a, type(a), b, type(b)))


import sys
import unittest
from xclib.prosody_io import prosody_io
from xclib.tests.iostub import iostub
from xclib.check import assertEqual
# Checks that prosody_io works as designed

class TestProsody(unittest.TestCase, iostub):

    def test_input(self):
        self.stub_stdin('isuser:login:\n' +
            'auth:log:dom:pass\n')
        tester = iter(prosody_io.read_request(sys.stdin, sys.stdout))
        output = next(tester)
        assertEqual(output, ('isuser', 'login', ''))
        output = next(tester)
        assertEqual(output, ('auth', 'log', 'dom', 'pass'))
        self.assertRaises(StopIteration, next, tester)

    def test_output_false(self):
        self.stub_stdout()
        prosody_io.write_response(False, sys.stdout)
        self.assertEqual(sys.stdout.getvalue(), '0\n')

    # Cannot be merged, as getvalue() returns the aggregate value
    def test_output_true(self):
        self.stub_stdout()

# Path: xclib/prosody_io.py
# class prosody_io:
#     @classmethod
#     def read_request(cls, infd, outfd):
#         # "for line in sys.stdin:" would be more concise but adds unwanted buffering
#         while True:
#             line = infd.readline()
#             if not line:
#                 break
#             line = line.rstrip("\r\n")
#             yield tuple(line.split(':', 3))
# 
#     @classmethod
#     def write_response(cls, flag, outfd):
#         if isinstance(flag, str):
#             # Hack for interactive 'roster' command used by tests/online_test.py
#             answer = flag
#         else:
#             answer = '0'
#             if flag:
#                 answer = '1'
#         outfd.write(answer+"\n")
#         outfd.flush()
# 
# Path: xclib/tests/iostub.py
# class iostub:
#     def stub_stdin(testcase_inst, inputs, ioclass=io.StringIO):
#         stdin = sys.stdin
# 
#         def cleanup():
#             sys.stdin = stdin
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdin = ioclass(inputs)
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdin.buffer = sys.stdin
# 
#     def stub_stdout(testcase_inst, ioclass=io.StringIO):
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
# 
#     def stub_stdouts(testcase_inst, ioclass=io.StringIO):
#         stderr = sys.stderr
#         stdout = sys.stdout
# 
#         def cleanup():
#             sys.stderr = stderr
#             sys.stdout = stdout
# 
#         testcase_inst.addCleanup(cleanup)
#         sys.stderr = ioclass()
#         sys.stdout = ioclass()
#         if ioclass == io.BytesIO:
#             # Fake 'buffer' variable
#             sys.stdout.buffer = sys.stdout
#             sys.stderr.buffer = sys.stderr
# 
# Path: xclib/check.py
# def assertEqual(a, b):
#     if type(a) == type(b):
#         assertSimilar(a, b)
#     else:
#         raise AssertionError('Assertion failed: Type mismatch %r (%s) != %r (%s)' % (a, type(a), b, type(b)))


# Path: xclib/tests/20_prosody_test.py
import sys
import unittest
from xclib.prosody_io import prosody_io
from xclib.tests.iostub import iostub
from xclib.check import assertEqual

# Checks that prosody_io works as designed

class TestProsody(unittest.TestCase, iostub):

    def test_input(self):
        self.stub_stdin('isuser:login:\n' +
            'auth:log:dom:pass\n')
        tester = iter(prosody_io.read_request(sys.stdin, sys.stdout))
        output = next(tester)
        assertEqual(output, ('isuser', 'login', ''))
        output = next(tester)
        assertEqual(output, ('auth', 'log', 'dom', 'pass'))
        self.assertRaises(StopIteration, next, tester)

    def test_output_false(self):
        self.stub_stdout()
        prosody_io.write_response(False, sys.stdout)
        self.assertEqual(sys.stdout.getvalue(), '0\n')

    # Cannot be merged, as getvalue() returns the aggregate value
    def test_output_true(self):
        self.stub_stdout()

        prosody_io.write_response(True, sys.stdout)