# Path: distance/_nonlevel.py
# class Leaderboard(BaseObject):
# 
#     type = 'LocalLeaderboard'
# 
# Path: distance/printing.py
# class PrintContext(object):
# 
#     """Context class for printing objects."""
# 
#     def __init__(self, file=sys.stdout, flags=()):
#         self.file = file
#         self.flags = flags
#         # Data for each level of tree_children():
#         # 0. Buffered lines for that level.
#         # 1. Whether the child on that level has been ended
#         #    by a call to tree_next_child().
#         # 2. Counter of remaining children if 'count' was passed to
#         #    tree_children() on that level.
#         self._tree_data = [], [], []
# 
#     @classmethod
#     def for_test(cls, file=None, flags=None):
#         if flags is None:
#             class ContainsEverything:
#                 def __contains__(self, obj):
#                     return True
#             flags = ContainsEverything()
#         p = cls(file=file, flags=flags)
#         def print_exc(e):
#             raise e
#         p.print_exception = print_exc
#         return p
# 
#     def __call__(self, text):
#         buf, ended, remain = self._tree_data
#         if buf:
#             count = remain[-1]
#             if count is not None:
#                 self._tree_push_up(len(buf) - 1, [text], count <= 1)
#             else:
#                 lines = buf[-1]
#                 if ended[-1]:
#                     self._tree_push_up(len(buf) - 1, lines, False)
#                     lines.clear()
#                 lines.extend(text.split('\n'))
#         else:
#             f = self.file
#             if f is not None:
#                 print(text, file=f)
# 
#     def _tree_push_up(self, level, lines, last):
#         while True:
#             if not lines:
#                 return
#             buf, ended, remain = self._tree_data
#             if level < 0:
#                 raise IndexError
#             was_ended = ended[level]
#             ended[level] = False
#             if level > 0:
#                 upbuffer = buf[level - 1]
#                 push_line = upbuffer.append
#             else:
#                 f = self.file
#                 def push_line(line):
#                     if f is not None:
#                         print(line, file=f)
#             it = iter(lines)
#             if was_ended:
#                 if last:
#                     prefix = "└─ "
#                 else:
#                     prefix = "├─ "
#                 push_line(prefix + next(it))
#             if last:
#                 prefix = "   "
#             else:
#                 prefix = "│  "
#             for line in it:
#                 push_line(prefix + line)
# 
#             if remain is not None:
#                 lines.clear()
#             if level > 0 and remain[level - 1] is not None:
#                 # In unbuffered mode (with 'count' passed to tree_children)
#                 # we iterate up to the root and print everything immediately.
#                 level -= 1
#                 lines = upbuffer
#                 last = remain[level] <= 1
#             else:
#                 return
# 
#     @contextmanager
#     def tree_children(self, count=None):
#         buf, ended, remain = self._tree_data
#         level = len(buf)
#         if remain and remain[level - 1] is None:
#             # We are nested inside a tree_children() without count. Cannot
#             # use unbufferd printing.
#             count = None
#         lines = []
#         buf.append(lines)
#         # When unbuffered, we start with ended state, so we get our tree
#         # printed on the first nested line.
#         ended.append(count is not None)
#         remain.append(count)
#         broken = False
#         try:
#             yield
#         except BrokenPipeError:
#             broken = True
#             raise
#         finally:
#             ended[level] = True
#             if not broken and count is None:
#                 self._tree_push_up(level, lines, True)
#             buf.pop()
#             ended.pop()
#             remain.pop()
# 
#     def tree_next_child(self):
#         buf, ended, remain = self._tree_data
#         if buf:
#             count = remain[-1]
#             if count is not None:
#                 # We don't count down if nothing was printed.
#                 # For our tree to look correct, classes need to make sure
#                 # they print at least once for each child anyways.
#                 if not ended[-1]:
#                     remain[-1] = count - 1
#                     ended[-1] = True
#             elif buf[-1]:
#                 ended[-1] = True
# 
#     def print_object(self, obj):
#         obj.print(p=self)
# 
#     def print_exception(self, exc):
#         exc_str = traceback.format_exception(type(exc), exc, exc.__traceback__)
#         for part in exc_str:
#             if part.endswith('\n'):
#                 part = part[:-1]
#             for line in part.split('\n'):
#                 self(line)
#         try:
#             self(f"Exception start: 0x{exc.start_pos:08x}")
#             self(f"Exception pos:   0x{exc.exc_pos:08x}")
#         except AttributeError:
#             pass
# 
# Path: tests/common.py
# def check_exceptions(obj):
#     if obj.exception:
#         raise obj.exception
#     if isinstance(obj, Level):
#         check_exceptions(obj.settings)
#         for layer in obj.layers:
#             check_exceptions(layer)
#     if isinstance(obj, BaseObject):
#         for frag in obj.fragments:
#             check_exceptions(frag)
#         for child in obj.children:
#             check_exceptions(child)
#     if isinstance(obj, Layer):
#         for obj in obj.objects:
#             check_exceptions(obj)
# 
# def write_read(obj, read_func=None, do_check_exceptions=True):
#     if read_func is None:
#         read_func = type(obj)
# 
#     dbytes = DstBytes.in_memory()
# 
#     obj.write(dbytes)
#     dbytes.seek(0)
#     disable_writes(dbytes)
#     result = read_func(dbytes)
# 
#     if do_check_exceptions:
#         check_exceptions(result)
# 
#     return result, dbytes.file


import unittest
from distance import Leaderboard
from distance.printing import PrintContext
from .common import check_exceptions, write_read



class Version0Test(unittest.TestCase):

    def test_version0(self):
        lb = Leaderboard("tests/in/leaderboard/version_0.bytes")
        entries = lb.entries

