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

more tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2007-12-18 05:02:24 +00:00
parent 1281cb00e5
commit 0c19240983
2 changed files with 126 additions and 71 deletions

View file

@ -260,4 +260,10 @@ assert_equal 'true', %q{
"\xa1\xa1".force_encoding("euc-jp") "\xa1\xa1".force_encoding("euc-jp")
} }
assert_equal 'a', %q{
s = "a".dup.force_encoding("EUC-JP")
s.length
s[0,2]
}

View file

@ -5,14 +5,19 @@ class TestM17N < Test::Unit::TestCase
assert_equal(Encoding.find(encname), actual, message) assert_equal(Encoding.find(encname), actual, message)
end end
def a(str) str.dup.force_encoding("ASCII-8BIT") end module AESU
def e(str) str.dup.force_encoding("EUC-JP") end def a(str) str.dup.force_encoding("ASCII-8BIT") end
def s(str) str.dup.force_encoding("Shift_JIS") end def e(str) str.dup.force_encoding("EUC-JP") end
def u(str) str.dup.force_encoding("UTF-8") end def s(str) str.dup.force_encoding("Shift_JIS") end
def u(str) str.dup.force_encoding("UTF-8") end
end
include AESU
extend AESU
def assert_strenc(bytes, encname, actual, message=nil) def assert_strenc(bytes, enc, actual, message=nil)
assert_instance_of(String, actual, message) assert_instance_of(String, actual, message)
assert_equal(Encoding.find(encname), actual.encoding, message) enc = Encoding.find(enc) if String === enc
assert_equal(enc, actual.encoding, message)
assert_equal(a(bytes), a(actual), message) assert_equal(a(bytes), a(actual), message)
end end
@ -500,34 +505,67 @@ class TestM17N < Test::Unit::TestCase
assert_nothing_raised { eval(u(%{/\\u{6666}#{}\\xc2\\xa0/})) } assert_nothing_raised { eval(u(%{/\\u{6666}#{}\\xc2\\xa0/})) }
end end
STRINGS = [
a(""), e(""), s(""), u(""),
a("a"), e("a"), s("a"), u("a"),
# single character
a("\x80"),
e("\xa1\xa1"), e("\x8e\xa1"), e("\x8f\xa1\xa1"),
s("\x81\x40"), e("\xa1"),
u("\xc2\x80"),
# same byte sequence
a("\xc2\xa1"), e("\xc2\xa1"), s("\xc2\xa1"), u("\xc2\xa1"),
# invalid
e("\xa1"),
s("\x81"),
u("\xc2"),
]
def test_str_new def test_str_new
assert_strenc('a', 'ASCII-8BIT', String.new(a("a"))) STRINGS.each {|s|
assert_strenc('a', 'EUC-JP', String.new(e("a"))) t = String.new(s)
assert_strenc('a', 'Shift_JIS', String.new(s("a"))) assert_strenc(a(s), s.encoding, t)
assert_strenc('a', 'UTF-8', String.new(u("a"))) }
assert_strenc("\xc2\xa1", 'ASCII-8BIT', String.new(a("\xc2\xa1"))) end
assert_strenc("\xc2\xa1", 'EUC-JP', String.new(e("\xc2\xa1")))
assert_strenc("\xc2\xa1", 'Shift_JIS', String.new(s("\xc2\xa1"))) def is_ascii_only?(str)
assert_strenc("\xc2\xa1", 'UTF-8', String.new(u("\xc2\xa1"))) /\A[\x00-\x7f]*\z/ =~ str.dup.force_encoding("ASCII-8BIT") ? true : false
end
def assert_str_enc_propagation(t, s1, s2)
if !is_ascii_only?(s1)
assert_equal(s1.encoding, t.encoding)
elsif !is_ascii_only?(s2)
assert_equal(s2.encoding, t.encoding)
else
assert([s1.encoding, s2.encoding].include?(t.encoding))
end
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
}
}
end end
def test_str_times def test_str_times
assert_strenc('', 'ASCII-8BIT', a("a") * 0) STRINGS.each {|s|
assert_strenc('', 'EUC-JP', e("a") * 0) [0,1,2].each {|n|
assert_strenc('', 'Shift_JIS', s("a") * 0) t = s * n
assert_strenc('', 'UTF-8', u("a") * 0) assert_strenc(a(s) * n, s.encoding, t)
assert_strenc('a', 'ASCII-8BIT', a("a") * 1) }
assert_strenc('a', 'EUC-JP', e("a") * 1) }
assert_strenc('a', 'Shift_JIS', s("a") * 1)
assert_strenc('a', 'UTF-8', u("a") * 1)
assert_strenc('aa', 'ASCII-8BIT', a("a") * 2)
assert_strenc('aa', 'EUC-JP', e("a") * 2)
assert_strenc('aa', 'Shift_JIS', s("a") * 2)
assert_strenc('aa', 'UTF-8', u("a") * 2)
assert_strenc("\xc2\xa1\xc2\xa1", 'ASCII-8BIT', a("\xc2\xa1") * 2)
assert_strenc("\xc2\xa1\xc2\xa1", 'EUC-JP', e("\xc2\xa1") * 2)
assert_strenc("\xc2\xa1\xc2\xa1", 'Shift_JIS', s("\xc2\xa1") * 2)
assert_strenc("\xc2\xa1\xc2\xa1", 'UTF-8', u("\xc2\xa1") * 2)
end end
def test_sprintf_c def test_sprintf_c
@ -542,17 +580,12 @@ class TestM17N < Test::Unit::TestCase
end end
def test_sprintf_s def test_sprintf_s
assert_strenc("", 'ASCII-8BIT', a("%s") % a("")) STRINGS.each {|s|
assert_strenc("", 'EUC-JP', e("%s") % e("")) assert_strenc(a(s), s.encoding, "%s".force_encoding(s.encoding) % s)
assert_strenc("", 'Shift_JIS', s("%s") % s("")) if !s.empty? # xxx
assert_strenc("", 'UTF-8', u("%s") % u("")) assert_strenc(a(s), s.encoding, a("%s") % s)
end
#assert_strenc("", 'EUC-JP', "%s" % e("")) }
assert_strenc("\xc2\xa1", 'ASCII-8BIT', a("%s") % a("\xc2\xa1"))
assert_strenc("\xc2\xa1", 'EUC-JP', "%s" % e("\xc2\xa1"))
assert_strenc("\xc2\xa1", 'Shift_JIS', "%s" % s("\xc2\xa1"))
assert_strenc("\xc2\xa1", 'UTF-8', "%s" % u("\xc2\xa1"))
end end
def test_sprintf_p def test_sprintf_p
@ -578,36 +611,18 @@ class TestM17N < Test::Unit::TestCase
end end
def test_str_eq def test_str_eq
empty = [a(""), e(""), s(""), u("")] STRINGS.each {|s1|
empty.each {|s1| STRINGS.each {|s2|
empty.each {|s2| if is_ascii_only?(s1) && is_ascii_only?(s2) && a(s1) == a(s2)
assert_equal(s1, s2) assert(s1 == s2)
assert(s1 == s2) elsif s1.encoding == s2.encoding && a(s1) == a(s2)
assert(!(s1 != s2))
assert(s1 <=> s2)
}
}
ascii = [a("a"), e("a"), s("a"), u("a")]
ascii.each {|s1|
ascii.each {|s2|
assert(s1 == s2)
assert(!(s1 != s2))
assert(s1 <=> s2)
}
}
nonascii = [a("\xc2\xa1"), e("\xc2\xa1"), s("\xc2\xa1"), u("\xc2\xa1")]
nonascii.each_with_index {|s1, i1|
nonascii.each_with_index {|s2, i2|
if i1 == i2
assert(s1 == s2) assert(s1 == s2)
assert(!(s1 != s2)) assert(!(s1 != s2))
assert(s1 <=> s2) assert_equal(0, s1 <=> s2)
else else
assert(s1 != s2)
assert(!(s1 == s2)) assert(!(s1 == s2))
assert(0 != (s1 <=> s2)) assert(s1 != s2)
assert_not_equal(0, s1 <=> s2)
end end
} }
} }
@ -620,10 +635,18 @@ class TestM17N < Test::Unit::TestCase
end end
def test_str_concat def test_str_concat
s = a("a") STRINGS.each {|s1|
s << e("\xc2\xa1") STRINGS.each {|s2|
assert_strenc("a\xc2\xa1", "EUC-JP", s) s = s1.dup
assert_raise(ArgumentError) { s << s("\xc2\xa1") } 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 end
def test_str_aref def test_str_aref
@ -637,6 +660,15 @@ class TestM17N < Test::Unit::TestCase
assert_equal(nil, s("\xc2\xa1")[2]) assert_equal(nil, s("\xc2\xa1")[2])
assert_equal(u("\xc2\xa1"), u("\xc2\xa1")[0]) assert_equal(u("\xc2\xa1"), u("\xc2\xa1")[0])
assert_equal(nil, u("\xc2\xa1")[1]) assert_equal(nil, u("\xc2\xa1")[1])
STRINGS.each {|s|
t = ''
0.upto(s.length-1) {|i|
t << s[i]
}
assert_equal(t, s)
}
end end
def test_str_aref_len def test_str_aref_len
@ -651,6 +683,23 @@ class TestM17N < Test::Unit::TestCase
assert_equal(u("\xc2\xa2"), u("\xc2\xa1\xc2\xa2\xc2\xa3")[1, 1]) assert_equal(u("\xc2\xa2"), u("\xc2\xa1\xc2\xa2\xc2\xa3")[1, 1])
assert_equal(u("\xc2\xa2\xc2\xa3"), u("\xc2\xa1\xc2\xa2\xc2\xa3")[1, 2]) assert_equal(u("\xc2\xa2\xc2\xa3"), u("\xc2\xa1\xc2\xa2\xc2\xa3")[1, 2])
STRINGS.each {|s|
t = ''
0.upto(s.length-1) {|i|
t << s[i,1]
}
assert_equal(t, s)
}
STRINGS.each {|s|
t = ''
0.step(s.length-1, 2) {|i|
p [s,s.encoding,i]
t << s[i,2]
}
assert_equal(t, s)
}
end end
def test_str_aref_substr def test_str_aref_substr