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

* test/ruby/test_string.rb: add tests to achieve over 90% test

coverage of string.c.

* test/ruby/test_m17n.rb: ditto.

* test/ruby/test_symbol.rb: ditto.

* test/ruby/test_pack.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16427 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2008-05-15 14:37:18 +00:00
parent 41bf214fd7
commit b8118e9645
5 changed files with 288 additions and 5 deletions

View file

@ -1,3 +1,14 @@
Thu May 15 23:36:09 2008 Yusuke Endoh <mame@tsg.ne.jp>
* test/ruby/test_string.rb: add tests to achieve over 90% test
coverage of string.c.
* test/ruby/test_m17n.rb: ditto.
* test/ruby/test_symbol.rb: ditto.
* test/ruby/test_pack.rb: ditto.
Thu May 15 23:01:06 2008 Yusuke Endoh <mame@tsg.ne.jp>
* string.c (tr_find): String#delete returned wrong result when multiple

View file

@ -877,6 +877,9 @@ class TestM17N < Test::Unit::TestCase
}
assert_equal(e("\xA1\xA1"), a("a").tr(a("a"), e("\xA1\xA1")))
assert_equal("X\u3042\u3044X", "A\u3042\u3044\u3046".tr("^\u3042\u3044", "X"))
assert_equal("\u3042\u3046" * 100, ("\u3042\u3044" * 100).tr("\u3044", "\u3046"))
end
def test_tr_s
@ -968,6 +971,12 @@ class TestM17N < Test::Unit::TestCase
assert_equal(u("\xf0jihgfedcba"), u("abcdefghij\xf0").reverse)
end
def test_reverse_bang
s = u("abcdefghij\xf0")
s.reverse!
assert_equal(u("\xf0jihgfedcba"), s)
end
def test_plus
assert_raise(ArgumentError){u("\xe3\x81\x82") + a("\xa1")}
end
@ -1201,8 +1210,27 @@ class TestM17N < Test::Unit::TestCase
assert_equal(true, (s.dup << s).valid_encoding?)
assert_equal(true, "".center(2, s).valid_encoding?)
s = "\xa1\xa1\x8f".force_encoding("euc-jp")
assert_equal(false, s.valid_encoding?)
assert_equal(true, s.reverse.valid_encoding?)
end
s = "\xa1\xa1\x8f".force_encoding("euc-jp")
assert_equal(false, s.valid_encoding?)
assert_equal(true, s.reverse.valid_encoding?)
end
def test_getbyte
assert_equal(0x82, u("\xE3\x81\x82\xE3\x81\x84").getbyte(2))
assert_equal(0x82, u("\xE3\x81\x82\xE3\x81\x84").getbyte(-4))
assert_nil(u("\xE3\x81\x82\xE3\x81\x84").getbyte(100))
end
def test_setbyte
s = u("\xE3\x81\x82\xE3\x81\x84")
s.setbyte(2, 0x84)
assert_equal(u("\xE3\x81\x84\xE3\x81\x84"), s)
s = u("\xE3\x81\x82\xE3\x81\x84")
assert_raise(IndexError) { s.setbyte(100, 0) }
s = u("\xE3\x81\x82\xE3\x81\x84")
s.setbyte(-4, 0x84)
assert_equal(u("\xE3\x81\x84\xE3\x81\x84"), s)
end
end

View file

@ -63,6 +63,7 @@ class TestPack < Test::Unit::TestCase
assert_equal a, a.pack("P").unpack("P*")
assert_equal "a", a.pack("P").unpack("P")[0]
assert_equal a, a.pack("P").freeze.unpack("P*")
assert_raise(ArgumentError) { (a.pack("P") + "").unpack("P*") }
end
def test_pack_p
@ -70,6 +71,7 @@ class TestPack < Test::Unit::TestCase
assert_equal a, a.pack("p").unpack("p*")
assert_equal a[0], a.pack("p").unpack("p")[0]
assert_equal a, a.pack("p").freeze.unpack("p*")
assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") }
end
def test_format_string_modified
@ -429,4 +431,14 @@ class TestPack < Test::Unit::TestCase
assert_equal([0xffffffff], "\217\377\377\377\177".unpack("w"), [0xffffffff])
assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000])
end
def test_modify_under_safe4
s = "foo"
assert_raise(SecurityError) do
Thread.new do
$SAFE = 4
s.clear
end.join
end
end
end

View file

