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