[Tomoe-cvs 1246] CVS update: tomoe/test

Zurück zum Archiv-Index

Kouhei Sutou kous****@users*****
2006年 11月 28日 (火) 11:01:29 JST


Index: tomoe/test/context_spec.rb
diff -u tomoe/test/context_spec.rb:1.14 tomoe/test/context_spec.rb:1.15
--- tomoe/test/context_spec.rb:1.14	Mon Nov 27 17:52:01 2006
+++ tomoe/test/context_spec.rb	Tue Nov 28 11:01:29 2006
@@ -10,39 +10,16 @@
     @@context
   end
 
-  def numbers_to_point(str)
-    point = str.split.collect {|x| Integer(x)}
-    raise ArgumentError if point.size != 2
-    point
-  end
-
-  test_data_dir = TomoeSpecUtils::Config.test_data_dir
-  Dir.glob(File.join(test_data_dir, "*.data")).each do |file|
+  TomoeSpecUtils::Config.test_data_files.each do |file|
     base = File.basename(file)
     specify "Search by strokes for #{base}" do
-      expected = [base]
-      writing = Tomoe::Writing.new
-      File.open(file) do |f|
-        expected << f.gets.split
-        f.each do |line|
-          next if /\A\s*\z/ =~ line
-          begin
-            first_point, *rest_points = line.split(/,/)
-            numbered_first_point = numbers_to_point(first_point)
-            writing.move_to(*numbered_first_point)
-            rest_points.each do |point|
-              writing.line_to(*numbers_to_point(point))
-            end
-          rescue ArgumentError
-            raise "invalid format in #{file} at #{f.lineno}: #{line}"
-          end
-        end
-      end
+      expected, writing = TomoeSpecUtils::TestData.parse(file)
       query = Tomoe::Query.new
       query.writing = writing
       cands = context.search(query)
+      actual = cands.collect {|cand| cand.character.utf8}
 
-      [base, cands.collect {|cand| cand.character.utf8}].should == expected
+      [base, actual].should == [base, expected]
     end
   end
 
Index: tomoe/test/tomoe-spec-utils.rb
diff -u tomoe/test/tomoe-spec-utils.rb:1.8 tomoe/test/tomoe-spec-utils.rb:1.9
--- tomoe/test/tomoe-spec-utils.rb:1.8	Mon Nov 27 14:21:31 2006
+++ tomoe/test/tomoe-spec-utils.rb	Tue Nov 28 11:01:29 2006
@@ -69,33 +69,72 @@
 
     def setup_context
       super
-      @config_file = Tempfile.new("tomoe")
-      setup_config_file
+      @config_file = make_config_file
     end
 
     def teardown_context
       super
     end
 
-    def setup_config_file
-      files = %w(kanjidic2.xml readingtest.xml all.xml)
-      names = files.collect {|file| File.join(data_dir, file)}.join(";")
-      @config_file.open
-      @config_file.puts(<<-EOC)
+    def dictionaries
+      %w(kanjidic2.xml all.xml).collect do |xml|
+        File.join(data_dir, xml)
+      end
+    end
+
+    def test_data_files
+      Dir.glob(File.join(test_data_dir, "*.data"))
+    end
+
+    def make_config_file(name=nil)
+      name ||= "tomoe"
+      config_file = Tempfile.new(name)
+      config_file.open
+      config_file.puts(<<-EOC)
 [config]
 use_system_dictionaries = false
-
-[all-dictionary]
+EOC
+      dictionaries.each_with_index do |dictionary, i|
+        config_file.puts(<<-EOC)
+[#{File.basename(dictionary)}-dictionary]
 type = xml
-file = #{File.join(data_dir, "all.xml")}
-use = true
+file = #{dictionary}
+#{(i % 2).zero? ? 'use = true' : ''}
+EOC
+      end
+      config_file.close
+      config_file
+    end
+  end
 
-[kanjidic2-dictionary]
-type = xml
-file = #{File.join(data_dir, "kanjidic2.xml")}
+  module TestData
+    module_function
+    def parse(file)
+      expected = nil
+      writing = Tomoe::Writing.new
+      File.open(file) do |f|
+        expected = f.gets.split
+        f.each do |line|
+          next if /\A\s*\z/ =~ line
+          begin
+            first_point, *rest_points = line.split(/,/)
+            numbered_first_point = numbers_to_point(first_point)
+            writing.move_to(*numbered_first_point)
+            rest_points.each do |point|
+              writing.line_to(*numbers_to_point(point))
+            end
+          rescue ArgumentError
+            raise "invalid format in #{file} at #{f.lineno}: #{line}"
+          end
+        end
+      end
+      [expected, writing]
+    end
 
-EOC
-      @config_file.close
+    def numbers_to_point(str)
+      point = str.split.collect {|x| Integer(x)}
+      raise ArgumentError if point.size != 2
+      point
     end
   end
 end


tomoe-cvs メーリングリストの案内
Zurück zum Archiv-Index