# Path: cjktools/resources/split_by_codes.py
# def load_coded_dictionary(file_or_stream):
#     """ Load a dictionary which is split by the codes within it. """
#     if isinstance(file_or_stream, (six.text_type, six.string_types)):
#         file_or_stream = open(file_or_stream)
#     else:
#         file_or_stream = NullContextWrapper(file_or_stream)
# 
#     with file_or_stream as file_or_stream:
#         word_to_entry = {}
#         coded_dict = defaultdict(dict)
#         for entry in iter_entries(file_or_stream):
#             word = entry.word
# 
#             # Merge any word entries with the same graphical form.
#             if word in word_to_entry:
#                 e = word_to_entry[word]
#                 e.update(entry)
#                 entry = e
#             else:
#                 word_to_entry[word] = entry
# 
#             codes = set()
#             for sense in entry.senses:
#                 codes.update(get_codes(sense))
# 
#             # Create a subset of the dictionary for each code.
#             for code in codes:
#                 # Note that overwriting is ok, since we already merged any
#                 # coincidental forms.
#                 coded_dict[code][word] = entry
# 
#     return coded_dict
# 
# Path: cjktools/tests/_common.py
# def to_unicode_stream(x):
#     o = StringIO(x)
# 
#     if isinstance(x, binary_type):
#         o = codecs.getreader('utf8')(o)
# 
#     return o


import unittest
from cjktools.resources.split_by_codes import load_coded_dictionary
from .._common import to_unicode_stream
    test_suite = unittest.TestSuite((
        unittest.makeSuite(SplitByCodesTestCase)
    ))
    return test_suite


ENAMDICT_SAMPLE = \
"""？？？？ /ENAMDICT - Japanese Proper Name Dictionary File/Copyright Electronic Dictionary Research & Development Group, Monash University, 2007/Created: 2011-07-03/
秋葉橋 [あきはばし] /Akihabashi (p)/
秋葉橋 [あきばばし] /Akibabashi (p)/
"""  # nopep8

EDICT_SAMPLE = \
"""　？？？ /EDICT, EDICT_SUB(P), EDICT2 Japanese-English Electronic Dictionary Files/Copyright Electronic Dictionary Research & Development Group - 2011/Created: 2011-07-03/
素晴らしい [すばらしい] /(adj-i) wonderful/splendid/magnificent/(P)/
素晴らしい [すんばらしい] /(adj-i) wonderful/splendid/magnificent/
"""  # nopep8


class SplitByCodesTestCase(unittest.TestCase):
    def test_enamdict(self):
        "Checks code splitting for enamdict."
        enamdic_coded = load_coded_dictionary(
            to_unicode_stream(ENAMDICT_SAMPLE)
        )
        self.assertIn('p', enamdic_coded)
        self.assertIn('秋葉橋', enamdic_coded['p'])
        self.assertNotIn('秋葉橋', enamdic_coded['s'])

    def test_edict(self):

# Path: cjktools/resources/split_by_codes.py
# def load_coded_dictionary(file_or_stream):
#     """ Load a dictionary which is split by the codes within it. """
#     if isinstance(file_or_stream, (six.text_type, six.string_types)):
#         file_or_stream = open(file_or_stream)
#     else:
#         file_or_stream = NullContextWrapper(file_or_stream)
# 
#     with file_or_stream as file_or_stream:
#         word_to_entry = {}
#         coded_dict = defaultdict(dict)
#         for entry in iter_entries(file_or_stream):
#             word = entry.word
# 
#             # Merge any word entries with the same graphical form.
#             if word in word_to_entry:
#                 e = word_to_entry[word]
#                 e.update(entry)
#                 entry = e
#             else:
#                 word_to_entry[word] = entry
# 
#             codes = set()
#             for sense in entry.senses:
#                 codes.update(get_codes(sense))
# 
#             # Create a subset of the dictionary for each code.
#             for code in codes:
#                 # Note that overwriting is ok, since we already merged any
#                 # coincidental forms.
#                 coded_dict[code][word] = entry
# 
#     return coded_dict
# 
# Path: cjktools/tests/_common.py
# def to_unicode_stream(x):
#     o = StringIO(x)
# 
#     if isinstance(x, binary_type):
#         o = codecs.getreader('utf8')(o)
# 
#     return o


# Path: cjktools/tests/resources/test_split_by_codes.py
import unittest
from cjktools.resources.split_by_codes import load_coded_dictionary
from .._common import to_unicode_stream

    test_suite = unittest.TestSuite((
        unittest.makeSuite(SplitByCodesTestCase)
    ))
    return test_suite


ENAMDICT_SAMPLE = \
"""？？？？ /ENAMDICT - Japanese Proper Name Dictionary File/Copyright Electronic Dictionary Research & Development Group, Monash University, 2007/Created: 2011-07-03/
秋葉橋 [あきはばし] /Akihabashi (p)/
秋葉橋 [あきばばし] /Akibabashi (p)/
"""  # nopep8

EDICT_SAMPLE = \
"""　？？？ /EDICT, EDICT_SUB(P), EDICT2 Japanese-English Electronic Dictionary Files/Copyright Electronic Dictionary Research & Development Group - 2011/Created: 2011-07-03/
素晴らしい [すばらしい] /(adj-i) wonderful/splendid/magnificent/(P)/
素晴らしい [すんばらしい] /(adj-i) wonderful/splendid/magnificent/
"""  # nopep8


class SplitByCodesTestCase(unittest.TestCase):
    def test_enamdict(self):
        "Checks code splitting for enamdict."
        enamdic_coded = load_coded_dictionary(
            to_unicode_stream(ENAMDICT_SAMPLE)
        )
        self.assertIn('p', enamdic_coded)
        self.assertIn('秋葉橋', enamdic_coded['p'])
        self.assertNotIn('秋葉橋', enamdic_coded['s'])

    def test_edict(self):

        edict_coded = load_coded_dictionary(