[前][次][番号順一覧][スレッド一覧]

ruby-changes:2819

From: ko1@a...
Date: 19 Dec 2007 01:22:47 +0900
Subject: [ruby-changes:2819] akr - Ruby:r14310 (trunk): more m17n tests.

akr	2007-12-19 01:01:09 +0900 (Wed, 19 Dec 2007)

  New Revision: 14310

  Modified files:
    trunk/test/ruby/test_m17n.rb

  Log:
    more m17n tests.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_m17n.rb?r1=14310&r2=14309

Index: test/ruby/test_m17n.rb
===================================================================
--- test/ruby/test_m17n.rb	(revision 14309)
+++ test/ruby/test_m17n.rb	(revision 14310)
@@ -518,12 +518,28 @@
     # same byte sequence
     a("\xc2\xa1"), e("\xc2\xa1"), s("\xc2\xa1"), u("\xc2\xa1"),
 
+    s("\x81A"), # mutibyte character which contains "A"
+    s("\x81a"), # mutibyte character which contains "a"
+
     # invalid
     e("\xa1"),
     s("\x81"),
     u("\xc2"),
   ]
 
+  def combination(*args)
+    if args.empty?
+      yield []
+    else
+      arg = args.shift
+      arg.each {|v|
+        combination(*args) {|vs|
+          yield [v, *vs]
+        }
+      }
+    end
+  end
+
   def test_str_new
     STRINGS.each {|s|
       t = String.new(s)
@@ -546,16 +562,14 @@
   end
 
   def test_str_plus
-    STRINGS.each {|s1|
-      STRINGS.each {|s2|
-        if s1.encoding != s2.encoding && !is_ascii_only?(s1) && !is_ascii_only?(s2)
-          assert_raise(ArgumentError) { s1 + s2 }
-        else
-          t = s1 + s2
-          assert_equal(a(s1) + a(s2), a(t))
-          assert_str_enc_propagation(t, s1, s2)
-        end
-      }
+    combination(STRINGS, STRINGS) {|s1, s2|
+      if s1.encoding != s2.encoding && !is_ascii_only?(s1) && !is_ascii_only?(s2)
+        assert_raise(ArgumentError) { s1 + s2 }
+      else
+        t = s1 + s2
+        assert_equal(a(s1) + a(s2), a(t))
+        assert_str_enc_propagation(t, s1, s2)
+      end
     }
   end
 
@@ -611,41 +625,37 @@
   end
 
   def test_str_eq
-    STRINGS.each {|s1|
-      STRINGS.each {|s2|
-        if is_ascii_only?(s1) && is_ascii_only?(s2) && a(s1) == a(s2)
-          assert(s1 == s2)
-        elsif s1.encoding == s2.encoding && a(s1) == a(s2)
-          assert(s1 == s2)
-          assert(!(s1 != s2))
-          assert_equal(0, s1 <=> s2)
-        else
-          assert(!(s1 == s2))
-          assert(s1 != s2)
-          assert_not_equal(0, s1 <=> s2)
-        end
-      }
+    combination(STRINGS, STRINGS) {|s1, s2|
+      if is_ascii_only?(s1) && is_ascii_only?(s2) && a(s1) == a(s2)
+        assert(s1 == s2)
+      elsif s1.encoding == s2.encoding && a(s1) == a(s2)
+        assert(s1 == s2)
+        assert(!(s1 != s2))
+        assert_equal(0, s1 <=> s2)
+      else
+        assert(!(s1 == s2))
+        assert(s1 != s2)
+        assert_not_equal(0, s1 <=> s2)
+      end
     }
   end
 
-  def test_str_cmp
+  def test_str_lt
     assert(a("a") < a("\xa1"))
     assert(a("a") < s("\xa1"))
     assert(s("a") < a("\xa1"))
   end
 
   def test_str_concat
-    STRINGS.each {|s1|
-      STRINGS.each {|s2|
-        s = s1.dup
-        if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
-          s << s2
-          assert_equal(a(s), a(s1) + a(s2))
-          assert_str_enc_propagation(s, s1, s2)
-        else
-          assert_raise(ArgumentError) { s << s2 }
-        end
-      }
+    combination(STRINGS, STRINGS) {|s1, s2|
+      s = s1.dup
+      if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
+        s << s2
+        assert_equal(a(s), a(s1) + a(s2))
+        assert_str_enc_propagation(s, s1, s2)
+      else
+        assert_raise(ArgumentError) { s << s2 }
+      end
     }
   end
 
@@ -714,118 +724,252 @@
     assert_equal(nil, u("\xc2\xa1\xc2\xa2\xc2\xa3")[u("\xa1\xc2")])
     assert_raise(ArgumentError) { u("\xc2\xa1\xc2\xa2\xc2\xa3")[a("\xa1\xc2")] }
 
-    STRINGS.each {|s1|
-      STRINGS.each {|s2|
+    combination(STRINGS, STRINGS) {|s1, s2|
+      if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
+        t = s1[s2]
+        if t != nil
+          assert_equal(s2, t)
+          assert_match(/#{Regexp.escape(s2)}/, s1)
+        end
+      else
+        assert_raise(ArgumentError) { s1[s2] }
+      end
+    }
+  end
+
+  def test_str_aref_range2
+    combination(STRINGS, -2..2, -2..2) {|s, first, last|
+      t = s[first..last]
+      if first < 0
+        first += s.length
+        if first < 0
+          assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
+          next
+        end
+      end
+      if s.length < first
+        assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
+        next
+      end
+      if last < 0
+        last += s.length
+      end
+      t2 = ''
+      first.upto(last) {|i|
+        c = s[i]
+        t2 << c if c
+      }
+      assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
+    }
+  end
+
+  def test_str_aref_range3
+    combination(STRINGS, -2..2, -2..2) {|s, first, last|
+      t = s[first...last]
+      if first < 0
+        first += s.length
+        if first < 0
+          assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
+          next
+        end
+      end
+      if s.length < first
+        assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
+        next
+      end
+      if last < 0
+        last += s.length
+      end
+      t2 = ''
+      first.upto(last-1) {|i|
+        c = s[i]
+        t2 << c if c
+      }
+      assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
+    }
+  end
+
+  def encdump(str)
+    "#{str.dump}.force_encoding(#{str.encoding.name.dump})"
+  end
+
+  def test_str_assign
+    combination(STRINGS, STRINGS) {|s1, s2|
+      (-2).upto(2) {|i|
+        t = s1.dup
         if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
-          t = s1[s2]
-          if t != nil
-            assert_equal(s2, t)
-            assert_match(/#{Regexp.escape(s2)}/, s1)
+          if i < -s1.length || s1.length < i
+            assert_raise(IndexError) { t[i] = s2 }
+          else
+            t[i] = s2
+            assert(a(t).index(a(s2)))
+            if s1.valid_encoding? && s2.valid_encoding?
+              if i == s1.length && s2.empty?
+                assert_nil(t[i])
+              elsif i < 0
+                assert_equal(s2, t[i-s2.length+1,s2.length],
+                  "t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i-s2.length+1},#{s2.length}]")
+              else
+                assert_equal(s2, t[i,s2.length],
+                  "t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
+              end
+            end
           end
         else
-          assert_raise(ArgumentError) { s1[s2] }
+          assert_raise(ArgumentError) { t[i] = s2 }
         end
-
       }
     }
   end
 
-  def test_str_aref_range2
-    STRINGS.each {|s|
-      (-2).upto(2) {|first|
-        (-2).upto(2) {|last|
-          t = s[first..last]
-          if first < 0
-            first += s.length
-            if first < 0
-              assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
-              next
+  def test_str_assign_len
+    combination(STRINGS, STRINGS, -2..2, 0..2) {|s1, s2, i, len|
+      t = s1.dup
+      if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
+        if i < -s1.length || s1.length < i
+          assert_raise(IndexError) { t[i,len] = s2 }
+        else
+          t[i,len] = s2
+          assert(a(t).index(a(s2)))
+          if s1.valid_encoding? && s2.valid_encoding?
+            if i == s1.length && s2.empty?
+              assert_nil(t[i])
+            elsif i < 0
+              if -i < len
+                len = -i
+              end
+              assert_equal(s2, t[i-s2.length+len,s2.length],
+                "t = #{encdump(s1)}; t[#{i},#{len}] = #{encdump(s2)}; t[#{i-s2.length+len},#{s2.length}]")
+            else
+              assert_equal(s2, t[i,s2.length],
+                "t = #{encdump(s1)}; t[#{i},#{len}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
             end
           end
-          if s.length < first
-            assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
-            next
+        end
+      else
+        assert_raise(ArgumentError) { t[i,len] = s2 }
+      end
+    }
+  end
+
+  def test_str_assign_substr
+    combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
+      t = s1.dup
+      encs = [
+        !is_ascii_only?(s1) ? s1.encoding : nil,
+        !is_ascii_only?(s2) ? s2.encoding : nil,
+        !is_ascii_only?(s3) ? s3.encoding : nil].uniq.compact
+      if 1 < encs.length
+        assert_raise(ArgumentError, IndexError) { t[s2] = s3 }
+      else
+        if encs.empty?
+          encs = [
+            s1.encoding,
+            s2.encoding,
+            s3.encoding].uniq.reject {|e| e == Encoding.find("ASCII-8BIT") }
+          if encs.empty?
+            encs = [Encoding.find("ASCII-8BIT")]
           end
-          if last < 0
-            last += s.length
-          end
-          t2 = ''
-          first.upto(last) {|i|
-            c = s[i]
-            t2 << c if c
-          }
-          assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
-        }
-      }
+        end
+        if !t[s2]
+        else
+          t[s2] = s3
+        end
+      end
     }
   end
 
-  def test_str_aref_range3
-    STRINGS.each {|s|
-      (-2).upto(2) {|first|
-        (-2).upto(2) {|last|
-          t = s[first...last]
-          if first < 0
-            first += s.length
+  def test_str_assign_range2
+    combination(STRINGS, -2..2, -2..2, STRINGS) {|s1, first, last, s2|
+      t = s1.dup
+      if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
+        if first < -s1.length || s1.length < first
+          assert_raise(RangeError) { t[first..last] = s2 }
+        else
+          t[first..last] = s2
+          assert(a(t).index(a(s2)))
+          if s1.valid_encoding? && s2.valid_encoding?
             if first < 0
-              assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
-              next
+              assert_equal(s2, t[s1.length+first, s2.length])
+            else
+              assert_equal(s2, t[first, s2.length])
             end
           end
-          if s.length < first
-            assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
-            next
-          end
-          if last < 0
-            last += s.length
-          end
-          t2 = ''
-          first.upto(last-1) {|i|
-            c = s[i]
-            t2 << c if c
-          }
-          assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
+        end
+      else
+        assert_raise(ArgumentError, RangeError,
+                     "t=#{encdump(s1)};t[#{first}..#{last}]=#{encdump(s2)}") {
+          t[first..last] = s2
         }
-      }
+      end
     }
   end
 
-  def encinsp(str)
-    "#{str.inspect}.force_encoding(#{str.encoding.name.inspect})"
-  end
-
-  def test_str_assign
-    STRINGS.each {|s1|
-      STRINGS.each {|s2|
-        (-2).upto(2) {|i|
-          t = s1.dup
-          if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
-            if i < -s1.length || s1.length < i
-              assert_raise(IndexError) { t[i] = s2 }
+  def test_str_assign_range3
+    combination(STRINGS, -2..2, -2..2, STRINGS) {|s1, first, last, s2|
+      t = s1.dup
+      if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
+        if first < -s1.length || s1.length < first
+          assert_raise(RangeError) { t[first...last] = s2 }
+        else
+          t[first...last] = s2
+          assert(a(t).index(a(s2)))
+          if s1.valid_encoding? && s2.valid_encoding?
+            if first < 0
+              assert_equal(s2, t[s1.length+first, s2.length])
             else
-              t[i] = s2
-              if !s1.valid_encoding? || !s2.valid_encoding?
-                assert(a(t).index(a(s2)))
-              else
-                if i == s1.length && s2.empty?
-                  assert_nil(t[i])
-                elsif i < 0
-                  assert_equal(s2, t[i-s2.length+1,s2.length],
-                    "t = #{encinsp(s1)}; t[#{i}] = #{encinsp(s2)}; t[#{i-s2.length+1},#{s2.length}]")
-                else
-                  assert_equal(s2, t[i,s2.length],
-                    "t = #{encinsp(s1)}; t[#{i}] = #{encinsp(s2)}; t[#{i},#{s2.length}]")
-                end
-              end
+              assert_equal(s2, t[first, s2.length])
             end
-          else
-            assert_raise(ArgumentError) { t[i] = s2 }
           end
+        end
+      else
+        assert_raise(ArgumentError, RangeError,
+                     "t=#{encdump(s1)};t[#{first}...#{last}]=#{encdump(s2)}") {
+          t[first...last] = s2
         }
-      }
+      end
     }
   end
 
+  def test_str_cmp
+    combination(STRINGS, STRINGS) {|s1, s2|
+      r = s1 <=> s2
+      if s1 == s2
+        assert_equal(0, r)
+      else
+        assert_not_equal(0, r)
+      end
+    }
+  end
+
+  def test_str_capitalize
+    STRINGS.each {|s|
+      begin
+        t1 = s.capitalize
+      rescue ArgumentError
+        assert(!s.valid_encoding?)
+        next
+      end
+      t2 = s.dup
+      t2.capitalize!
+      assert_equal(t1, t2)
+      assert_equal(s.downcase.sub(/\A[a-z]/) {|ch| a(ch).upcase }, t1)
+    }
+  end
+
+  def test_str_casecmp
+    combination(STRINGS, STRINGS) {|s1, s2|
+      #puts "#{encdump(s1)}.casecmp(#{encdump(s2)})"
+      begin
+        r = s1.casecmp(s2)
+      rescue ArgumentError
+        assert(!s1.valid_encoding? || !s2.valid_encoding?)
+        next
+      end
+      #assert_equal(s1.upcase <=> s2.upcase, r)
+    }
+  end
+
   def test_tr
     s = "\x81\x41".force_encoding("shift_jis")
     assert_equal(s.tr("A", "B"), s)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧]