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 # same byte sequence
a("\xc2\xa1"), e("\xc2\xa1"), s("\xc2\xa1"), u("\xc2\xa1"), 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 # invalid
e("\xa1"), e("\xa1"),
s("\x81"), s("\x81"),
u("\xc2"), 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 def test_str_new
STRINGS.each {|s| STRINGS.each {|s|
t = String.new(s) t = String.new(s)
@ -546,16 +562,14 @@ class TestM17N < Test::Unit::TestCase
end end
def test_str_plus def test_str_plus
STRINGS.each {|s1| combination(STRINGS, STRINGS) {|s1, s2|
STRINGS.each {|s2| if s1.encoding != s2.encoding && !is_ascii_only?(s1) && !is_ascii_only?(s2)
if s1.encoding != s2.encoding && !is_ascii_only?(s1) && !is_ascii_only?(s2) assert_raise(ArgumentError) { s1 + s2 }
assert_raise(ArgumentError) { s1 + s2 } else
else t = s1 + s2
t = s1 + s2 assert_equal(a(s1) + a(s2), a(t))
assert_equal(a(s1) + a(s2), a(t)) assert_str_enc_propagation(t, s1, s2)
assert_str_enc_propagation(t, s1, s2) end
end
}
} }
end end
@ -611,41 +625,37 @@ class TestM17N < Test::Unit::TestCase
end end
def test_str_eq def test_str_eq
STRINGS.each {|s1| combination(STRINGS, STRINGS) {|s1, s2|
STRINGS.each {|s2| if is_ascii_only?(s1) && is_ascii_only?(s2) && a(s1) == a(s2)
if is_ascii_only?(s1) && is_ascii_only?(s2) && a(s1) == a(s2) assert(s1 == s2)
assert(s1 == s2) elsif s1.encoding == s2.encoding && a(s1) == a(s2)
elsif s1.encoding == s2.encoding && a(s1) == a(s2) assert(s1 == s2)
assert(s1 == s2) assert(!(s1 != s2))
assert(!(s1 != s2)) assert_equal(0, s1 <=> s2)
assert_equal(0, s1 <=> s2) else
else assert(!(s1 == s2))
assert(!(s1 == s2)) assert(s1 != s2)
assert(s1 != s2) assert_not_equal(0, s1 <=> s2)
assert_not_equal(0, s1 <=> s2) end
end
}
} }
end end
def test_str_cmp def test_str_lt
assert(a("a") < a("\xa1")) assert(a("a") < a("\xa1"))
assert(a("a") < s("\xa1")) assert(a("a") < s("\xa1"))
assert(s("a") < a("\xa1")) assert(s("a") < a("\xa1"))
end end
def test_str_concat def test_str_concat
STRINGS.each {|s1| combination(STRINGS, STRINGS) {|s1, s2|
STRINGS.each {|s2| s = s1.dup
s = s1.dup if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding s << s2
s << s2 assert_equal(a(s), a(s1) + a(s2))
assert_equal(a(s), a(s1) + a(s2)) assert_str_enc_propagation(s, s1, s2)
assert_str_enc_propagation(s, s1, s2) else
else assert_raise(ArgumentError) { s << s2 }
assert_raise(ArgumentError) { s << s2 } end
end
}
} }
end end
@ -714,115 +724,249 @@ class TestM17N < Test::Unit::TestCase
assert_equal(nil, u("\xc2\xa1\xc2\xa2\xc2\xa3")[u("\xa1\xc2")]) 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")] } assert_raise(ArgumentError) { u("\xc2\xa1\xc2\xa2\xc2\xa3")[a("\xa1\xc2")] }
STRINGS.each {|s1| combination(STRINGS, STRINGS) {|s1, s2|
STRINGS.each {|s2| if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding
if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding t = s1[s2]
t = s1[s2] if t != nil
if t != nil assert_equal(s2, t)
assert_equal(s2, t) assert_match(/#{Regexp.escape(s2)}/, s1)
assert_match(/#{Regexp.escape(s2)}/, s1)
end
else
assert_raise(ArgumentError) { s1[s2] }
end end
else
} assert_raise(ArgumentError) { s1[s2] }
end
} }
end end
def test_str_aref_range2 def test_str_aref_range2
STRINGS.each {|s| combination(STRINGS, -2..2, -2..2) {|s, first, last|
(-2).upto(2) {|first| t = s[first..last]
(-2).upto(2) {|last| if first < 0
t = s[first..last] first += s.length
if first < 0 if first < 0
first += s.length assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
if first < 0 next
assert_nil(t, "#{s.inspect}[#{first}..#{last}]") end
next end
end if s.length < first
end assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
if s.length < first next
assert_nil(t, "#{s.inspect}[#{first}..#{last}]") end
next if last < 0
end last += s.length
if last < 0 end
last += s.length t2 = ''
end first.upto(last) {|i|
t2 = '' c = s[i]
first.upto(last) {|i| t2 << c if c
c = s[i]
t2 << c if c
}
assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
}
} }
assert_equal(t2, t, "#{s.inspect}[#{first}..#{last}]")
} }
end end
def test_str_aref_range3 def test_str_aref_range3
STRINGS.each {|s| combination(STRINGS, -2..2, -2..2) {|s, first, last|
(-2).upto(2) {|first| t = s[first...last]
(-2).upto(2) {|last| if first < 0
t = s[first...last] first += s.length
if first < 0 if first < 0
first += s.length assert_nil(t, "#{s.inspect}[#{first}..#{last}]")
if first < 0 next
assert_nil(t, "#{s.inspect}[#{first}..#{last}]") end
next 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
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
end end
if s.length < first else
assert_nil(t, "#{s.inspect}[#{first}..#{last}]") assert_raise(ArgumentError) { t[i] = s2 }
next end
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 end
def encinsp(str) def test_str_assign_len
"#{str.inspect}.force_encoding(#{str.encoding.name.inspect})" 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 end
def test_str_assign def test_str_assign_substr
STRINGS.each {|s1| combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
STRINGS.each {|s2| t = s1.dup
(-2).upto(2) {|i| encs = [
t = s1.dup !is_ascii_only?(s1) ? s1.encoding : nil,
if is_ascii_only?(s1) || is_ascii_only?(s2) || s1.encoding == s2.encoding !is_ascii_only?(s2) ? s2.encoding : nil,
if i < -s1.length || s1.length < i !is_ascii_only?(s3) ? s3.encoding : nil].uniq.compact
assert_raise(IndexError) { t[i] = s2 } if 1 < encs.length
else assert_raise(ArgumentError, IndexError) { t[s2] = s3 }
t[i] = s2 else
if !s1.valid_encoding? || !s2.valid_encoding? if encs.empty?
assert(a(t).index(a(s2))) encs = [
else s1.encoding,
if i == s1.length && s2.empty? s2.encoding,
assert_nil(t[i]) s3.encoding].uniq.reject {|e| e == Encoding.find("ASCII-8BIT") }
elsif i < 0 if encs.empty?
assert_equal(s2, t[i-s2.length+1,s2.length], encs = [Encoding.find("ASCII-8BIT")]
"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
end
else
assert_raise(ArgumentError) { t[i] = s2 }
end 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 end