summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-12-20 04:51:36 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-12-20 04:51:36 +0000
commit27fbeb3c4e8803d89fa1d765d7b1fdb49dddece2 (patch)
tree400db711b8d98e90cacd77cf555ccc2fa4e92e1f
parente0dcf23cd5973c9f1fdb7218cf3eac7774f1f0c2 (diff)
add m17n tests for scan, slice, slice!, split.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--test/ruby/test_m17n.rb145
1 files changed, 141 insertions, 4 deletions
diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb
index a0f3428c3d..b4852427f3 100644
--- a/test/ruby/test_m17n.rb
+++ b/test/ruby/test_m17n.rb
@@ -508,11 +508,12 @@ class TestM17N < Test::Unit::TestCase
STRINGS = [
a(""), e(""), s(""), u(""),
a("a"), e("a"), s("a"), u("a"),
+ a("."), e("."), s("."), u("."),
# single character
a("\x80"),
e("\xa1\xa1"), e("\x8e\xa1"), e("\x8f\xa1\xa1"),
- s("\x81\x40"), e("\xa1"),
+ s("\x81\x40"), s("\xa1"),
u("\xc2\x80"),
# same byte sequence
@@ -522,9 +523,9 @@ class TestM17N < Test::Unit::TestCase
s("\x81a"), # mutibyte character which contains "a"
# invalid
- e("\xa1"),
- s("\x81"),
- u("\xc2"),
+ e("\xa1"), e("\x80"),
+ s("\x81"), s("\x80"),
+ u("\xc2"), u("\x80"),
]
def combination(*args)
@@ -1378,6 +1379,142 @@ class TestM17N < Test::Unit::TestCase
}
end
+ def test_str_scan
+ combination(STRINGS, STRINGS) {|s1, s2|
+ if !s2.valid_encoding?
+ assert_raise(RegexpError) { s1.scan(s2) }
+ next
+ end
+ if !is_ascii_only?(s1) && !is_ascii_only?(s2) && s1.encoding != s2.encoding
+ assert_raise(ArgumentError) { s1.scan(s2) }
+ next
+ end
+ r = s1.scan(s2)
+ r.each {|t|
+ assert_equal(s2, t)
+ }
+ }
+ end
+
+ def assert_same_result(expected_proc, actual_proc)
+ e = nil
+ begin
+ t = expected_proc.call
+ rescue
+ e = $!
+ end
+ if e
+ assert_raise(e.class) { actual_proc.call }
+ else
+ assert_equal(t, actual_proc.call)
+ end
+ end
+
+ def each_slice_call
+ combination(STRINGS, -2..2) {|s, nth|
+ yield s, nth
+ }
+ combination(STRINGS, -2..2, 0..2) {|s, nth, len|
+ yield s, nth, len
+ }
+ combination(STRINGS, STRINGS) {|s, substr|
+ yield s, substr
+ }
+ combination(STRINGS, -2..2, 0..2) {|s, first, last|
+ yield s, first..last
+ yield s, first...last
+ }
+ combination(STRINGS, STRINGS) {|s1, s2|
+ if !s2.valid_encoding?
+ next
+ end
+ yield s1, Regexp.new(Regexp.escape(s2))
+ }
+ combination(STRINGS, STRINGS, 0..2) {|s1, s2, nth|
+ if !s2.valid_encoding?
+ next
+ end
+ yield s1, Regexp.new(Regexp.escape(s2)), nth
+ }
+ end
+
+ def test_str_slice
+ each_slice_call {|obj, *args|
+ assert_same_result(lambda { obj[*args] }, lambda { obj.slice(*args) })
+ }
+ end
+
+ def encdumpargs(args)
+ r = '('
+ args.each_with_index {|a, i|
+ r << ',' if 0 < i
+ if String === a
+ r << encdump(a)
+ else
+ r << a.inspect
+ end
+ }
+ r << ')'
+ r
+ end
+
+ def test_str_slice!
+ each_slice_call {|s, *args|
+ t = s.dup
+ begin
+ r = t.slice!(*args)
+ rescue
+ e = $!
+ end
+ if e
+ assert_raise(e.class) { s.slice(*args) }
+ next
+ end
+ if !r
+ assert_nil(s.slice(*args))
+ next
+ end
+ assert_equal(s.slice(*args), r)
+ assert_equal(s.bytesize, r.bytesize + t.bytesize)
+ if args.length == 1 && String === args[0]
+ assert_equal(args[0].encoding, r.encoding,
+ "#{encdump s}.slice!#{encdumpargs args}.encoding")
+ else
+ assert_equal(s.encoding, r.encoding,
+ "#{encdump s}.slice!#{encdumpargs args}.encoding")
+ end
+ if [s, *args].all? {|o| !(String === o) || o.valid_encoding? }
+ assert(r.valid_encoding?)
+ assert(t.valid_encoding?)
+ assert_equal(s.length, r.length + t.length)
+ end
+ }
+ end
+
+ def test_str_split
+ combination(STRINGS, STRINGS) {|s1, s2|
+ if !s2.valid_encoding?
+ assert_raise(RegexpError) { s1.split(s2) }
+ next
+ end
+ if !is_ascii_only?(s1) && !is_ascii_only?(s2) && s1.encoding != s2.encoding
+ assert_raise(ArgumentError) { s1.split(s2) }
+ next
+ end
+ t = s1.split(s2)
+ t.each {|r|
+ assert(a(s1).include?(a(r)))
+ assert_equal(s1.encoding, r.encoding)
+ }
+ assert(a(s1).include?(t.map {|u| a(u) }.join(a(s2))))
+ if s1.valid_encoding? && s2.valid_encoding?
+ t.each {|r|
+ assert(r.valid_encoding?)
+ }
+ end
+ }
+ end
+
def test_tr
s = "\x81\x41".force_encoding("shift_jis")
assert_equal(s.tr("A", "B"), s)