adapt to new feature names.
[chise/ruby.git] / t / tc_char.rb
1 #!/usr/bin/env ruby
2 # by eto 2003-0112
3
4 require 'test/unit'
5 $LOAD_PATH << '../src'
6 require 'chise'
7 include CHISE
8
9 class TC_Character < Test::Unit::TestCase
10   def setup() @char = Character.get("字") end #UTF8で与えること
11   def test_char(char)
12 #    assert_equal(6, char.get_char_attribute('total_strokes'), "get total strokes by XEmacs UTF-2000 like method")
13     assert_equal(6, char['total_strokes'], "get total strokes by Hash like method") if char.is_a? Character
14     assert_equal(6, char.total_strokes, "get total strokes by method")
15     assert_equal(23383, char.ucs, "translate to ucs")
16     assert_equal(22358, char.gb2312, "get character code in chinese GB2312")
17     assert_equal(1777, char.shinjigen_2, "get shinjigen 2")
18     assert_equal(3, char.ideographic_strokes, "get")
19     assert_equal(39, char.ideographic_radical, "get")
20   end
21   def test_chars
22     test_char(Character.get("字"))
23     test_char(Character.new("字"))
24     test_char("字".char)
25     test_char("字")
26   end
27   def test_create
28     assert_equal(23383, Character.parse_char_id("字"))
29   end
30   def test_put_attributes
31     @char.put_char_attribute('test_attribute', 'test')
32     assert_equal('test', @char.get_char_attribute('test_attribute'), "put, get")
33     @char['test_attribute'] = 'test'
34     assert_equal('test', @char['test_attribute'], "[]=, []")
35   end
36   def test_method
37     assert_instance_of(Hash, @char.char_attribute_alist, "returns Hash")
38     assert_instance_of(Hash, @char.alist, ".alist returns Hash")
39     assert_instance_of(Array, @char.char_attribute_list, "returns Array")
40     assert_instance_of(Array, @char.list, ".list returns Array")
41     assert_instance_of(String, @char.inspect)
42   end
43   def test_er
44     assert_equal(Character.get("&J90-3B7A;"), @char, "jisx0208")
45 #    assert_equal("&J90-3B7A;", @char.to_er, "jisx0208")
46     assert_equal(Character.get("&MCS-00005B57;"), @char, "mcs")
47 #    assert_equal(Character.get("&M-06942;"), @char, "ideograph-daikanwa, Morohashi")
48   end
49   def test_latin
50     char = Character.get("A")
51     assert_equal(65, char.ascii, "ascii")
52     assert_equal(char.bidi_category, "L", "bidi")
53     assert_equal(char.name, "LATIN CAPITAL LETTER A", "name")
54     assert_equal(65, char.ucs, "ucs")
55     assert_equal(char.latin_jisx0201, 65, "jisx0201")
56     assert_equal(char.latin_viscii, 65, "viscii") #って何?
57 #->fullwidth: (((name . "FULLWIDTH LATIN CAPITAL LETTER A") (ucs . 65313)))
58 #->lowercase: (((name . "LATIN SMALL LETTER A") (ucs . 97)))
59 #general-category: (letter uppercase)
60 #このへんのS式の展開が必要なものは、また後程扱うべし。
61   end
62   def test_ids
63     char = Character.get("⿰")
64     assert_equal("IDEOGRAPHIC DESCRIPTION CHARACTER LEFT TO RIGHT", char.name, "ids name")
65     #assert_equal(char.to_er, "&U+2FF0;", "ids er")
66     assert_equal(char.to_er, "&#x2ff0;", "ids er")
67     assert_equal(char.bidi_category, "ON", "ids bidi")
68   end
69   def test_jis
70     char = Character.get("逢")
71     assert_instance_of(String, char.get_attributes)
72     char = Character.get("学")
73     assert_instance_of(String, char.get_attributes)
74   end
75   def test_flyweight
76     char1 = Character.new("字")
77     char2 = Character.new("字") #.newで生成した場合は別々のinstanceになるのだ
78     assert_equal(char1, char2) #==ではある
79     assert_not_same(char1, char2) #equal?かというと違う
80
81     cf = CharacterFactory.instance
82     char1 = cf.get("字")
83     char2 = cf.get("字")
84     assert_equal(char1, char2, "factory") #==である
85     assert_same(char1, char2, "factory") #かつ同じinstanceであることが保証される
86
87     char1 = Character.get("字") #Character.newの代りにCharacter.getを使うとCharacterFactoryを使ったのと同じ効果がある。
88     char2 = Character.get("字")
89     assert_equal(char1, char2) #==である
90     assert_same(char1, char2) #かつ同じinstanceであることが保証される
91   end
92   def p_er(er)
93     p er.de_er.char.inspect_all
94   end
95   def nu_test_has_attribute
96     assert("&J90-4833;".de_er.char.has_attribute?) #罪
97     assert(! "&MCS-00E06E9B;;".de_er.char.has_attribute?) #罪のisolated character, attributeを持ってない
98     assert("&C1-602E;".de_er.char.has_attribute?) #渡
99     assert("&J90-454F;".de_er.char.has_attribute?) #渡
100     p_er("&C1-602E;") #渡
101     p_er("&J90-454F;")
102     p_er("&J83-4D63;") #翼
103     p_er("&J90-4D63;")
104     p_er("&J83-3958;") #購
105     p_er("&J90-3958;")
106   end
107   def test_read_ucs
108     assert_equal("字", "&U5B57;".de_er)
109     assert_equal("字", "&U-5B57;".de_er)
110     assert_equal("字", "&U+5B57;".de_er)
111     assert_equal("字", "&#x5B57;".de_er)
112   end
113   def test_alias
114     assert_raises(NameError, message=""){
115       t = "字字".ucs
116     }
117     assert_equal(23383, "字".ucs)
118     assert_equal(0x5B57, "字".ucs)
119     assert_equal(0x5B57, @char.ucs)
120
121     assert_equal(0xfa55, "突".char['=>ucs@jis'])
122     assert_equal(0xfa55, "突".map_ucs_at_jis)
123
124     char1 = Character.get("23383")
125     char2 = Character.get(23383)
126     assert_equal(char1, char2)
127
128     char1 = Character.get("2")
129     char2 = Character.get(2)
130     assert_not_equal(char1, char2)
131
132     assert_equal("(((name . \"FULLWIDTH DIGIT ONE\") (=ucs . 65297)))", "1".char['->fullwidth'])
133     assert_equal("(((name . \"FULLWIDTH DIGIT ONE\") (=ucs . 65297)))", "1".char.to_fullwidth)
134     assert_equal("(((name . \"DIGIT ONE\") (=ucs . 49)))", "1".char['<-fullwidth'])
135     assert_equal("(((name . \"DIGIT ONE\") (=ucs . 49)))", "1".char.from_fullwidth)
136   end
137   def teardown() @char = nil  end
138 end
139
140 #===== PRINT_ALL [字] MCS-00005B57 &J90-3B7A; =====
141 #chinese-gb2312: 0x5756
142 #chinese-isoir165: 0x5756
143 #korean-ksc5601: 0x6D2E
144 #ucs: 0x5B57
145 #chinese-cns11643-1: 0x4773
146 #chinese-big5: 0xA672
147
148 #  test_print(Character.get("&CDP-8B42;"))
149 #  test_print(Character.get("&I-CDP-8AF6;"))
150 #===== PRINT_ALL [舛] MCS-00ECA524 &K0-743F; =====
151
152 #----------------------------------------------------------------------end.