rgen 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. data/CHANGELOG +20 -1
  2. data/MIT-LICENSE +1 -1
  3. data/README +12 -9
  4. data/lib/instantiators/ea_instantiator.rb +36 -0
  5. data/lib/metamodels/uml13_metamodel.rb +559 -0
  6. data/lib/metamodels/uml13_metamodel_ext.rb +26 -0
  7. data/lib/mmgen/metamodel_generator.rb +5 -5
  8. data/lib/mmgen/mm_ext/ecore_ext.rb +95 -0
  9. data/lib/mmgen/mmgen.rb +6 -4
  10. data/lib/mmgen/templates/annotations.tpl +37 -0
  11. data/lib/mmgen/templates/metamodel_generator.tpl +171 -0
  12. data/lib/rgen/ecore/ecore.rb +190 -0
  13. data/lib/rgen/ecore/ecore_instantiator.rb +25 -0
  14. data/lib/rgen/ecore/ecore_transformer.rb +85 -0
  15. data/lib/rgen/environment.rb +9 -24
  16. data/lib/rgen/find_helper.rb +68 -0
  17. data/lib/rgen/{instantiator.rb → instantiator/abstract_instantiator.rb} +6 -2
  18. data/lib/rgen/instantiator/abstract_xml_instantiator.rb +59 -0
  19. data/lib/rgen/instantiator/default_xml_instantiator.rb +117 -0
  20. data/lib/rgen/instantiator/ecore_xml_instantiator.rb +144 -0
  21. data/lib/rgen/instantiator/nodebased_xml_instantiator.rb +157 -0
  22. data/lib/rgen/instantiator/xmi11_instantiator.rb +164 -0
  23. data/lib/rgen/metamodel_builder.rb +103 -9
  24. data/lib/rgen/metamodel_builder/build_helper.rb +26 -4
  25. data/lib/rgen/metamodel_builder/builder_extensions.rb +285 -88
  26. data/lib/rgen/metamodel_builder/builder_runtime.rb +7 -1
  27. data/lib/rgen/metamodel_builder/data_types.rb +67 -0
  28. data/lib/rgen/metamodel_builder/intermediate/annotation.rb +30 -0
  29. data/lib/rgen/metamodel_builder/metamodel_description.rb +232 -0
  30. data/lib/rgen/metamodel_builder/mm_multiple.rb +23 -0
  31. data/lib/rgen/metamodel_builder/module_extension.rb +33 -0
  32. data/lib/rgen/model_comparator.rb +56 -0
  33. data/lib/rgen/model_dumper.rb +5 -5
  34. data/lib/rgen/name_helper.rb +17 -1
  35. data/lib/rgen/template_language.rb +148 -28
  36. data/lib/rgen/template_language/directory_template_container.rb +56 -38
  37. data/lib/rgen/template_language/output_handler.rb +93 -77
  38. data/lib/rgen/template_language/template_container.rb +186 -143
  39. data/lib/rgen/transformer.rb +19 -14
  40. data/lib/transformers/uml13_to_ecore.rb +75 -0
  41. data/redist/xmlscan/ChangeLog +1301 -0
  42. data/redist/xmlscan/README +34 -0
  43. data/redist/xmlscan/THANKS +11 -0
  44. data/redist/xmlscan/doc/changes.html +74 -0
  45. data/redist/xmlscan/doc/changes.rd +80 -0
  46. data/redist/xmlscan/doc/en/conformance.html +136 -0
  47. data/redist/xmlscan/doc/en/conformance.rd +152 -0
  48. data/redist/xmlscan/doc/en/manual.html +356 -0
  49. data/redist/xmlscan/doc/en/manual.rd +402 -0
  50. data/redist/xmlscan/doc/ja/conformance.ja.html +118 -0
  51. data/redist/xmlscan/doc/ja/conformance.ja.rd +134 -0
  52. data/redist/xmlscan/doc/ja/manual.ja.html +325 -0
  53. data/redist/xmlscan/doc/ja/manual.ja.rd +370 -0
  54. data/redist/xmlscan/doc/src/Makefile +41 -0
  55. data/redist/xmlscan/doc/src/conformance.rd.src +256 -0
  56. data/redist/xmlscan/doc/src/langsplit.rb +110 -0
  57. data/redist/xmlscan/doc/src/manual.rd.src +614 -0
  58. data/redist/xmlscan/install.rb +41 -0
  59. data/redist/xmlscan/lib/xmlscan/encoding.rb +311 -0
  60. data/redist/xmlscan/lib/xmlscan/htmlscan.rb +289 -0
  61. data/redist/xmlscan/lib/xmlscan/namespace.rb +352 -0
  62. data/redist/xmlscan/lib/xmlscan/parser.rb +299 -0
  63. data/redist/xmlscan/lib/xmlscan/scanner.rb +1109 -0
  64. data/redist/xmlscan/lib/xmlscan/version.rb +22 -0
  65. data/redist/xmlscan/lib/xmlscan/visitor.rb +158 -0
  66. data/redist/xmlscan/lib/xmlscan/xmlchar.rb +441 -0
  67. data/redist/xmlscan/memo/CONFORMANCE +1249 -0
  68. data/redist/xmlscan/memo/PRODUCTIONS +195 -0
  69. data/redist/xmlscan/memo/contentspec.ry +335 -0
  70. data/redist/xmlscan/samples/chibixml.rb +105 -0
  71. data/redist/xmlscan/samples/getxmlchar.rb +122 -0
  72. data/redist/xmlscan/samples/rexml.rb +159 -0
  73. data/redist/xmlscan/samples/xmlbench.rb +88 -0
  74. data/redist/xmlscan/samples/xmlbench/parser/chibixml.rb +22 -0
  75. data/redist/xmlscan/samples/xmlbench/parser/nqxml.rb +29 -0
  76. data/redist/xmlscan/samples/xmlbench/parser/rexml.rb +62 -0
  77. data/redist/xmlscan/samples/xmlbench/parser/xmlparser.rb +22 -0
  78. data/redist/xmlscan/samples/xmlbench/parser/xmlscan-0.0.10.rb +62 -0
  79. data/redist/xmlscan/samples/xmlbench/parser/xmlscan-chibixml.rb +22 -0
  80. data/redist/xmlscan/samples/xmlbench/parser/xmlscan-rexml.rb +22 -0
  81. data/redist/xmlscan/samples/xmlbench/parser/xmlscan.rb +99 -0
  82. data/redist/xmlscan/samples/xmlbench/xmlbench-lib.rb +116 -0
  83. data/redist/xmlscan/samples/xmlconftest.rb +200 -0
  84. data/redist/xmlscan/test.rb +7 -0
  85. data/redist/xmlscan/tests/deftestcase.rb +73 -0
  86. data/redist/xmlscan/tests/runtest.rb +47 -0
  87. data/redist/xmlscan/tests/testall.rb +14 -0
  88. data/redist/xmlscan/tests/testencoding.rb +438 -0
  89. data/redist/xmlscan/tests/testhtmlscan.rb +752 -0
  90. data/redist/xmlscan/tests/testnamespace.rb +457 -0
  91. data/redist/xmlscan/tests/testparser.rb +591 -0
  92. data/redist/xmlscan/tests/testscanner.rb +1749 -0
  93. data/redist/xmlscan/tests/testxmlchar.rb +143 -0
  94. data/redist/xmlscan/tests/visitor.rb +34 -0
  95. data/test/array_extensions_test.rb +2 -2
  96. data/test/ea_instantiator_test.rb +41 -0
  97. data/test/ecore_self_test.rb +53 -0
  98. data/test/environment_test.rb +11 -6
  99. data/test/metamodel_builder_test.rb +404 -245
  100. data/test/metamodel_roundtrip_test.rb +52 -0
  101. data/test/metamodel_roundtrip_test/TestModel.rb +65 -0
  102. data/test/metamodel_roundtrip_test/TestModel_Regenerated.rb +64 -0
  103. data/test/metamodel_roundtrip_test/houseMetamodel.ecore +32 -0
  104. data/test/metamodel_roundtrip_test/houseMetamodel_from_ecore.rb +39 -0
  105. data/test/rgen_test.rb +3 -3
  106. data/test/template_language_test.rb +65 -39
  107. data/test/template_language_test/expected_result.txt +24 -3
  108. data/test/template_language_test/templates/code/array.tpl +11 -0
  109. data/test/template_language_test/templates/content/author.tpl +7 -0
  110. data/test/template_language_test/templates/content/chapter.tpl +1 -1
  111. data/test/template_language_test/templates/root.tpl +17 -8
  112. data/test/template_language_test/testout.txt +24 -3
  113. data/test/testmodel/class_model_checker.rb +119 -0
  114. data/test/{xmi_instantiator_test/testmodel.eap → testmodel/ea_testmodel.eap} +0 -0
  115. data/test/{xmi_instantiator_test/testmodel.xml → testmodel/ea_testmodel.xml} +81 -14
  116. data/test/testmodel/ea_testmodel_partial.xml +317 -0
  117. data/test/testmodel/ecore_model_checker.rb +101 -0
  118. data/test/testmodel/manual_testmodel.xml +22 -0
  119. data/test/testmodel/object_model_checker.rb +67 -0
  120. data/test/transformer_test.rb +18 -10
  121. data/test/xml_instantiator_test.rb +81 -8
  122. data/test/xml_instantiator_test/simple_ecore_model_checker.rb +94 -0
  123. data/test/xml_instantiator_test/simple_xmi_ecore_instantiator.rb +53 -0
  124. data/test/xml_instantiator_test/simple_xmi_metamodel.rb +49 -0
  125. data/test/xml_instantiator_test/simple_xmi_to_ecore.rb +75 -0
  126. metadata +126 -28
  127. data/lib/ea/xmi_class_instantiator.rb +0 -46
  128. data/lib/ea/xmi_helper.rb +0 -26
  129. data/lib/ea/xmi_metamodel.rb +0 -34
  130. data/lib/ea/xmi_object_instantiator.rb +0 -46
  131. data/lib/ea/xmi_to_classmodel.rb +0 -78
  132. data/lib/ea/xmi_to_objectmodel.rb +0 -92
  133. data/lib/mmgen/mm_ext/uml_classmodel_ext.rb +0 -71
  134. data/lib/mmgen/templates/uml_classmodel.tpl +0 -63
  135. data/lib/rgen/xml_instantiator.rb +0 -132
  136. data/lib/uml/objectmodel_instantiator.rb +0 -53
  137. data/lib/uml/uml_classmodel.rb +0 -92
  138. data/lib/uml/uml_objectmodel.rb +0 -65
  139. data/test/metamodel_generator_test.rb +0 -44
  140. data/test/metamodel_generator_test/TestModel.rb +0 -40
  141. data/test/metamodel_generator_test/expected_result.txt +0 -40
  142. data/test/xmi_class_instantiator_test.rb +0 -24
  143. data/test/xmi_instantiator_test/class_model_checker.rb +0 -97
  144. data/test/xmi_object_instantiator_test.rb +0 -65
  145. data/test/xml_instantiator_test/testmodel.xml +0 -7
