09daa1362b111dd34ca7853395e407a0be048510
[chise/ruby.git] / test / test-libchise.rb
1 #!/usr/bin/env ruby
2 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
3
4 require "common"
5
6 class TestLibChise < Test::Unit::TestCase
7   def test_libchise
8     db_dir = CHISE::DataSource::DB_DIR
9     assert_match(/chise-db\Z/, db_dir)
10
11     @ds = CHISE::DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755)
12     assert_instance_of(CHISE::DataSource, @ds)
13
14     @ds.each_feature_name {|name|
15       #assert_instance_of(String, name)
16     }
17
18     # get a feature value
19     char_id = 0x4ECF
20     feature = @ds.get_feature("ideographic-structure")
21     assert_instance_of(CHISE::Feature, feature)
22     #assert_equal(true, feature.setup_db(0))
23     #assert_equal(true, feature.sync)
24     value = feature.get_value(char_id)
25     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
26
27     # shortcut
28     value = @ds.load_feature(char_id, "ideographic-structure")
29     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
30
31     # set a value
32     feature = @ds.get_feature("test-libchise")
33     assert_instance_of(CHISE::Feature, feature)
34     v = "testvalue"+$$.to_s
35     assert_equal(true, feature.set_value(char_id, v))
36     assert_equal(v, feature.get_value(char_id))
37
38     # each char
39     feature = @ds.get_feature("numeric-value")
40     feature.each_char {|cid, valdatum|
41       assert_kind_of(Numeric, cid)
42       assert_instance_of(String, valdatum)
43
44       ucs = @ds.get_feature("=ucs").get_value(cid)
45       if ucs
46         assert_instance_of(String, ucs)
47       else
48         ucs = @ds.get_feature("=>ucs").get_value(cid)
49         if ucs
50           assert_instance_of(String, ucs)
51         end
52       end
53
54       name = @ds.get_feature("name").get_value(cid)
55       if name
56         assert_instance_of(String, name)
57       end
58     }
59
60     # get a character by Daikanwa number 364.
61     ccs = @ds.get_ccs("=daikanwa")
62     assert_instance_of(CHISE::CCS, ccs)
63     #assert_equal(true, ccs.setup_db(0))
64     #assert_equal(true, ccs.sync)
65     char_id = ccs.decode(364)
66     assert_equal(0x4ECF, char_id)
67
68     # shortcut
69     char_id = @ds.decode_char("=daikanwa", 364)
70     assert_equal(0x4ECF, char_id)
71
72     # set a code_point
73     ccs = @ds.get_ccs("test-ccs")
74     assert_instance_of(CHISE::CCS, ccs)
75     assert_equal(true, ccs.set($$, 0x4ECF))
76     assert_equal(0x4ECF, ccs.decode($$))
77
78     @ds.close
79   end
80 end