def test_add_variant(self): c = Char(0x002A) c.add_variant([0x0030]) self.assertEqual(len(c._variants), 1) self.assertIn((0x0030, ), c._variants) self.assertEqual(len(c._variants[(0x0030, )]), 1) self.assertEqual(c._variants[(0x0030, )][0].cp, (0x0030, )) self.assertRaises(VariantAlreadyExists, c.add_variant, [0x0030]) c.add_variant([0x0030], when='w1') self.assertRaises(VariantAlreadyExists, c.add_variant, [0x0030], when='w1') c.add_variant([0x0030], not_when='w1') self.assertRaises(VariantAlreadyExists, c.add_variant, [0x0030], not_when='w1') c.add_variant([0x0030], when='w1', not_when='nw-1') self.assertRaises(VariantAlreadyExists, c.add_variant, [0x0030], when='w1', not_when='nw-1') self.assertEqual(len(c._variants[(0x0030, )]), 4)
def test_iter(self): self.cd.add_char([0x0010]) self.cd.add_range(0x0001, 0x000A) self.cd.add_char([0x0000]) self.cd.add_char([0x0011, 0x0012]) self.cd.add_char([0x0013]) expected_output = [ Char(0x0000), RangeChar(0x0001, 0x0001, 0x000A), Char(0x0010), CharSequence([0x0011, 0x0012]), Char(0x0013), ] self.assertEqual(list(self.cd), expected_output)
def test_is_prefix_of(self): c = Char(0x002A) s = CharBase.from_cp_or_sequence([0x002A, 0x002B]) r = RangeChar(0x002A, 0x002A, 0x002B) self.assertTrue(c.is_prefix_of([0x002A])) self.assertTrue(r.is_prefix_of([0x002A])) self.assertTrue(c.is_prefix_of([0x002A, 0x002B])) self.assertTrue(r.is_prefix_of([0x002A, 0x002B])) self.assertTrue(s.is_prefix_of([0x002A, 0x002B])) self.assertTrue(s.is_prefix_of([0x002A, 0x002B, 0x002C])) self.assertFalse(c.is_prefix_of([0x002B])) self.assertFalse(r.is_prefix_of([0x002B])) self.assertFalse(s.is_prefix_of([0x002B, 0x002A])) self.assertFalse(s.is_prefix_of([0x002A])) self.assertFalse(s.is_prefix_of([0x002B]))
def test_as_index(self): self.assertEqual(Char(0x002A).as_index(), 0x002A) self.assertEqual(CharSequence((0x002A, 0x002B)).as_index(), 0x002A) self.assertEqual(RangeChar(0x002A, 0x002A, 0x02C).as_index(), 0x002A) self.assertEqual( CharBase.from_cp_or_sequence(0x002A).as_index(), 0x002A) self.assertEqual( CharBase.from_cp_or_sequence([0x002A]).as_index(), 0x002A) self.assertEqual( CharBase.from_cp_or_sequence([0x002A, 0x002B]).as_index(), 0x002A)
def test_add_codepoints(self): self.lgr.add_codepoints([c for c in range(0x0061, 0x007A + 1)] + [0x0107] + [0x0137, 0x0138]) expected_output = [ RangeChar(0x061, 0x0061, 0x007A), Char(0x0107), RangeChar(0x0137, 0x0137, 0x0138) ] self.assertEqual(expected_output, list(self.lgr.repertoire))
def test_get_variant(self): c = Char(0x002A) c.add_variant([0x0030], when='w1') c.add_variant([0x0031], when='w2') c.add_variant([0x0031], not_when='nw-1') variant = c.get_variant((0x031, )) expected_output = [ Variant((0x0031, ), when='w2'), Variant((0x0031, ), not_when='nw-1') ] self.assertEqual(variant, expected_output)
def test_len(self): self.assertEqual(len(Char(0x002A)), 1) self.assertEqual(len(CharBase.from_cp_or_sequence([0x002A, 0x002B])), 2) self.assertEqual(len(RangeChar(0x002A, 0x002A, 0x002B)), 1)
def test_equal_cp(self): self.assertEqual(Char([0x002A]), Char([0x002A])) self.assertNotEqual(Char([0x002A]), Char([0x002B])) c1 = Char(0x002A) c2 = Char(0x002A) self.assertEqual(c1, c2)
def test_get_variants(self): c = Char(0x002A) c.add_variant([0x0030]) c.add_variant([0x0030], when='w1') c.add_variant([0x0030], not_when='w1') c.add_variant([0x0030], when='w1', not_when='nw-1') variants = c.get_variants() self.assertIsInstance(variants, types.GeneratorType) variant_list = list(variants) self.assertEqual(len(variant_list), 4) expected_output = [ Variant((0x0030, )), Variant((0x0030, ), when='w1'), Variant((0x0030, ), not_when='w1'), Variant((0x0030, ), when='w1', not_when='nw-1') ] self.assertEqual(variant_list, expected_output)
def test_del_variant(self): c = Char(0x002A) c.add_variant([0x0030]) c.del_variant([0x0030]) self.assertEqual(len(c._variants), 0) c.add_variant([0x0030]) c.add_variant([0x0031]) c.del_variant([0x0031]) self.assertEqual(len(c._variants), 1) self.assertIn((0x0030, ), c._variants) self.assertEqual(len(c._variants[(0x0030, )]), 1) self.assertEqual(c._variants[(0x0030, )][0].cp, (0x0030, )) c.add_variant([0x0030], when='w1') c.del_variant([0x0030], when='w1') self.assertEqual(len(c._variants[(0x0030, )]), 1) self.assertEqual(c._variants[(0x0030, )][0].cp, (0x0030, )) self.assertIsNone(c._variants[(0x0030, )][0].when)
def test_char_range(self): char = Char(0x002A) range_char = RangeChar(0x002A, 0x002A, 0x002B) self.assertEqual(char, range_char) self.assertEqual(char.__hash__(), range_char.__hash__())
def test_hash_char(self): c1 = Char(0x002A) c2 = Char(0x002A) c3 = Char(0x002B) self.assertEqual(c1.__hash__(), c2.__hash__()) self.assertNotEqual(c1.__hash__(), c3.__hash__())
def test_all_repertoire(self): self.cd.add_char([0x0010]) self.cd.add_range(0x0001, 0x0005) self.cd.add_char([0x0000]) self.cd.add_char([0x0011, 0x0012]) self.cd.add_char([0x0013]) # Full output expected_output = { Char(0x0000), RangeChar(0x0001, 0x0001, 0x0005), RangeChar(0x0002, 0x0001, 0x0005), RangeChar(0x0003, 0x0001, 0x0005), RangeChar(0x0004, 0x0001, 0x0005), RangeChar(0x0005, 0x0001, 0x0005), Char(0x0010), CharSequence([0x0011, 0x0012]), Char(0x0013), } self.assertEqual(set(self.cd.all_repertoire()), expected_output) # Exclude ranges expected_output = { Char(0x0000), Char(0x0010), CharSequence([0x0011, 0x0012]), Char(0x0013), } self.assertEqual(set(self.cd.all_repertoire(include_ranges=False)), expected_output) # Exclude sequences expected_output = { Char(0x0000), RangeChar(0x0001, 0x0001, 0x0005), RangeChar(0x0002, 0x0001, 0x0005), RangeChar(0x0003, 0x0001, 0x0005), RangeChar(0x0004, 0x0001, 0x0005), RangeChar(0x0005, 0x0001, 0x0005), Char(0x0010), Char(0x0013), } self.assertEqual(set(self.cd.all_repertoire(include_sequences=False)), expected_output) # Exclude ranges and sequences expected_output = { Char(0x0000), Char(0x0010), Char(0x0013), } self.assertEqual( set( self.cd.all_repertoire(include_ranges=False, include_sequences=False)), expected_output)