@@ -0,0 +1,200 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # samples/xmlconftest.rb - OASIS XML Conformance Test
4
+ #
5
+ # Copyright (C) UENO Katsuhiro 2002
6
+ #
7
+ # $Id: xmlconftest.rb,v 1.3 2003/01/22 16:46:32 katsu Exp $
8
+ #
9
+ #
10
+ # OASIS - Technical Committees - XML Conformance
11
+ # http://www.oasis-open.org/committees/xml-conformance/xml-test-suite.shtml
12
+ #
13
+
14
+ require 'samples/xmlbench/xmlbench-lib'
15
+
16
+ $KCODE = 'U'
17
+
18
+ # you must edit here as your environment.
19
+ XMLCONFDIR = "./xmlconf/"
20
+ XMLCONFLOG = "./xmlconftest.log"
21
+
22
+
23
+
24
+ PositiveTests = Dir.glob(XMLCONFDIR + '*/valid/*/*.xml') +
25
+ Dir.glob(XMLCONFDIR + 'oasis/p*pass*.xml')
26
+
27
+ NegativeTests = Dir.glob(XMLCONFDIR + '*/not-wf/*/*.xml') +
28
+ Dir.glob(XMLCONFDIR + 'oasis/p*fail*.xml')
29
+
30
+ NegativeTestRejects = [
31
+ # non-validating processors may accept these instances.
32
+ 'oasis/p06fail1.xml',
33
+ 'oasis/p08fail1.xml',
34
+ 'oasis/p08fail2.xml',
35
+ # non-validating processors may accept these instances
36
+ # since they require XML processors to read external entites.
37
+ 'ibm/not-wf/P30/ibm30n01.xml',
38
+ 'ibm/not-wf/P31/ibm31n01.xml',
39
+ 'ibm/not-wf/P61/ibm61n01.xml',
40
+ 'ibm/not-wf/P62/ibm62n01.xml',
41
+ 'ibm/not-wf/P62/ibm62n02.xml',
42
+ 'ibm/not-wf/P62/ibm62n03.xml',
43
+ 'ibm/not-wf/P62/ibm62n04.xml',
44
+ 'ibm/not-wf/P62/ibm62n05.xml',
45
+ 'ibm/not-wf/P62/ibm62n06.xml',
46
+ 'ibm/not-wf/P62/ibm62n07.xml',
47
+ 'ibm/not-wf/P62/ibm62n08.xml',
48
+ 'ibm/not-wf/P63/ibm63n01.xml',
49
+ 'ibm/not-wf/P63/ibm63n02.xml',
50
+ 'ibm/not-wf/P63/ibm63n03.xml',
51
+ 'ibm/not-wf/P63/ibm63n04.xml',
52
+ 'ibm/not-wf/P63/ibm63n05.xml',
53
+ 'ibm/not-wf/P63/ibm63n06.xml',
54
+ 'ibm/not-wf/P63/ibm63n07.xml',
55
+ 'ibm/not-wf/P64/ibm64n01.xml',
56
+ 'ibm/not-wf/P64/ibm64n02.xml',
57
+ 'ibm/not-wf/P64/ibm64n03.xml',
58
+ 'ibm/not-wf/P65/ibm65n01.xml',
59
+ 'ibm/not-wf/P65/ibm65n02.xml',
60
+ 'ibm/not-wf/P77/ibm77n03.xml',
61
+ 'ibm/not-wf/P77/ibm77n04.xml',
62
+ 'ibm/not-wf/P78/ibm78n01.xml',
63
+ 'ibm/not-wf/P78/ibm78n02.xml',
64
+ 'ibm/not-wf/P79/ibm79n01.xml',
65
+ 'ibm/not-wf/P79/ibm79n02.xml',
66
+ 'oasis/p09fail1.xml',
67
+ 'oasis/p09fail2.xml',
68
+ 'oasis/p30fail1.xml',
69
+ 'oasis/p31fail1.xml',
70
+ 'oasis/p61fail1.xml',
71
+ 'oasis/p62fail1.xml',
72
+ 'oasis/p62fail2.xml',
73
+ 'oasis/p63fail1.xml',
74
+ 'oasis/p63fail2.xml',
75
+ 'oasis/p64fail1.xml',
76
+ 'oasis/p64fail2.xml',
77
+ 'xmltest/not-wf/ext-sa/001.xml',
78
+ 'xmltest/not-wf/ext-sa/002.xml',
79
+ 'xmltest/not-wf/ext-sa/003.xml',
80
+ 'xmltest/not-wf/not-sa/001.xml',
81
+ 'xmltest/not-wf/not-sa/003.xml',
82
+ 'xmltest/not-wf/not-sa/004.xml',
83
+ 'xmltest/not-wf/not-sa/005.xml',
84
+ 'xmltest/not-wf/not-sa/006.xml',
85
+ 'xmltest/not-wf/not-sa/007.xml',
86
+ 'xmltest/not-wf/not-sa/008.xml',
87
+ ]
88
+
89
+ #NegativeTests.reject! { |i|
90
+ # NegativeTestRejects.include? i[XMLCONFDIR.size..-1]
91
+ #}
92
+
93
+
94
+
95
+ class XMLConfTest
96
+
97
+ def initialize(parser)
98
+ @parser = parser
99
+ @result = {}
100
+ end
101
+
102
+ def class_name
103
+ @parser.class.name
104
+ end
105
+
106
+ def name
107
+ @parser.name
108
+ end
109
+
110
+ def total
111
+ @result.size
112
+ end
113
+
114
+ def success
115
+ @result.select{ |k,v| v }.collect{ |k,v| k }.sort
116
+ end
117
+
118
+ def failure
119
+ @result.select{ |k,v| not v }.collect{ |k,v| k }.sort
120
+ end
121
+
122
+ def success_percent
123
+ 100.0 * success.size / total
124
+ end
125
+
126
+ def failure_percent
127
+ 100.0 * failure.size / total
128
+ end
129
+
130
+ def check(format, *files)
131
+ total = 0
132
+ ok = 0
133
+ failed = 0
134
+ files.each { |i|
135
+ total += 1
136
+ File.open(i) { |f|
137
+ result = true
138
+ begin
139
+ @parser.parse f.read
140
+ rescue Exception
141
+ result = false
142
+ end
143
+ @result[i] = result
144
+ if result then
145
+ ok += 1
146
+ else
147
+ failed += 1
148
+ end
149
+ }
150
+ printf format+"\r",
151
+ name, total, ok, ok*100.0/total, failed, failed*100.0/total
152
+ }
153
+ print "\n"
154
+ end
155
+
156
+ end
157
+
158
+
159
+
160
+ XMLBench.weight_limit = 10
161
+
162
+ if ARGV.empty? then
163
+ subjects = XMLBench.setup_all
164
+ else
165
+ subjects = XMLBench.setup(*ARGV)
166
+ end
167
+
168
+
169
+
170
+ log = File.open(XMLCONFLOG, 'w')
171
+ END { log.close }
172
+
173
+
174
+ puts ' '*30+' total success failure'
175
+
176
+ puts "** POSITIVE TEST **"
177
+
178
+ subjects.each { |parser|
179
+ conf = XMLConfTest.new(parser)
180
+ conf.check("%-30s %4d %4d (%5.1f%%) %4d (%5.1f%%)",
181
+ *PositiveTests)
182
+ if conf.failure.size > 0 then
183
+ log.puts "---- #{conf.name} couldn't parse the following files:"
184
+ conf.failure.each { |i| log.puts "P #{conf.class_name} #{i}" }
185
+ log.puts
186
+ end
187
+ }
188
+
189
+ puts "** NEGATIVE TEST **"
190
+
191
+ subjects.each { |parser|
192
+ conf = XMLConfTest.new(parser)
193
+ conf.check("%-30s %4d %4d (%5.1f%%) %4d (%5.1f%%)",
194
+ *NegativeTests)
195
+ if conf.success.size > 0 then
196
+ log.puts "---- #{conf.name} could parse the following files:"
197
+ conf.success.each { |i| log.puts "N #{conf.class_name} #{i}" }
198
+ log.puts
199
+ end
200
+ }
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/ruby
2
+ $-w = true
3
+ $LOAD_PATH.unshift 'lib'
4
+ $LOAD_PATH.unshift 'tests'
5
+ Dir.chdir File.dirname($0)
6
+ require 'testall'
7
+ load 'runtest.rb'
@@ -0,0 +1,73 @@
1
+ #
2
+ # tests/deftestcase.rb
3
+ #
4
+ # Copyright (C) UENO Katsuhiro 2002
5
+ #
6
+ # $Id: deftestcase.rb,v 1.7 2003/02/28 12:31:07 katsu Exp $
7
+ #
8
+
9
+ module DefTestCase
10
+
11
+ class << self
12
+ private
13
+ def append_features(mod)
14
+ super
15
+ mod.extend DefTestCaseSingleton
16
+ end
17
+ end
18
+
19
+
20
+ module DefTestCaseSingleton
21
+
22
+ private
23
+
24
+ def deftestcase(prefix, testcases)
25
+ filename, lineno = caller.first.split(':')
26
+ lineno = lineno.to_i + 1
27
+ testno = 0
28
+ testcases.split(/^\s+?^/).each { |test|
29
+ test = test.split(/^/)
30
+ test.each { |i| i.strip! }
31
+ args = test.reject { |i| i.empty? or /\A#/ =~ i }
32
+ unless args.empty? then
33
+ name = sprintf('test_%s_%03d', prefix, testno += 1)
34
+ src = "def #{name} ; do_testcase(#{args.join(',')}) ; end"
35
+ module_eval src, filename, lineno
36
+ end
37
+ lineno += test.size + 1
38
+ }
39
+ end
40
+
41
+ end
42
+
43
+
44
+ private
45
+
46
+ def do_testcase(src, *expected)
47
+ expected = [ [:on_start_document] ] + expected + [ [:on_end_document] ]
48
+ result = parse(src)
49
+ begin
50
+ assert_equal expected, result
51
+ rescue Test::Unit::AssertionFailedError => e
52
+ was = ''
53
+ result.each_with_index { |i,n|
54
+ was << "\t#{i.inspect}"
55
+ was << " <= !!!!" unless i == expected[n]
56
+ was << "\n"
57
+ }
58
+ msg = "\ntestcase:\n\t" + src +
59
+ "\nexpected:\n" + expected.collect{|i|"\t#{i.inspect}\n"}.join +
60
+ "but was:\n" + was
61
+ backtrace = e.backtrace.reject { |i|
62
+ /\A#{__FILE__}:\d+:in `do_testcase'/n =~ i
63
+ }
64
+ raise e, msg, backtrace
65
+ end
66
+ end
67
+
68
+
69
+ def parse(src)
70
+ raise NotImplementedError, "abstract method"
71
+ end
72
+
73
+ end
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # runtest.rb
4
+ #
5
+ # $Id: runtest.rb,v 1.6 2005/05/22 07:07:12 nahi Exp $
6
+ #
7
+
8
+ require 'test/unit'
9
+ require 'test/unit/testsuite'
10
+ require 'test/unit/ui/console/testrunner'
11
+
12
+ $SAFE = 2 rescue nil
13
+ STDOUT.sync = true
14
+ STDERR.sync = true
15
+
16
+
17
+ if ARGV[0] == '-v' then
18
+ output_level = Test::Unit::UI::VERBOSE
19
+ ARGV.shift
20
+ else
21
+ output_level = Test::Unit::UI::NORMAL
22
+ end
23
+
24
+
25
+ argv = ARGV.collect { |i| i.split(/[#:]/, 2) } # [ class, testcase ]
26
+
27
+ def argv.exactly_include?(classname)
28
+ empty? or find{ |i| i == [classname] }
29
+ end
30
+ def argv.testcases(classname)
31
+ select{ |i| i.first == classname }.collect{ |i| i.last }
32
+ end
33
+
34
+
35
+ suite = Test::Unit::TestSuite.new
36
+ ObjectSpace.each_object(Class) { |c|
37
+ if /\ATest/ =~ c.name and not c.equal? Test::Unit::TestCase and
38
+ c.ancestors.include? Test::Unit::TestCase then
39
+ if argv.exactly_include? c.name then
40
+ suite << c.suite
41
+ else
42
+ argv.testcases(c.name).each { |i| suite << c.new(i) }
43
+ end
44
+ end
45
+ }
46
+
47
+ Test::Unit::UI::Console::TestRunner.run suite, output_level
@@ -0,0 +1,14 @@
1
+ #
2
+ # tests/testall.rb
3
+ #
4
+ # $Id: testall.rb,v 1.6 2003/01/12 04:10:59 katsu Exp $
5
+ #
6
+
7
+ require 'testscanner'
8
+ require 'testhtmlscan'
9
+ require 'testparser'
10
+ require 'testnamespace'
11
+ require 'testxmlchar'
12
+ require 'testencoding'
13
+
14
+ load "#{File.dirname($0)}/runtest.rb" if __FILE__ == $0
@@ -0,0 +1,438 @@
1
+ #
2
+ # tests/xmlchar.rb
3
+ #
4
+ # Copyright (C) Ueno Katsuhiro 2002
5
+ #
6
+ # $Id: testencoding.rb,v 1.5 2003/02/28 12:31:07 katsu Exp $
7
+ #
8
+
9
+ require 'test/unit'
10
+ require 'xmlscan/encoding'
11
+
12
+
13
+ class TestEncoding < Test::Unit::TestCase
14
+
15
+ NoMethodError = NameError unless defined? NoMethodError
16
+
17
+ class MyEncodingClass < XMLScan::EncodingClass
18
+ class << self
19
+ public :new
20
+ end
21
+ end
22
+
23
+ EncodingError = XMLScan::EncodingError
24
+
25
+ class FugaConverter < XMLScan::Converter
26
+ def initialize
27
+ @hoge = 'FUGA'
28
+ end
29
+ def convert(s)
30
+ ret = "#{@hoge}:#{s}:BUYO"
31
+ @hoge = ''
32
+ ret
33
+ end
34
+ def finish
35
+ "FOOBAR"
36
+ end
37
+ end
38
+
39
+
40
+ NONE = //n.kcode
41
+ EUC = //e.kcode
42
+ SJIS = //s.kcode
43
+ UTF8 = //u.kcode
44
+
45
+
46
+ def setup
47
+ @e = MyEncodingClass.new
48
+ @e.add_converter('hoge', 'fuga', 10) { |s| "HOGE:#{s}:FUGA" }
49
+ @e.add_converter('fuga', 'buyo', 10, FugaConverter)
50
+ @e.set_kcode 'hoge', 'E'
51
+ @e.set_kcode 'fuga', 'S'
52
+ end
53
+
54
+
55
+ def test_s_new
56
+ assert_raises(NoMethodError) { XMLScan::EncodingClass.new }
57
+ end
58
+
59
+ def test_s_instance
60
+ assert_kind_of XMLScan::EncodingClass, XMLScan::Encoding
61
+ end
62
+
63
+ def test_preset_kcode
64
+ assert_equal UTF8, XMLScan::Encoding.kcode('utf-8')
65
+ assert_equal nil, XMLScan::Encoding.kcode('utf-16')
66
+ assert_equal nil, XMLScan::Encoding.kcode('iso-10646-ucs-2')
67
+ assert_equal nil, XMLScan::Encoding.kcode('iso-10646-ucs-4')
68
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-1')
69
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-2')
70
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-3')
71
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-4')
72
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-5')
73
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-6')
74
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-7')
75
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-8')
76
+ assert_equal NONE, XMLScan::Encoding.kcode('iso-8859-9')
77
+ assert_equal nil, XMLScan::Encoding.kcode('iso-2022-jp')
78
+ assert_equal SJIS, XMLScan::Encoding.kcode('shift_jis')
79
+ assert_equal SJIS, XMLScan::Encoding.kcode('windows-31J')
80
+ assert_equal EUC, XMLScan::Encoding.kcode('euc-jp')
81
+ assert_equal EUC, XMLScan::Encoding.kcode('euc-kr')
82
+ end
83
+
84
+
85
+ def test_alias
86
+ assert_nil @e.alias('foo', 'hoge')
87
+ assert_equal EUC, @e.kcode('foo')
88
+ c = nil
89
+ assert_nothing_raised { c = @e.converter('foo', 'fuga') }
90
+ assert_equal "HOGE:foo:FUGA", c.convert("foo")
91
+ end
92
+
93
+ def test_alias_share
94
+ assert_nil @e.alias('foo', 'hoge')
95
+ @e.add_converter('hoge', 'buyo', 10) { |s| "HOGE:#{s}:BUYO" }
96
+ c = nil
97
+ assert_nothing_raised { c = @e.converter('foo', 'buyo') }
98
+ assert_equal "HOGE:foo:BUYO", c.convert("foo")
99
+ end
100
+
101
+ def test_alias_chain
102
+ assert_nil @e.alias('foo', 'hoge')
103
+ assert_nil @e.alias('bar', 'foo')
104
+ assert_equal EUC, @e.kcode('bar')
105
+ c = nil
106
+ assert_nothing_raised { c = @e.converter('bar', 'fuga') }
107
+ assert_equal "HOGE:foo:FUGA", c.convert("foo")
108
+ end
109
+
110
+ def test_alias_caseinsensitive
111
+ assert_nil @e.alias('FOO', 'hoge')
112
+ assert_equal EUC, @e.kcode('foo')
113
+ c = nil
114
+ assert_nothing_raised { c = @e.converter('foo', 'fuga') }
115
+ assert_equal "HOGE:foo:FUGA", c.convert("foo")
116
+ end
117
+
118
+ def test_alias_doubled
119
+ assert_nil @e.alias('foo', 'hoge')
120
+ assert_raises(EncodingError) { @e.alias('foo', 'fuga') }
121
+ end
122
+
123
+ def test_alias_doubled_2
124
+ assert_raises(EncodingError) { @e.alias('fuga', 'hoge') }
125
+ end
126
+
127
+ def test_alias_doubled_3
128
+ assert_raises(EncodingError) { @e.alias('hoge', 'hoge') }
129
+ end
130
+
131
+ def test_alias_undeclared
132
+ assert_raises(EncodingError) { @e.alias('foo', 'bar') }
133
+ end
134
+
135
+
136
+ def test_kcode
137
+ assert_equal EUC, @e.kcode('hoge')
138
+ assert_equal SJIS, @e.kcode('fuga')
139
+ assert_equal NONE, @e.kcode('buyo')
140
+ end
141
+
142
+ def test_kcode_undeclared
143
+ assert_equal NONE, @e.kcode('foo')
144
+ end
145
+
146
+ def test_kcode_caseinsensitive
147
+ assert_equal EUC, @e.kcode('HOGE')
148
+ assert_equal SJIS, @e.kcode('FUGA')
149
+ assert_equal NONE, @e.kcode('BUYO')
150
+ end
151
+
152
+
153
+ def test_set_kcode_e
154
+ assert_equal NONE, @e.kcode('buyo')
155
+ assert_equal EUC, @e.set_kcode('buyo', 'e')
156
+ assert_equal EUC, @e.kcode('buyo')
157
+ end
158
+
159
+ def test_set_kcode_u
160
+ assert_equal NONE, @e.kcode('buyo')
161
+ assert_equal UTF8, @e.set_kcode('buyo', 'u')
162
+ assert_equal UTF8, @e.kcode('buyo')
163
+ end
164
+
165
+ def test_set_kcode_s
166
+ assert_equal NONE, @e.kcode('buyo')
167
+ assert_equal SJIS, @e.set_kcode('buyo', 's')
168
+ assert_equal SJIS, @e.kcode('buyo')
169
+ end
170
+
171
+ def test_set_kcode_n
172
+ assert_equal NONE, @e.kcode('buyo')
173
+ assert_equal NONE, @e.set_kcode('buyo', 'n')
174
+ assert_equal NONE, @e.kcode('buyo')
175
+ end
176
+
177
+ def test_set_kcode_emp
178
+ assert_equal NONE, @e.kcode('buyo')
179
+ assert_equal nil, @e.set_kcode('buyo', '')
180
+ assert_equal nil, @e.kcode('buyo')
181
+ end
182
+
183
+ def test_set_kcode_nil
184
+ assert_equal NONE, @e.kcode('buyo')
185
+ assert_equal nil, @e.set_kcode('buyo', nil)
186
+ assert_equal nil, @e.kcode('buyo')
187
+ end
188
+
189
+ def test_set_kcode_reset
190
+ assert_nothing_raised { @e.set_kcode('hoge', 'e') }
191
+ assert_nothing_raised { @e.set_kcode('fuga', 's') }
192
+ end
193
+
194
+ def test_set_kcode_conflict
195
+ assert_raises(EncodingError) { @e.set_kcode('hoge', 'u') }
196
+ assert_raises(EncodingError) { @e.set_kcode('fuga', 'u') }
197
+ end
198
+
199
+ def test_set_kcode_conflict_2
200
+ assert_equal NONE, @e.kcode('buyo')
201
+ assert_nothing_raised { @e.set_kcode 'buyo', 'e' }
202
+ assert_equal EUC, @e.kcode('buyo')
203
+ assert_raises(EncodingError) { @e.set_kcode('buyo', 'n') }
204
+ end
205
+
206
+ def test_set_kcode_conflict_nil
207
+ assert_equal NONE, @e.kcode('buyo')
208
+ assert_nothing_raised { @e.set_kcode 'buyo', 'n' }
209
+ assert_equal NONE, @e.kcode('buyo')
210
+ assert_raises(EncodingError) { @e.set_kcode('buyo', 'e') }
211
+ end
212
+
213
+ def test_set_kcode_caseinsensitive
214
+ @e.set_kcode 'Buyo', 'E'
215
+ assert_equal EUC, @e.kcode('buyo')
216
+ assert_raises(EncodingError) { @e.set_kcode('BUYO', 's') }
217
+ end
218
+
219
+
220
+ def test_converter
221
+ c = @e.converter('hoge', 'fuga')
222
+ assert_kind_of XMLScan::SimpleConverter, c
223
+ assert_equal "HOGE:foo:FUGA", c.convert('foo')
224
+ assert_equal '', c.finish
225
+ c = @e.converter('fuga', 'buyo')
226
+ assert_kind_of FugaConverter, c
227
+ assert_equal "FUGA:foo:BUYO", c.convert('foo')
228
+ assert_equal ":bar:BUYO", c.convert('bar')
229
+ assert_equal 'FOOBAR', c.finish
230
+ end
231
+
232
+ def test_converter_same
233
+ assert_nil @e.converter('hoge', 'hoge')
234
+ assert_nil @e.converter('fuga', 'fuga')
235
+ end
236
+
237
+ def test_converter_unregistered
238
+ assert_raises(EncodingError) { @e.converter('hoge', 'buyo') }
239
+ assert_raises(EncodingError) { @e.converter('buyo', 'hoge') }
240
+ assert_raises(EncodingError) { @e.converter('fuga', 'hoge') }
241
+ assert_raises(EncodingError) { @e.converter('foo', 'bar') }
242
+ end
243
+
244
+ def test_converter_caseinsensitive
245
+ assert_nothing_raised { @e.converter('HOGE', 'fuGA') }
246
+ assert_raises(EncodingError) { @e.converter('HogE', 'BuYo') }
247
+ end
248
+
249
+
250
+ def test_add_converter
251
+ assert_nil @e.add_converter('buyo', 'foo', 0) { |s| "BUYO:#{s}:FOO" }
252
+ c = nil
253
+ assert_nothing_raised { c = @e.converter('buyo', 'foo') }
254
+ assert_equal "BUYO:foo:FOO", c.convert("foo")
255
+ end
256
+
257
+ def test_add_converter_proc
258
+ assert_nil @e.add_converter('hoge', 'buyo', 20) { |s| "HOGE:#{s}:BUYO" }
259
+ c = @e.converter('hoge', 'buyo')
260
+ assert_kind_of XMLScan::SimpleConverter, c
261
+ assert_equal "HOGE:foo:BUYO", c.convert('foo')
262
+ end
263
+
264
+ def test_add_converter_proc_2
265
+ assert_nil @e.add_converter('hoge', 'buyo', 20, proc{|s| "HOGE:#{s}:BUYO"})
266
+ c = @e.converter('hoge', 'buyo')
267
+ assert_kind_of XMLScan::SimpleConverter, c
268
+ assert_equal "HOGE:foo:BUYO", c.convert('foo')
269
+ end
270
+
271
+ def test_add_converter_klass
272
+ assert_nil @e.add_converter('hoge', 'buyo', 20, FugaConverter)
273
+ c = @e.converter('hoge', 'buyo')
274
+ assert_kind_of FugaConverter, c
275
+ assert_equal "FUGA:foo:BUYO", c.convert('foo')
276
+ end
277
+
278
+ def test_add_converter_argerror
279
+ assert_raises(ArgumentError) { @e.add_converter('hoge', 'buyo', 20) }
280
+ assert_raises(ArgumentError) {
281
+ @e.add_converter('hoge', 'buyo', 20, FugaConverter) { }
282
+ }
283
+ end
284
+
285
+ def test_add_converter_invalid
286
+ assert_nil @e.add_converter('hoge', 'buyo', 20, Object.new)
287
+ assert_raises(NoMethodError) { @e.converter('hoge', 'buyo') }
288
+ end
289
+
290
+ def test_add_converter_heavier
291
+ assert_nil @e.add_converter('hoge', 'fuga', 20) { |s| "hOGE:#{s}:fUGA" }
292
+ c = @e.converter('hoge', 'fuga')
293
+ assert_equal "HOGE:foo:FUGA", c.convert("foo")
294
+ end
295
+
296
+ def test_add_converter_lighter
297
+ assert_nil @e.add_converter('hoge', 'fuga', 0) { |s| "hoge:#{s}:fuga" }
298
+ c = @e.converter('hoge', 'fuga')
299
+ assert_equal "hoge:foo:fuga", c.convert("foo")
300
+ end
301
+
302
+ def test_add_converter_as_heavy_as
303
+ assert_nil @e.add_converter('hoge', 'fuga', 10) { |s| "hOGe:#{s}:fUGa" }
304
+ c = @e.converter('hoge', 'fuga')
305
+ assert_equal "hOGe:foo:fUGa", c.convert("foo")
306
+ end
307
+
308
+ def test_add_converter_same_encoding
309
+ assert_raises(EncodingError) { @e.add_converter('hoge', 'hoge', 10){} }
310
+ end
311
+
312
+ def test_add_converter_same_encoding_alias
313
+ @e.alias 'foo', 'hoge'
314
+ assert_raises(EncodingError) { @e.add_converter('hoge', 'foo', 10){} }
315
+ assert_raises(EncodingError) { @e.add_converter('foo', 'hoge', 10){} }
316
+ end
317
+
318
+ def test_add_converter_caseinsensitive
319
+ assert_nil @e.add_converter('HOGE', 'BUYO', 10) { |s| "HOGE:#{s}:BUYO" }
320
+ c = nil
321
+ assert_nothing_raised { c = @e.converter('hoge', 'buyo') }
322
+ assert_equal 'HOGE:foo:BUYO', c.convert('foo')
323
+ end
324
+
325
+
326
+ def test_converter3
327
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
328
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
329
+ assert_equal SJIS, kcode
330
+ assert_equal "FUGA:bar:BUYO", c2.convert('bar')
331
+ end
332
+
333
+ def test_converter3_2
334
+ c1, kcode, c2 = @e.converter3('hoge', nil)
335
+ assert_equal nil, c1
336
+ assert_equal EUC, kcode
337
+ assert_equal nil, c2
338
+ c1, kcode, c2 = @e.converter3('hoge')
339
+ assert_equal nil, c1
340
+ assert_equal EUC, kcode
341
+ assert_equal nil, c2
342
+ end
343
+
344
+ def test_converter3_3
345
+ c1, kcode, c2 = @e.converter3('hoge', 'fuga')
346
+ if c1 then
347
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
348
+ assert_equal SJIS, kcode
349
+ assert_equal nil, c2
350
+ else
351
+ assert_equal nil, c1
352
+ assert_equal EUC, kcode
353
+ assert_equal "HOGE:foo:FUGA", c2.convert('foo')
354
+ end
355
+ end
356
+
357
+ def test_converter3_4
358
+ c1, kcode, c2 = @e.converter3('fuga', 'buyo')
359
+ assert_equal nil, c1
360
+ assert_equal SJIS, kcode
361
+ assert_equal "FUGA:foo:BUYO", c2.convert('foo')
362
+ end
363
+
364
+ def test_converter3_5
365
+ c1, kcode, c2 = @e.converter3('buyo')
366
+ assert_equal nil, c1
367
+ assert_equal NONE, kcode
368
+ assert_equal nil, c2
369
+ end
370
+
371
+ def test_converter3_6
372
+ assert_raises(EncodingError) { @e.converter3('buyo', 'hoge') }
373
+ end
374
+
375
+ def test_conveter3_select
376
+ @e.add_converter('hoge', 'foo', 8) { |s| "HOGE:#{s}:FOO" }
377
+ @e.add_converter('foo', 'buyo', 10) { |s| "FOO:#{s}:BUYO" }
378
+ @e.set_kcode 'foo', 'U'
379
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
380
+ assert_equal "HOGE:foo:FOO", c1.convert('foo')
381
+ assert_equal UTF8, kcode
382
+ assert_equal "FOO:bar:BUYO", c2.convert('bar')
383
+ end
384
+
385
+ def test_conveter3_select_2
386
+ @e.add_converter('hoge', 'foo', 8) { |s| "HOGE:#{s}:FOO" }
387
+ @e.set_kcode 'foo', 'U'
388
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
389
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
390
+ assert_equal SJIS, kcode
391
+ assert_equal "FUGA:bar:BUYO", c2.convert('bar')
392
+ end
393
+
394
+ def test_conveter3_select_3
395
+ @e.add_converter('hoge', 'foo', 8) { |s| "HOGE:#{s}:FOO" }
396
+ @e.add_converter('foo', 'buyo', 20) { |s| "FOO:#{s}:BUYO" }
397
+ @e.set_kcode 'foo', 'U'
398
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
399
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
400
+ assert_equal SJIS, kcode
401
+ assert_equal "FUGA:bar:BUYO", c2.convert('bar')
402
+ end
403
+
404
+ def test_conveter3_select_4
405
+ @e.add_converter('hoge', 'foo', 20) { |s| "HOGE:#{s}:FOO" }
406
+ @e.add_converter('foo', 'buyo', 8) { |s| "FOO:#{s}:BUYO" }
407
+ @e.set_kcode 'foo', 'U'
408
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
409
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
410
+ assert_equal SJIS, kcode
411
+ assert_equal "FUGA:bar:BUYO", c2.convert('bar')
412
+ end
413
+
414
+ def test_conveter3_select_5
415
+ @e.add_converter('hoge', 'foo', 5) { |s| "HOGE:#{s}:FOO" }
416
+ @e.add_converter('foo', 'buyo', 5) { |s| "FOO:#{s}:BUYO" }
417
+ @e.set_kcode 'foo', nil
418
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
419
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
420
+ assert_equal SJIS, kcode
421
+ assert_equal "FUGA:bar:BUYO", c2.convert('bar')
422
+ end
423
+
424
+ def test_conveter3_select_6
425
+ @e.add_converter('hoge', 'foo', 5) { |s| "HOGE:#{s}:FOO" }
426
+ @e.add_converter('foo', 'buyo', 5) { |s| "FOO:#{s}:BUYO" }
427
+ c1, kcode, c2 = @e.converter3('hoge', 'buyo')
428
+ assert_equal "HOGE:foo:FUGA", c1.convert('foo')
429
+ assert_equal SJIS, kcode
430
+ assert_equal "FUGA:bar:BUYO", c2.convert('bar')
431
+ end
432
+
433
+ end
434
+
435
+
436
+
437
+
438
+ load "#{File.dirname($0)}/runtest.rb" if __FILE__ == $0