@ -57,6 +57,12 @@ class TestString < Test::Unit::TestCase
assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
end
o = Object.new
def o.to_int; 2; end
assert_equal("o", "foo"[o])
assert_raise(ArgumentError) { "foo"[] }
end
def test_ASET # '[]='
@ -136,6 +142,14 @@ class TestString < Test::Unit::TestCase
s = S("a string")
s[0..s.size] = S("another string")
assert_equal(S("another string"), s)
o = Object.new
def o.to_int; 2; end
s = "foo"
s[o] = "bar"
assert_equal("fobar", s)
assert_raise(ArgumentError) { "foo"[1, 2, 3] = "" }
end
def test_CMP # '<=>'
@ -150,6 +164,21 @@ class TestString < Test::Unit::TestCase
assert_equal(0, S("ABCDEF") <=> S("abcdef"))
$= = false
end
assert_nil("foo" <=> Object.new)
o = Object.new
def o.to_str; "bar"; end
assert_nil("foo" <=> o)
def o.<=>(x); nil; end
assert_nil("foo" <=> o)
def o.<=>(x); 1; end
assert_equal(-1, "foo" <=> o)
def o.<=>(x); 2**100; end
assert_equal(-(2**100), "foo" <=> o)
end
def test_EQUAL # '=='
@ -165,6 +194,13 @@ class TestString < Test::Unit::TestCase
assert(S("CAT") != S('cat'))
assert(S("CaT") != S('cAt'))
o = Object.new
def o.to_str; end
def o.==(x); false; end
assert_equal(false, "foo" == o)
def o.==(x); true; end
assert_equal(true, "foo" == o)
end
def test_LSHIFT # '<<'
@ -176,6 +212,11 @@ class TestString < Test::Unit::TestCase
s << s
assert_equal("a" * (2 << i), s)
}
s = ["foo"].pack("p")
l = s.size
s << "bar"
assert_equal(l + 3, s.size)
end
def test_MATCH # '=~'
@ -187,6 +228,12 @@ class TestString < Test::Unit::TestCase
assert_equal(10, S("FeeFieFoo-Fum") =~ /FUM$/)
$= = false
end
o = Object.new
def o.=~(x); x + "bar"; end
assert_equal("foobar", S("foo") =~ o)
assert_raise(TypeError) { S("foo") =~ "foo" }
end
def test_MOD # '%'
@ -329,7 +376,22 @@ class TestString < Test::Unit::TestCase
b = a.dup
assert_equal(S("hello"), a.chomp!)
assert_equal(S("hello\n"), b)
s = "foo\r\n"
s.chomp!
assert_equal("foo", s)
s = "foo\r"
s.chomp!
assert_equal("foo", s)
s = "foo\r\n"
s.chomp!("")
assert_equal("foo", s)
s = "foo\r"
s.chomp!("")
assert_equal("foo\r", s)
end
def test_chop
@ -393,6 +455,8 @@ class TestString < Test::Unit::TestCase
assert_equal(4, a.count(S("hello"), S("^l")))
assert_equal(4, a.count(S("ej-m")))
assert_equal(0, S("y").count(S("a\\-z")))
assert_raise(ArgumentError) { "foo".count }
end
def test_crypt
@ -432,6 +496,12 @@ class TestString < Test::Unit::TestCase
a.delete!(S("lo"))
assert_equal(S("he"), a)
assert_equal(S("hello"), b)
a = S("hello")
a.delete!(S("^el"))
assert_equal(S("ell"), a)
assert_raise(ArgumentError) { S("foo").delete! }
end
@ -525,6 +595,10 @@ class TestString < Test::Unit::TestCase
assert_equal(S("world"), res[1])
$/ = save
s = nil
"foo\nbar".each_line(nil) {|s2| s = s2 }
assert_equal("foo\nbar", s)
end
def test_empty?
@ -549,6 +623,8 @@ class TestString < Test::Unit::TestCase
a = S("hello")
a.taint
assert(a.gsub(/./, S('X')).tainted?)
assert_raise(ArgumentError) { "foo".gsub }
end
def test_gsub!
@ -637,6 +713,14 @@ class TestString < Test::Unit::TestCase
assert_nil(S("hello").index(?z))
assert_nil(S("hello").index(S("z")))
assert_nil(S("hello").index(/z./))
o = Object.new
def o.to_str; "bar"; end
assert_equal(3, "foobarbarbaz".index(o))
assert_raise(TypeError) { "foo".index(Object.new) }
assert_nil("foo".index(//, -100))
assert_nil($~)
end
def test_intern
@ -727,6 +811,15 @@ class TestString < Test::Unit::TestCase
b = a.replace(S("xyz"))
assert_equal(S("xyz"), b)
assert(b.tainted?)
s = "foo" * 100
s2 = ("bar" * 100).dup
s.replace(s2)
assert_equal(s2, s)
s2 = ["foo"].pack("p")
s.replace(s2)
assert_equal(s2, s)
end
def test_reverse
@ -768,6 +861,14 @@ class TestString < Test::Unit::TestCase
assert_nil(S("hello").rindex(?z))
assert_nil(S("hello").rindex(S("z")))
assert_nil(S("hello").rindex(/z./))
o = Object.new
def o.to_str; "bar"; end
assert_equal(6, "foobarbarbaz".rindex(o))
assert_raise(TypeError) { "foo".rindex(Object.new) }
assert_nil("foo".rindex(//, -100))
assert_nil($~)
end
def test_rjust
@ -935,6 +1036,8 @@ class TestString < Test::Unit::TestCase
assert_nil(a.slice!(S("plugh")))
assert_equal(S("FooBar"), a)
end
assert_raise(ArgumentError) { "foo".slice! }
end
def test_split
@ -958,6 +1061,8 @@ class TestString < Test::Unit::TestCase
assert_equal([S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|')))
assert_equal([S("a"), S(""), S("b"), S("c"), S("")], S("a||b|c|").split(S('|'), -1))
assert_equal([], "".split(//, 1))
end
def test_squeeze
@ -1048,6 +1153,20 @@ class TestString < Test::Unit::TestCase
a = S("hello")
a.taint
assert(a.sub(/./, S('X')).tainted?)
o = Object.new
def o.to_str; "bar"; end
assert_equal("fooBARbaz", "foobarbaz".sub(o, "BAR"))
assert_raise(TypeError) { "foo".sub(Object.new, "") }
assert_raise(ArgumentError) { "foo".sub }
assert_raise(IndexError) { "foo"[/(?:(o$)|(x))/, 2] = 'bar' }
o = Object.new
def o.to_s; self; end
assert_match(/^foo#<Object:0x.*>baz$/, "foobarbaz".sub("bar") { o })
end
def test_sub!
@ -1402,6 +1521,8 @@ class TestString < Test::Unit::TestCase
assert_equal(s1, s2)
s1 << 'a'
}
assert_raise(ArgumentError) { "foo" * (-1) }
end
def test_respond_to
@ -1411,4 +1532,72 @@ class TestString < Test::Unit::TestCase
def o.==(other) "" == other end
assert_equal(false, "" == o)
end
def test_match_method
assert_equal("bar", "foobarbaz".match(/bar/).to_s)
o = /foo/
def o.match(x, y, z); x + y + z; end
assert_equal("foobarbaz", "foo".match(o, "bar", "baz"))
x = nil
"foo".match(o, "bar", "baz") {|y| x = y }
assert_equal("foobarbaz", x)
assert_raise(ArgumentError) { "foo".match }
end
def test_clear
s = "foo" * 100
s.clear
assert_equal("", s)
end
def test_to_s_2
c = Class.new(String)
s = c.new
s.replace("foo")
assert_equal("foo", s.to_s)
assert_instance_of(String, s.to_s)
end
def test_partition
assert_equal(%w(he l lo), "hello".partition(/l/))
assert_equal(%w(he l lo), "hello".partition("l"))
assert_raise(TypeError) { "hello".partition(1) }
end
def test_rpartition
assert_equal(%w(hel l o), "hello".rpartition(/l/))
assert_equal(%w(hel l o), "hello".rpartition("l"))
assert_raise(TypeError) { "hello".rpartition(1) }
end
def test_setter
assert_raise(TypeError) { $/ = 1 }
end
def test_to_id
c = Class.new
c.class_eval do
def initialize
@foo = :foo
end
end
assert_raise(TypeError) do
c.class_eval { attr 1 }
end
o = Object.new
def o.to_str; :foo; end
assert_raise(TypeError) do
c.class_eval { attr 1 }
end
def o.to_str; "foo"; end
assert_nothing_raised do
c.class_eval { attr o }
end
assert_equal(:foo, c.new.foo)
end
end

View file

@ -89,4 +89,47 @@ class TestSymbol < Test::Unit::TestCase
assert_equal ary_ids, ary.collect(&:object_id)
end
end
def test_call
o = Object.new
def o.foo(x, y); x + y; end
assert_equal(3, :foo.to_proc.call(o, 1, 2))
assert_raise(ArgumentError) { :foo.to_proc.call }
end
def test_succ
assert_equal(:fop, :foo.succ)
end
def test_cmp
assert_equal(0, :FoO <=> :FoO)
assert_equal(-1, :FoO <=> :fOO)
assert_equal(1, :fOO <=> :FoO)
assert_nil(:foo <=> "foo")
end
def test_casecmp
assert_equal(0, :FoO.casecmp(:fOO))
assert_equal(1, :FoO.casecmp(:BaR))
assert_equal(-1, :baR.casecmp(:FoO))
assert_nil(:foo.casecmp("foo"))
end
def test_length
assert_equal(3, :FoO.length)
assert_equal(3, :FoO.size)
end
def test_empty
assert_equal(false, :FoO.empty?)
assert_equal(true, :"".empty?)
end
def test_case
assert_equal(:FOO, :FoO.upcase)
assert_equal(:foo, :FoO.downcase)
assert_equal(:Foo, :foo.capitalize)
assert_equal(:fOo, :FoO.swapcase)
end
end