# Path: cjktools/resources/radkdict.py
# class RadkDict(dict):
#     """
#     Determines which radicals a character contains.
# 
#     :param istream:
#         The radkfile to parse.
#     """
# 
#     def __init__(self, istream=None):
#         """
# 
#         """
# 
#         with get_stream_context(_default_stream, istream) as istream:
#             self._parse_radkfile(stream_codec(istream))
# 
#     def _parse_radkfile(self, line_stream):
#         """
#         Parses the radkfile and populates the current dictionary.
# 
#         :param line_stream:
#             A stream yielding the  lines in the radkfile to parse.
#         """
#         radical_to_kanji = {}
#         radical_to_stroke_count = {}
# 
#         current_radical = None
#         stroke_count = None
# 
#         for line in line_stream:
#             line = line.rstrip()
#             if line.startswith('#'):
#                 # found a comment line
#                 continue
# 
#             if line.startswith('$'):
#                 # found a line with a new radical
#                 parts = line.split()
#                 if len(parts) not in (3, 4):
#                     raise Exception('format error parsing radkfile')
# 
#                 dollar, current_radical, stroke_count = parts[:3]
#                 radical_to_stroke_count[current_radical] = int(stroke_count)
#                 continue
# 
#             # found a line of kanji
#             kanji = line.strip()
#             radical_to_kanji.setdefault(current_radical, []).extend(kanji)
# 
#         self.update(maps.invert_mapping(radical_to_kanji))
#         maps.map_dict(tuple, self, in_place=True)
# 
#         self.radical_to_stroke_count = radical_to_stroke_count
#         self.radical_to_kanji = radical_to_kanji
# 
#     @classmethod
#     def get_cached(cls):
#         """ Returns a memory-cached class instance. """
#         cached = getattr(cls, '_cached', cls())
#         cls._cached = cached
# 
#         return cls._cached


import unittest
from six.moves import StringIO
from cjktools.resources.radkdict import RadkDict
"""
$ 一 1
偏
$ ｜ 1
偏
$ 化 2 js01
偏
$ 冂 2
偏
$ 尸 3
偏
$ 戸 4
偏
$ 冊 5
偏
"""  # nopep8


class RadkdictTestCase(unittest.TestCase):
    def test_fetch_radicals(self):
        key = u'偏'
        rkd = RadkDict(StringIO(SAMPLE))
        radicals = set(rkd[key])
        expected_radicals = set([u'一', u'｜', u'化', u'冂', u'尸', u'戸',
                                 u'冊'])
        self.assertEqual(radicals, expected_radicals)

    def test_get_cached(self):
        rd = RadkDict.get_cached()


# Path: cjktools/resources/radkdict.py
# class RadkDict(dict):
#     """
#     Determines which radicals a character contains.
# 
#     :param istream:
#         The radkfile to parse.
#     """
# 
#     def __init__(self, istream=None):
#         """
# 
#         """
# 
#         with get_stream_context(_default_stream, istream) as istream:
#             self._parse_radkfile(stream_codec(istream))
# 
#     def _parse_radkfile(self, line_stream):
#         """
#         Parses the radkfile and populates the current dictionary.
# 
#         :param line_stream:
#             A stream yielding the  lines in the radkfile to parse.
#         """
#         radical_to_kanji = {}
#         radical_to_stroke_count = {}
# 
#         current_radical = None
#         stroke_count = None
# 
#         for line in line_stream:
#             line = line.rstrip()
#             if line.startswith('#'):
#                 # found a comment line
#                 continue
# 
#             if line.startswith('$'):
#                 # found a line with a new radical
#                 parts = line.split()
#                 if len(parts) not in (3, 4):
#                     raise Exception('format error parsing radkfile')
# 
#                 dollar, current_radical, stroke_count = parts[:3]
#                 radical_to_stroke_count[current_radical] = int(stroke_count)
#                 continue
# 
#             # found a line of kanji
#             kanji = line.strip()
#             radical_to_kanji.setdefault(current_radical, []).extend(kanji)
# 
#         self.update(maps.invert_mapping(radical_to_kanji))
#         maps.map_dict(tuple, self, in_place=True)
# 
#         self.radical_to_stroke_count = radical_to_stroke_count
#         self.radical_to_kanji = radical_to_kanji
# 
#     @classmethod
#     def get_cached(cls):
#         """ Returns a memory-cached class instance. """
#         cached = getattr(cls, '_cached', cls())
#         cls._cached = cached
# 
#         return cls._cached


# Path: cjktools/tests/resources/test_radkdict.py
import unittest
from six.moves import StringIO
from cjktools.resources.radkdict import RadkDict

"""
$ 一 1
偏
$ ｜ 1
偏
$ 化 2 js01
偏
$ 冂 2
偏
$ 尸 3
偏
$ 戸 4
偏
$ 冊 5
偏
"""  # nopep8


class RadkdictTestCase(unittest.TestCase):
    def test_fetch_radicals(self):
        key = u'偏'
        rkd = RadkDict(StringIO(SAMPLE))
        radicals = set(rkd[key])
        expected_radicals = set([u'一', u'｜', u'化', u'冂', u'尸', u'戸',
                                 u'冊'])
        self.assertEqual(radicals, expected_radicals)

    def test_get_cached(self):
        rd = RadkDict.get_cached()


        self.assertIsInstance(rd, RadkDict)