1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

more m17n tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14310 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2007-12-18 16:01:09 +00:00
parent ba15fda359
commit eddc9b7fd4

View file

@ -518,12 +518,28 @@ class TestM17N < Test::Unit::TestCase
# 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,8 +562,7 @@ class TestM17N < Test::Unit::TestCase
end
def test_str_plus
STRINGS.each {|s1|
STRINGS.each {|s2|
combination(STRINGS, STRINGS) {|s1, s2|
if s1.encoding != s2.encoding && !is_ascii_only?(s1) && !is_ascii_only?(s2)
assert_raise(ArgumentError) { s1 + s2 }
else
@ -556,7 +571,6 @@ class TestM17N < Test::Unit::TestCase
assert_str_enc_propagation(t, s1, s2)
end
}
}
end
def test_str_times
@ -611,8 +625,7 @@ class TestM17N < Test::Unit::TestCase
end
def test_str_eq
STRINGS.each {|s1|
STRINGS.each {|s2|
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)
@ -625,18 +638,16 @@ class TestM17N < Test::Unit::TestCase
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|
combination(STRINGS, STRINGS) {|s1, s2|
s = s1.dup
if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
s << s2
@ -646,7 +657,6 @@ class TestM17N < Test::Unit::TestCase
assert_raise(ArgumentError) { s << s2 }
end
}
}
end
def test_str_aref
@ -714,8 +724,7 @@ class TestM17N < Test::Unit::TestCase
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
@ -725,15 +734,11 @@ class TestM17N < Test::Unit::TestCase
else
assert_raise(ArgumentError) { s1[s2] }
end
}
}
end
def test_str_aref_range2
STRINGS.each {|s|
(-2).upto(2) {|first|
(-2).upto(2) {|last|
combination(STRINGS, -2..2, -2..2) {|s, first, last|
t = s[first..last]
if first < 0
first += s.length
@ -756,14 +761,10 @@ class TestM17N < Test::Unit::TestCase
}
assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
}
}
}
end
def test_str_aref_range3
STRINGS.each {|s|
(-2).upto(2) {|first|
(-2).upto(2) {|last|
combination(STRINGS, -2..2, -2..2) {|s, first, last|
t = s[first...last]
if first < 0
first += s.length
@ -786,17 +787,14 @@ class TestM17N < Test::Unit::TestCase
}
assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
}
}
}
end
def encinsp(str)
"#{str.inspect}.force_encoding(#{str.encoding.name.inspect})"
def encdump(str)
"#{str.dump}.force_encoding(#{str.encoding.name.dump})"
end
def test_str_assign
STRINGS.each {|s1|
STRINGS.each {|s2|
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
@ -804,17 +802,16 @@ class TestM17N < Test::Unit::TestCase
assert_raise(IndexError) { t[i] = s2 }
else
t[i] = s2
if !s1.valid_encoding? || !s2.valid_encoding?
assert(a(t).index(a(s2)))
else
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 = #{encinsp(s1)}; t[#{i}] = #{encinsp(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 = #{encinsp(s1)}; t[#{i}] = #{encinsp(s2)}; t[#{i},#{s2.length}]")
"t = #{encdump(s1)}; t[#{i}] = #{encdump(s2)}; t[#{i},#{s2.length}]")
end
end
end
@ -823,6 +820,153 @@ class TestM17N < Test::Unit::TestCase
end
}
}
end
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
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
end
if !t[s2]
else
t[s2] = s3
end
end
}
end
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_equal(s2, t[s1.length+first, s2.length])
else
assert_equal(s2, t[first, s2.length])
end
end
end
else
assert_raise(ArgumentError, RangeError,
"t=#{encdump(s1)};t[#{first}..#{last}]=#{encdump(s2)}") {
t[first..last] = s2
}
end
}
end
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
assert_equal(s2, t[first, s2.length])
end
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