# Path: distance/_nonlevel.py
# class Leaderboard(BaseObject):
# 
#     type = 'LocalLeaderboard'
# 
# Path: distance/printing.py
# class PrintContext(object):
# 
#     """Context class for printing objects."""
# 
#     def __init__(self, file=sys.stdout, flags=()):
#         self.file = file
#         self.flags = flags
#         # Data for each level of tree_children():
#         # 0. Buffered lines for that level.
#         # 1. Whether the child on that level has been ended
#         #    by a call to tree_next_child().
#         # 2. Counter of remaining children if 'count' was passed to
#         #    tree_children() on that level.
#         self._tree_data = [], [], []
# 
#     @classmethod
#     def for_test(cls, file=None, flags=None):
#         if flags is None:
#             class ContainsEverything:
#                 def __contains__(self, obj):
#                     return True
#             flags = ContainsEverything()
#         p = cls(file=file, flags=flags)
#         def print_exc(e):
#             raise e
#         p.print_exception = print_exc
#         return p
# 
#     def __call__(self, text):
#         buf, ended, remain = self._tree_data
#         if buf:
#             count = remain[-1]
#             if count is not None:
#                 self._tree_push_up(len(buf) - 1, [text], count <= 1)
#             else:
#                 lines = buf[-1]
#                 if ended[-1]:
#                     self._tree_push_up(len(buf) - 1, lines, False)
#                     lines.clear()
#                 lines.extend(text.split('\n'))
#         else:
#             f = self.file
#             if f is not None:
#                 print(text, file=f)
# 
#     def _tree_push_up(self, level, lines, last):
#         while True:
#             if not lines:
#                 return
#             buf, ended, remain = self._tree_data
#             if level < 0:
#                 raise IndexError
#             was_ended = ended[level]
#             ended[level] = False
#             if level > 0:
#                 upbuffer = buf[level - 1]
#                 push_line = upbuffer.append
#             else:
#                 f = self.file
#                 def push_line(line):
#                     if f is not None:
#                         print(line, file=f)
#             it = iter(lines)
#             if was_ended:
#                 if last:
#                     prefix = "└─ "
#                 else:
#                     prefix = "├─ "
#                 push_line(prefix + next(it))
#             if last:
#                 prefix = "   "
#             else:
#                 prefix = "│  "
#             for line in it:
#                 push_line(prefix + line)
# 
#             if remain is not None:
#                 lines.clear()
#             if level > 0 and remain[level - 1] is not None:
#                 # In unbuffered mode (with 'count' passed to tree_children)
#                 # we iterate up to the root and print everything immediately.
#                 level -= 1
#                 lines = upbuffer
#                 last = remain[level] <= 1
#             else:
#                 return
# 
#     @contextmanager
#     def tree_children(self, count=None):
#         buf, ended, remain = self._tree_data
#         level = len(buf)
#         if remain and remain[level - 1] is None:
#             # We are nested inside a tree_children() without count. Cannot
#             # use unbufferd printing.
#             count = None
#         lines = []
#         buf.append(lines)
#         # When unbuffered, we start with ended state, so we get our tree
#         # printed on the first nested line.
#         ended.append(count is not None)
#         remain.append(count)
#         broken = False
#         try:
#             yield
#         except BrokenPipeError:
#             broken = True
#             raise
#         finally:
#             ended[level] = True
#             if not broken and count is None:
#                 self._tree_push_up(level, lines, True)
#             buf.pop()
#             ended.pop()
#             remain.pop()
# 
#     def tree_next_child(self):
#         buf, ended, remain = self._tree_data
#         if buf:
#             count = remain[-1]
#             if count is not None:
#                 # We don't count down if nothing was printed.
#                 # For our tree to look correct, classes need to make sure
#                 # they print at least once for each child anyways.
#                 if not ended[-1]:
#                     remain[-1] = count - 1
#                     ended[-1] = True
#             elif buf[-1]:
#                 ended[-1] = True
# 
#     def print_object(self, obj):
#         obj.print(p=self)
# 
#     def print_exception(self, exc):
#         exc_str = traceback.format_exception(type(exc), exc, exc.__traceback__)
#         for part in exc_str:
#             if part.endswith('\n'):
#                 part = part[:-1]
#             for line in part.split('\n'):
#                 self(line)
#         try:
#             self(f"Exception start: 0x{exc.start_pos:08x}")
#             self(f"Exception pos:   0x{exc.exc_pos:08x}")
#         except AttributeError:
#             pass
# 
# Path: tests/common.py
# def check_exceptions(obj):
#     if obj.exception:
#         raise obj.exception
#     if isinstance(obj, Level):
#         check_exceptions(obj.settings)
#         for layer in obj.layers:
#             check_exceptions(layer)
#     if isinstance(obj, BaseObject):
#         for frag in obj.fragments:
#             check_exceptions(frag)
#         for child in obj.children:
#             check_exceptions(child)
#     if isinstance(obj, Layer):
#         for obj in obj.objects:
#             check_exceptions(obj)
# 
# def write_read(obj, read_func=None, do_check_exceptions=True):
#     if read_func is None:
#         read_func = type(obj)
# 
#     dbytes = DstBytes.in_memory()
# 
#     obj.write(dbytes)
#     dbytes.seek(0)
#     disable_writes(dbytes)
#     result = read_func(dbytes)
# 
#     if do_check_exceptions:
#         check_exceptions(result)
# 
#     return result, dbytes.file


# Path: tests/test_leaderboard.py
import unittest
from distance import Leaderboard
from distance.printing import PrintContext
from .common import check_exceptions, write_read




class Version0Test(unittest.TestCase):

    def test_version0(self):
        lb = Leaderboard("tests/in/leaderboard/version_0.bytes")
        entries = lb.entries

        check_exceptions(lb)