2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2004-10-03 02:02:55 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestString < Test::Unit::TestCase
|
2013-02-25 03:10:44 -05:00
|
|
|
ENUMERATOR_WANTARRAY = RUBY_VERSION >= "3.0.0"
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
@cls = String
|
|
|
|
@aref_re_nth = true
|
|
|
|
@aref_re_silent = false
|
|
|
|
@aref_slicebang_silent = true
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-12-08 11:48:52 -05:00
|
|
|
def S(*args)
|
|
|
|
@cls.new(*args)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new
|
2015-12-08 11:48:52 -05:00
|
|
|
assert_equal("", S())
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, S().encoding)
|
|
|
|
|
|
|
|
assert_equal("", S(""))
|
|
|
|
assert_equal(__ENCODING__, S("").encoding)
|
|
|
|
|
|
|
|
src = "RUBY"
|
|
|
|
assert_equal(src, S(src))
|
|
|
|
assert_equal(__ENCODING__, S(src).encoding)
|
|
|
|
|
|
|
|
src.force_encoding("euc-jp")
|
|
|
|
assert_equal(src, S(src))
|
|
|
|
assert_equal(Encoding::EUC_JP, S(src).encoding)
|
|
|
|
|
|
|
|
|
|
|
|
assert_equal("", S(encoding: "euc-jp"))
|
|
|
|
assert_equal(Encoding::EUC_JP, S(encoding: "euc-jp").encoding)
|
|
|
|
|
|
|
|
assert_equal("", S("", encoding: "euc-jp"))
|
|
|
|
assert_equal(Encoding::EUC_JP, S("", encoding: "euc-jp").encoding)
|
|
|
|
|
|
|
|
src = "RUBY"
|
|
|
|
assert_equal(src, S(src, encoding: "euc-jp"))
|
|
|
|
assert_equal(Encoding::EUC_JP, S(src, encoding: "euc-jp").encoding)
|
|
|
|
|
|
|
|
src.force_encoding("euc-jp")
|
|
|
|
assert_equal(src, S(src, encoding: "utf-8"))
|
|
|
|
assert_equal(Encoding::UTF_8, S(src, encoding: "utf-8").encoding)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_AREF # '[]'
|
|
|
|
assert_equal("A", S("AooBar")[0])
|
|
|
|
assert_equal("B", S("FooBaB")[-1])
|
|
|
|
assert_equal(nil, S("FooBar")[6])
|
|
|
|
assert_equal(nil, S("FooBar")[-7])
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[0,3])
|
|
|
|
assert_equal(S("Bar"), S("FooBar")[-3,3])
|
|
|
|
assert_equal(S(""), S("FooBar")[6,2])
|
|
|
|
assert_equal(nil, S("FooBar")[-7,10])
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[0..2])
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[0...3])
|
|
|
|
assert_equal(S("Bar"), S("FooBar")[-3..-1])
|
|
|
|
assert_equal(S(""), S("FooBar")[6..2])
|
|
|
|
assert_equal(nil, S("FooBar")[-10..-7])
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[/^F../])
|
|
|
|
assert_equal(S("Bar"), S("FooBar")[/..r$/])
|
|
|
|
assert_equal(nil, S("FooBar")[/xyzzy/])
|
|
|
|
assert_equal(nil, S("FooBar")[/plugh/])
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[S("Foo")])
|
|
|
|
assert_equal(S("Bar"), S("FooBar")[S("Bar")])
|
|
|
|
assert_equal(nil, S("FooBar")[S("xyzzy")])
|
|
|
|
assert_equal(nil, S("FooBar")[S("plugh")])
|
|
|
|
|
|
|
|
if @aref_re_nth
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1])
|
|
|
|
assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2])
|
|
|
|
assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3])
|
|
|
|
assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
|
|
|
|
assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
|
|
|
|
assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
|
|
|
|
end
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.to_int; 2; end
|
|
|
|
assert_equal("o", "foo"[o])
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { "foo"[] }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ASET # '[]='
|
|
|
|
s = S("FooBar")
|
|
|
|
s[0] = S('A')
|
|
|
|
assert_equal(S("AooBar"), s)
|
|
|
|
|
|
|
|
s[-1]= S('B')
|
|
|
|
assert_equal(S("AooBaB"), s)
|
|
|
|
assert_raise(IndexError) { s[-7] = S("xyz") }
|
|
|
|
assert_equal(S("AooBaB"), s)
|
|
|
|
s[0] = S("ABC")
|
|
|
|
assert_equal(S("ABCooBaB"), s)
|
|
|
|
|
|
|
|
s = S("FooBar")
|
|
|
|
s[0,3] = S("A")
|
|
|
|
assert_equal(S("ABar"),s)
|
|
|
|
s[0] = S("Foo")
|
|
|
|
assert_equal(S("FooBar"), s)
|
|
|
|
s[-3,3] = S("Foo")
|
|
|
|
assert_equal(S("FooFoo"), s)
|
2007-02-08 08:28:03 -05:00
|
|
|
assert_raise(IndexError) { s[7,3] = S("Bar") }
|
|
|
|
assert_raise(IndexError) { s[-7,3] = S("Bar") }
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
s = S("FooBar")
|
|
|
|
s[0..2] = S("A")
|
|
|
|
assert_equal(S("ABar"), s)
|
|
|
|
s[1..3] = S("Foo")
|
|
|
|
assert_equal(S("AFoo"), s)
|
|
|
|
s[-4..-4] = S("Foo")
|
|
|
|
assert_equal(S("FooFoo"), s)
|
2007-02-08 08:28:03 -05:00
|
|
|
assert_raise(RangeError) { s[7..10] = S("Bar") }
|
|
|
|
assert_raise(RangeError) { s[-7..-10] = S("Bar") }
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
s = S("FooBar")
|
|
|
|
s[/^F../]= S("Bar")
|
|
|
|
assert_equal(S("BarBar"), s)
|
|
|
|
s[/..r$/] = S("Foo")
|
|
|
|
assert_equal(S("BarFoo"), s)
|
|
|
|
if @aref_re_silent
|
|
|
|
s[/xyzzy/] = S("None")
|
|
|
|
assert_equal(S("BarFoo"), s)
|
|
|
|
else
|
2007-02-08 08:28:03 -05:00
|
|
|
assert_raise(IndexError) { s[/xyzzy/] = S("None") }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
if @aref_re_nth
|
|
|
|
s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo")
|
|
|
|
assert_equal(S("FooFoo"), s)
|
|
|
|
s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar")
|
|
|
|
assert_equal(S("FooBar"), s)
|
2007-02-08 08:28:03 -05:00
|
|
|
assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" }
|
2006-12-31 10:02:22 -05:00
|
|
|
s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo")
|
|
|
|
assert_equal(S("FooFoo"), s)
|
|
|
|
s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar")
|
|
|
|
assert_equal(S("BarFoo"), s)
|
2007-02-08 08:28:03 -05:00
|
|
|
assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
s = S("FooBar")
|
|
|
|
s[S("Foo")] = S("Bar")
|
|
|
|
assert_equal(S("BarBar"), s)
|
|
|
|
|
|
|
|
s = S("a string")
|
|
|
|
s[0..s.size] = S("another string")
|
|
|
|
assert_equal(S("another string"), s)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
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] = "" }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_CMP # '<=>'
|
|
|
|
assert_equal(1, S("abcdef") <=> S("abcde"))
|
|
|
|
assert_equal(0, S("abcdef") <=> S("abcdef"))
|
|
|
|
assert_equal(-1, S("abcde") <=> S("abcdef"))
|
|
|
|
|
|
|
|
assert_equal(-1, S("ABCDEF") <=> S("abcdef"))
|
|
|
|
|
2008-05-15 10:37:18 -04:00
|
|
|
assert_nil("foo" <=> Object.new)
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.to_str; "bar"; end
|
2012-11-30 03:43:52 -05:00
|
|
|
assert_equal(1, "foo" <=> o)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
2012-11-30 03:43:52 -05:00
|
|
|
class << o;remove_method :to_str;end
|
2008-05-15 10:37:18 -04:00
|
|
|
def o.<=>(x); nil; end
|
|
|
|
assert_nil("foo" <=> o)
|
|
|
|
|
2010-10-13 15:17:49 -04:00
|
|
|
class << o;remove_method :<=>;end
|
2008-05-15 10:37:18 -04:00
|
|
|
def o.<=>(x); 1; end
|
|
|
|
assert_equal(-1, "foo" <=> o)
|
|
|
|
|
2010-10-13 15:17:49 -04:00
|
|
|
class << o;remove_method :<=>;end
|
2008-05-15 10:37:18 -04:00
|
|
|
def o.<=>(x); 2**100; end
|
2012-11-30 03:43:42 -05:00
|
|
|
assert_equal(-1, "foo" <=> o)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_EQUAL # '=='
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_equal(:foo, S("foo"))
|
|
|
|
assert_equal(S("abcdef"), S("abcdef"))
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_equal(S("CAT"), S('cat'))
|
|
|
|
assert_not_equal(S("CaT"), S('cAt'))
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.to_str; end
|
|
|
|
def o.==(x); false; end
|
|
|
|
assert_equal(false, "foo" == o)
|
2010-10-13 15:17:49 -04:00
|
|
|
class << o;remove_method :==;end
|
2008-05-15 10:37:18 -04:00
|
|
|
def o.==(x); true; end
|
|
|
|
assert_equal(true, "foo" == o)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_LSHIFT # '<<'
|
|
|
|
assert_equal(S("world!"), S("world") << 33)
|
2012-10-11 02:50:26 -04:00
|
|
|
assert_equal(S("world!"), S("world") << S("!"))
|
2008-01-07 17:57:00 -05:00
|
|
|
|
|
|
|
s = "a"
|
|
|
|
10.times {|i|
|
|
|
|
s << s
|
2008-02-16 19:18:16 -05:00
|
|
|
assert_equal("a" * (2 << i), s)
|
2008-01-07 17:57:00 -05:00
|
|
|
}
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
s = ["foo"].pack("p")
|
|
|
|
l = s.size
|
|
|
|
s << "bar"
|
|
|
|
assert_equal(l + 3, s.size)
|
2010-01-13 22:50:39 -05:00
|
|
|
|
|
|
|
bug = '[ruby-core:27583]'
|
|
|
|
assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -3}
|
|
|
|
assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -2}
|
|
|
|
assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << -1}
|
|
|
|
assert_raise(RangeError, bug) {S("a".force_encoding(Encoding::UTF_8)) << 0x81308130}
|
|
|
|
assert_nothing_raised {S("a".force_encoding(Encoding::GB18030)) << 0x81308130}
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_MATCH # '=~'
|
|
|
|
assert_equal(10, S("FeeFieFoo-Fum") =~ /Fum$/)
|
|
|
|
assert_equal(nil, S("FeeFieFoo-Fum") =~ /FUM$/)
|
|
|
|
|
2008-05-15 10:37:18 -04:00
|
|
|
o = Object.new
|
|
|
|
def o.=~(x); x + "bar"; end
|
|
|
|
assert_equal("foobar", S("foo") =~ o)
|
|
|
|
|
|
|
|
assert_raise(TypeError) { S("foo") =~ "foo" }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_MOD # '%'
|
|
|
|
assert_equal(S("00123"), S("%05d") % 123)
|
|
|
|
assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1])
|
|
|
|
x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") %
|
|
|
|
[S("hi"),
|
|
|
|
123,
|
|
|
|
S("never seen"),
|
|
|
|
456,
|
|
|
|
0,
|
|
|
|
?A,
|
|
|
|
3.0999,
|
|
|
|
11,
|
|
|
|
171,
|
|
|
|
171,
|
|
|
|
11,
|
|
|
|
171,
|
|
|
|
171]
|
|
|
|
|
2008-02-29 20:22:45 -05:00
|
|
|
assert_equal(S(' hi 123 %foo 456 0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_MUL # '*'
|
|
|
|
assert_equal(S("XXX"), S("X") * 3)
|
|
|
|
assert_equal(S("HOHO"), S("HO") * 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_PLUS # '+'
|
|
|
|
assert_equal(S("Yodel"), S("Yo") + S("del"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def casetest(a, b, rev=false)
|
2013-11-22 01:32:34 -05:00
|
|
|
msg = proc {"#{a} should#{' not' if rev} match #{b}"}
|
2006-12-31 10:02:22 -05:00
|
|
|
case a
|
2015-10-23 03:03:34 -04:00
|
|
|
when b
|
|
|
|
assert(!rev, msg)
|
|
|
|
else
|
|
|
|
assert(rev, msg)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_VERY_EQUAL # '==='
|
2007-02-08 08:28:03 -05:00
|
|
|
# assert_equal(true, S("foo") === :foo)
|
2006-12-31 10:02:22 -05:00
|
|
|
casetest(S("abcdef"), S("abcdef"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to
|
|
|
|
casetest(S("CaT"), S('cAt'), true) # find these in the case.
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_capitalize
|
|
|
|
assert_equal(S("Hello"), S("hello").capitalize)
|
|
|
|
assert_equal(S("Hello"), S("hELLO").capitalize)
|
|
|
|
assert_equal(S("123abc"), S("123ABC").capitalize)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_capitalize!
|
|
|
|
a = S("hello"); a.capitalize!
|
|
|
|
assert_equal(S("Hello"), a)
|
|
|
|
|
|
|
|
a = S("hELLO"); a.capitalize!
|
|
|
|
assert_equal(S("Hello"), a)
|
|
|
|
|
|
|
|
a = S("123ABC"); a.capitalize!
|
|
|
|
assert_equal(S("123abc"), a)
|
|
|
|
|
|
|
|
assert_equal(nil, S("123abc").capitalize!)
|
|
|
|
assert_equal(S("123abc"), S("123ABC").capitalize!)
|
|
|
|
assert_equal(S("Abc"), S("ABC").capitalize!)
|
|
|
|
assert_equal(S("Abc"), S("abc").capitalize!)
|
|
|
|
assert_equal(nil, S("Abc").capitalize!)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("Hello"), a.capitalize!)
|
|
|
|
assert_equal(S("hello"), b)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2009-12-08 19:50:37 -05:00
|
|
|
Bug2463 = '[ruby-dev:39856]'
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_center
|
|
|
|
assert_equal(S("hello"), S("hello").center(4))
|
|
|
|
assert_equal(S(" hello "), S("hello").center(11))
|
2009-12-08 19:50:37 -05:00
|
|
|
assert_equal(S("ababaababa"), S("").center(10, "ab"), Bug2463)
|
|
|
|
assert_equal(S("ababaababab"), S("").center(11, "ab"), Bug2463)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_chomp
|
|
|
|
assert_equal(S("hello"), S("hello").chomp("\n"))
|
|
|
|
assert_equal(S("hello"), S("hello\n").chomp("\n"))
|
2008-02-29 08:33:26 -05:00
|
|
|
save = $/
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
$/ = "\n"
|
|
|
|
|
|
|
|
assert_equal(S("hello"), S("hello").chomp)
|
|
|
|
assert_equal(S("hello"), S("hello\n").chomp)
|
|
|
|
|
|
|
|
$/ = "!"
|
|
|
|
assert_equal(S("hello"), S("hello").chomp)
|
|
|
|
assert_equal(S("hello"), S("hello!").chomp)
|
2008-02-29 08:33:26 -05:00
|
|
|
$/ = save
|
2009-02-24 12:28:56 -05:00
|
|
|
|
|
|
|
assert_equal(S("a").hash, S("a\u0101").chomp(S("\u0101")).hash, '[ruby-core:22414]')
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_chomp!
|
|
|
|
a = S("hello")
|
|
|
|
a.chomp!(S("\n"))
|
|
|
|
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
assert_equal(nil, a.chomp!(S("\n")))
|
|
|
|
|
|
|
|
a = S("hello\n")
|
|
|
|
a.chomp!(S("\n"))
|
|
|
|
assert_equal(S("hello"), a)
|
2008-02-29 08:33:26 -05:00
|
|
|
save = $/
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
$/ = "\n"
|
|
|
|
a = S("hello")
|
|
|
|
a.chomp!
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
|
|
|
|
a = S("hello\n")
|
|
|
|
a.chomp!
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
|
|
|
|
$/ = "!"
|
|
|
|
a = S("hello")
|
|
|
|
a.chomp!
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
|
|
|
|
a="hello!"
|
|
|
|
a.chomp!
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
|
2008-02-29 08:33:26 -05:00
|
|
|
$/ = save
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
a = S("hello\n")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("hello"), a.chomp!)
|
|
|
|
assert_equal(S("hello\n"), b)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
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)
|
2009-02-24 12:28:56 -05:00
|
|
|
|
|
|
|
assert_equal(S("a").hash, S("a\u0101").chomp!(S("\u0101")).hash, '[ruby-core:22414]')
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_chop
|
|
|
|
assert_equal(S("hell"), S("hello").chop)
|
|
|
|
assert_equal(S("hello"), S("hello\r\n").chop)
|
|
|
|
assert_equal(S("hello\n"), S("hello\n\r").chop)
|
|
|
|
assert_equal(S(""), S("\r\n").chop)
|
|
|
|
assert_equal(S(""), S("").chop)
|
2009-04-08 06:48:52 -04:00
|
|
|
assert_equal(S("a").hash, S("a\u00d8").chop.hash)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_chop!
|
|
|
|
a = S("hello").chop!
|
|
|
|
assert_equal(S("hell"), a)
|
|
|
|
|
|
|
|
a = S("hello\r\n").chop!
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
|
|
|
|
a = S("hello\n\r").chop!
|
|
|
|
assert_equal(S("hello\n"), a)
|
|
|
|
|
|
|
|
a = S("\r\n").chop!
|
|
|
|
assert_equal(S(""), a)
|
|
|
|
|
|
|
|
a = S("").chop!
|
|
|
|
assert_nil(a)
|
|
|
|
|
2009-04-08 06:48:52 -04:00
|
|
|
a = S("a\u00d8")
|
|
|
|
a.chop!
|
|
|
|
assert_equal(S("a").hash, a.hash)
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
a = S("hello\n")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("hello"), a.chop!)
|
|
|
|
assert_equal(S("hello\n"), b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clone
|
|
|
|
for taint in [ false, true ]
|
2013-06-13 01:41:44 -04:00
|
|
|
for frozen in [ false, true ]
|
|
|
|
a = S("Cool")
|
|
|
|
a.taint if taint
|
|
|
|
a.freeze if frozen
|
|
|
|
b = a.clone
|
|
|
|
|
|
|
|
assert_equal(a, b)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_same(a, b)
|
2013-06-13 01:41:44 -04:00
|
|
|
assert_equal(a.frozen?, b.frozen?)
|
|
|
|
assert_equal(a.tainted?, b.tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
end
|
2007-12-31 09:27:20 -05:00
|
|
|
|
2015-08-27 05:05:48 -04:00
|
|
|
assert_equal("", File.read(IO::NULL).clone, '[ruby-dev:32819] reported by Kazuhiro NISHIYAMA')
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_concat
|
|
|
|
assert_equal(S("world!"), S("world").concat(33))
|
|
|
|
assert_equal(S("world!"), S("world").concat(S('!')))
|
2012-09-30 08:20:09 -04:00
|
|
|
|
|
|
|
bug7090 = '[ruby-core:47751]'
|
|
|
|
result = S("").force_encoding(Encoding::UTF_16LE)
|
|
|
|
result << 0x0300
|
|
|
|
expected = S("\u0300".encode(Encoding::UTF_16LE))
|
|
|
|
assert_equal(expected, result, bug7090)
|
2013-11-26 22:28:48 -05:00
|
|
|
assert_raise(TypeError) { 'foo' << :foo }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_count
|
|
|
|
a = S("hello world")
|
|
|
|
assert_equal(5, a.count(S("lo")))
|
|
|
|
assert_equal(2, a.count(S("lo"), S("o")))
|
|
|
|
assert_equal(4, a.count(S("hello"), S("^l")))
|
|
|
|
assert_equal(4, a.count(S("ej-m")))
|
2008-01-01 02:04:28 -05:00
|
|
|
assert_equal(0, S("y").count(S("a\\-z")))
|
2010-08-30 02:01:14 -04:00
|
|
|
assert_equal(5, "abc\u{3042 3044 3046}".count("^a"))
|
2012-03-25 05:20:04 -04:00
|
|
|
assert_equal(1, "abc\u{3042 3044 3046}".count("\u3042"))
|
2010-08-30 02:01:14 -04:00
|
|
|
assert_equal(5, "abc\u{3042 3044 3046}".count("^\u3042"))
|
2010-09-01 03:52:16 -04:00
|
|
|
assert_equal(2, "abc\u{3042 3044 3046}".count("a-z", "^a"))
|
2012-03-25 05:20:04 -04:00
|
|
|
assert_equal(0, "abc\u{3042 3044 3046}".count("a", "\u3042"))
|
|
|
|
assert_equal(0, "abc\u{3042 3044 3046}".count("\u3042", "a"))
|
|
|
|
assert_equal(0, "abc\u{3042 3044 3046}".count("\u3042", "\u3044"))
|
|
|
|
assert_equal(4, "abc\u{3042 3044 3046}".count("^a", "^\u3044"))
|
|
|
|
assert_equal(4, "abc\u{3042 3044 3046}".count("^\u3044", "^a"))
|
|
|
|
assert_equal(4, "abc\u{3042 3044 3046}".count("^\u3042", "^\u3044"))
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) { "foo".count }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_crypt
|
|
|
|
assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa")))
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("ab")))
|
2014-12-11 20:49:20 -05:00
|
|
|
assert_raise(ArgumentError) {S("mypassword").crypt(S(""))}
|
|
|
|
assert_raise(ArgumentError) {S("mypassword").crypt(S("\0a"))}
|
|
|
|
assert_raise(ArgumentError) {S("mypassword").crypt(S("a\0"))}
|
2015-05-09 22:49:01 -04:00
|
|
|
assert_raise(ArgumentError) {S("poison\u0000null").crypt(S("aa"))}
|
2014-12-11 20:49:20 -05:00
|
|
|
[Encoding::UTF_16BE, Encoding::UTF_16LE,
|
|
|
|
Encoding::UTF_32BE, Encoding::UTF_32LE].each do |enc|
|
|
|
|
assert_raise(ArgumentError) {S("mypassword").crypt(S("aa".encode(enc)))}
|
|
|
|
assert_raise(ArgumentError) {S("mypassword".encode(enc)).crypt(S("aa"))}
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete
|
|
|
|
assert_equal(S("heo"), S("hello").delete(S("l"), S("lo")))
|
|
|
|
assert_equal(S("he"), S("hello").delete(S("lo")))
|
|
|
|
assert_equal(S("hell"), S("hello").delete(S("aeiou"), S("^e")))
|
|
|
|
assert_equal(S("ho"), S("hello").delete(S("ej-m")))
|
2009-02-24 12:17:35 -05:00
|
|
|
|
|
|
|
assert_equal("a".hash, "a\u0101".delete("\u0101").hash, '[ruby-talk:329267]')
|
|
|
|
assert_equal(true, "a\u0101".delete("\u0101").ascii_only?)
|
|
|
|
assert_equal(true, "a\u3041".delete("\u3041").ascii_only?)
|
|
|
|
assert_equal(false, "a\u3041\u3042".tr("\u3041", "a").ascii_only?)
|
2010-08-30 02:01:14 -04:00
|
|
|
|
|
|
|
assert_equal("a", "abc\u{3042 3044 3046}".delete("^a"))
|
2010-08-30 15:45:30 -04:00
|
|
|
assert_equal("bc\u{3042 3044 3046}", "abc\u{3042 3044 3046}".delete("a"))
|
2010-08-30 02:01:14 -04:00
|
|
|
assert_equal("\u3042", "abc\u{3042 3044 3046}".delete("^\u3042"))
|
2012-03-16 12:49:44 -04:00
|
|
|
|
|
|
|
bug6160 = '[ruby-dev:45374]'
|
|
|
|
assert_equal("", '\\'.delete('\\'), bug6160)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete!
|
|
|
|
a = S("hello")
|
|
|
|
a.delete!(S("l"), S("lo"))
|
|
|
|
assert_equal(S("heo"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.delete!(S("lo"))
|
|
|
|
assert_equal(S("he"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.delete!(S("aeiou"), S("^e"))
|
|
|
|
assert_equal(S("hell"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.delete!(S("ej-m"))
|
|
|
|
assert_equal(S("ho"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
assert_nil(a.delete!(S("z")))
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
a.delete!(S("lo"))
|
|
|
|
assert_equal(S("he"), a)
|
|
|
|
assert_equal(S("hello"), b)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.delete!(S("^el"))
|
|
|
|
assert_equal(S("ell"), a)
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) { S("foo").delete! }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_downcase
|
|
|
|
assert_equal(S("hello"), S("helLO").downcase)
|
|
|
|
assert_equal(S("hello"), S("hello").downcase)
|
|
|
|
assert_equal(S("hello"), S("HELLO").downcase)
|
|
|
|
assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_downcase!
|
|
|
|
a = S("helLO")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("hello"), a.downcase!)
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
assert_equal(S("helLO"), b)
|
|
|
|
|
|
|
|
a=S("hello")
|
|
|
|
assert_nil(a.downcase!)
|
|
|
|
assert_equal(S("hello"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dump
|
|
|
|
a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10
|
2007-12-09 16:48:05 -05:00
|
|
|
assert_equal(S('"Test\\x01\\x02\\x03\\t\\r\\n"'), a.dump)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dup
|
|
|
|
for taint in [ false, true ]
|
2013-06-13 01:41:44 -04:00
|
|
|
for frozen in [ false, true ]
|
|
|
|
a = S("hello")
|
|
|
|
a.taint if taint
|
|
|
|
a.freeze if frozen
|
|
|
|
b = a.dup
|
|
|
|
|
|
|
|
assert_equal(a, b)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_same(a, b)
|
|
|
|
assert_not_predicate(b, :frozen?)
|
2013-06-13 01:41:44 -04:00
|
|
|
assert_equal(a.tainted?, b.tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
2008-02-29 08:33:26 -05:00
|
|
|
save = $/
|
2006-12-31 10:02:22 -05:00
|
|
|
$/ = "\n"
|
|
|
|
res=[]
|
|
|
|
S("hello\nworld").lines.each {|x| res << x}
|
|
|
|
assert_equal(S("hello\n"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
res=[]
|
|
|
|
S("hello\n\n\nworld").lines(S('')).each {|x| res << x}
|
|
|
|
assert_equal(S("hello\n\n\n"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
$/ = "!"
|
|
|
|
res=[]
|
|
|
|
S("hello!world").lines.each {|x| res << x}
|
|
|
|
assert_equal(S("hello!"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
2008-02-29 08:33:26 -05:00
|
|
|
$/ = save
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_byte
|
2012-11-24 13:46:15 -05:00
|
|
|
s = S("ABC")
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
res = []
|
2012-11-24 13:46:15 -05:00
|
|
|
assert_equal s.object_id, s.each_byte {|x| res << x }.object_id
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(65, res[0])
|
|
|
|
assert_equal(66, res[1])
|
|
|
|
assert_equal(67, res[2])
|
2012-11-24 13:46:15 -05:00
|
|
|
|
|
|
|
assert_equal 65, s.each_byte.next
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bytes
|
|
|
|
s = S("ABC")
|
|
|
|
assert_equal [65, 66, 67], s.bytes
|
|
|
|
|
2013-02-25 03:10:40 -05:00
|
|
|
if ENUMERATOR_WANTARRAY
|
2012-11-24 13:46:15 -05:00
|
|
|
assert_warn(/block not used/) {
|
|
|
|
assert_equal [65, 66, 67], s.bytes {}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert_warning(/deprecated/) {
|
|
|
|
res = []
|
|
|
|
assert_equal s.object_id, s.bytes {|x| res << x }.object_id
|
|
|
|
assert_equal(65, res[0])
|
|
|
|
assert_equal(66, res[1])
|
|
|
|
assert_equal(67, res[2])
|
|
|
|
}
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2012-10-11 02:50:31 -04:00
|
|
|
def test_each_codepoint
|
2012-11-24 13:46:15 -05:00
|
|
|
# Single byte optimization
|
|
|
|
assert_equal 65, S("ABC").each_codepoint.next
|
|
|
|
|
|
|
|
s = S("\u3042\u3044\u3046")
|
|
|
|
|
2012-10-11 02:50:31 -04:00
|
|
|
res = []
|
2012-11-24 13:46:15 -05:00
|
|
|
assert_equal s.object_id, s.each_codepoint {|x| res << x }.object_id
|
|
|
|
assert_equal(0x3042, res[0])
|
|
|
|
assert_equal(0x3044, res[1])
|
|
|
|
assert_equal(0x3046, res[2])
|
|
|
|
|
|
|
|
assert_equal 0x3042, s.each_codepoint.next
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_codepoints
|
|
|
|
# Single byte optimization
|
|
|
|
assert_equal [65, 66, 67], S("ABC").codepoints
|
|
|
|
|
|
|
|
s = S("\u3042\u3044\u3046")
|
|
|
|
assert_equal [0x3042, 0x3044, 0x3046], s.codepoints
|
|
|
|
|
2013-02-25 03:10:40 -05:00
|
|
|
if ENUMERATOR_WANTARRAY
|
2012-11-24 13:46:15 -05:00
|
|
|
assert_warn(/block not used/) {
|
|
|
|
assert_equal [0x3042, 0x3044, 0x3046], s.codepoints {}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert_warning(/deprecated/) {
|
|
|
|
res = []
|
|
|
|
assert_equal s.object_id, s.codepoints {|x| res << x }.object_id
|
|
|
|
assert_equal(0x3042, res[0])
|
|
|
|
assert_equal(0x3044, res[1])
|
|
|
|
assert_equal(0x3046, res[2])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_char
|
|
|
|
s = S("ABC")
|
|
|
|
|
|
|
|
res = []
|
|
|
|
assert_equal s.object_id, s.each_char {|x| res << x }.object_id
|
|
|
|
assert_equal("A", res[0])
|
|
|
|
assert_equal("B", res[1])
|
|
|
|
assert_equal("C", res[2])
|
|
|
|
|
|
|
|
assert_equal "A", S("ABC").each_char.next
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chars
|
|
|
|
s = S("ABC")
|
|
|
|
assert_equal ["A", "B", "C"], s.chars
|
|
|
|
|
2013-02-25 03:10:40 -05:00
|
|
|
if ENUMERATOR_WANTARRAY
|
2012-11-24 13:46:15 -05:00
|
|
|
assert_warn(/block not used/) {
|
|
|
|
assert_equal ["A", "B", "C"], s.chars {}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert_warning(/deprecated/) {
|
|
|
|
res = []
|
|
|
|
assert_equal s.object_id, s.chars {|x| res << x }.object_id
|
|
|
|
assert_equal("A", res[0])
|
|
|
|
assert_equal("B", res[1])
|
|
|
|
assert_equal("C", res[2])
|
|
|
|
}
|
|
|
|
end
|
2012-10-11 02:50:31 -04:00
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_each_line
|
2008-02-29 08:33:26 -05:00
|
|
|
save = $/
|
2006-12-31 10:02:22 -05:00
|
|
|
$/ = "\n"
|
|
|
|
res=[]
|
2012-11-24 13:46:15 -05:00
|
|
|
S("hello\nworld").each_line {|x| res << x}
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(S("hello\n"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
|
|
|
|
|
|
|
res=[]
|
2012-11-24 13:46:15 -05:00
|
|
|
S("hello\n\n\nworld").each_line(S('')) {|x| res << x}
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(S("hello\n\n\n"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
$/ = "!"
|
|
|
|
|
|
|
|
res=[]
|
2012-11-24 13:46:15 -05:00
|
|
|
S("hello!world").each_line {|x| res << x}
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(S("hello!"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-08-06 20:43:44 -04:00
|
|
|
$/ = "ab"
|
|
|
|
|
|
|
|
res=[]
|
|
|
|
S("a").lines.each {|x| res << x}
|
|
|
|
assert_equal(1, res.size)
|
|
|
|
assert_equal(S("a"), res[0])
|
|
|
|
|
2008-02-29 08:33:26 -05:00
|
|
|
$/ = save
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
s = nil
|
|
|
|
"foo\nbar".each_line(nil) {|s2| s = s2 }
|
|
|
|
assert_equal("foo\nbar", s)
|
2012-11-24 13:46:15 -05:00
|
|
|
|
|
|
|
assert_equal "hello\n", S("hello\nworld").each_line.next
|
|
|
|
assert_equal "hello\nworld", S("hello\nworld").each_line(nil).next
|
2013-01-04 23:45:32 -05:00
|
|
|
|
|
|
|
bug7646 = "[ruby-dev:46827]"
|
|
|
|
assert_nothing_raised(bug7646) do
|
|
|
|
"\n\u0100".each_line("\n") {}
|
|
|
|
end
|
2012-11-24 13:46:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_lines
|
|
|
|
s = S("hello\nworld")
|
|
|
|
assert_equal ["hello\n", "world"], s.lines
|
|
|
|
assert_equal ["hello\nworld"], s.lines(nil)
|
|
|
|
|
2013-02-25 03:10:40 -05:00
|
|
|
if ENUMERATOR_WANTARRAY
|
2012-11-24 13:46:15 -05:00
|
|
|
assert_warn(/block not used/) {
|
|
|
|
assert_equal ["hello\n", "world"], s.lines {}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert_warning(/deprecated/) {
|
|
|
|
res = []
|
|
|
|
assert_equal s.object_id, s.lines {|x| res << x }.object_id
|
|
|
|
assert_equal(S("hello\n"), res[0])
|
|
|
|
assert_equal(S("world"), res[1])
|
|
|
|
}
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty?
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_empty(S(""))
|
|
|
|
assert_not_empty(S("not"))
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2012-04-01 21:34:11 -04:00
|
|
|
def test_end_with?
|
|
|
|
assert_send([S("hello"), :end_with?, S("llo")])
|
|
|
|
assert_not_send([S("hello"), :end_with?, S("ll")])
|
|
|
|
assert_send([S("hello"), :end_with?, S("el"), S("lo")])
|
|
|
|
|
|
|
|
bug5536 = '[ruby-core:40623]'
|
|
|
|
assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string}
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_eql?
|
|
|
|
a = S("hello")
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_operator(a, :eql?, S("hello"))
|
|
|
|
assert_operator(a, :eql?, a)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gsub
|
|
|
|
assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*')))
|
|
|
|
assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>')))
|
|
|
|
assert_equal(S("h e l l o "),
|
|
|
|
S("hello").gsub(/./) { |s| s[0].to_s + S(' ')})
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_equal(S("HELL-o"),
|
2006-12-31 10:02:22 -05:00
|
|
|
S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 })
|
2014-03-27 22:28:16 -04:00
|
|
|
assert_equal(S("<>h<>e<>l<>l<>o<>"), S("hello").gsub(S(''), S('<\0>')))
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.taint
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_predicate(a.gsub(/./, S('X')), :tainted?)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
2008-05-19 01:20:21 -04:00
|
|
|
assert_equal("z", "abc".gsub(/./, "a" => "z"), "moved from btest/knownbug")
|
|
|
|
|
2008-05-15 10:37:18 -04:00
|
|
|
assert_raise(ArgumentError) { "foo".gsub }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2011-02-05 22:54:50 -05:00
|
|
|
def test_gsub_encoding
|
|
|
|
a = S("hello world")
|
|
|
|
a.force_encoding Encoding::UTF_8
|
|
|
|
|
|
|
|
b = S("hi")
|
|
|
|
b.force_encoding Encoding::US_ASCII
|
|
|
|
|
|
|
|
assert_equal Encoding::UTF_8, a.gsub(/hello/, b).encoding
|
|
|
|
|
|
|
|
c = S("everybody")
|
|
|
|
c.force_encoding Encoding::US_ASCII
|
|
|
|
|
|
|
|
assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding
|
2014-03-27 05:58:12 -04:00
|
|
|
|
|
|
|
assert_equal S("a\u{e9}apos<"), S("a\u{e9}'<").gsub("'", "apos")
|
2014-05-19 03:29:51 -04:00
|
|
|
|
|
|
|
bug9849 = '[ruby-core:62669] [Bug #9849]'
|
|
|
|
assert_equal S("\u{3042 3042 3042}!foo!"), S("\u{3042 3042 3042}/foo/").gsub("/", "!"), bug9849
|
2011-02-05 22:54:50 -05:00
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_gsub!
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
a.gsub!(/[aeiou]/, S('*'))
|
|
|
|
assert_equal(S("h*ll*"), a)
|
|
|
|
assert_equal(S("hello"), b)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.gsub!(/([aeiou])/, S('<\1>'))
|
|
|
|
assert_equal(S("h<e>ll<o>"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.gsub!(/./) { |s| s[0].to_s + S(' ')}
|
|
|
|
assert_equal(S("h e l l o "), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
|
|
|
|
assert_equal(S("HELL-o"), a)
|
|
|
|
|
|
|
|
r = S('X')
|
|
|
|
r.taint
|
|
|
|
a.gsub!(/./, r)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_predicate(a, :tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
assert_nil(a.sub!(S('X'), S('Y')))
|
|
|
|
end
|
|
|
|
|
2008-02-17 22:51:34 -05:00
|
|
|
def test_sub_hash
|
|
|
|
assert_equal('azc', 'abc'.sub(/b/, "b" => "z"))
|
|
|
|
assert_equal('ac', 'abc'.sub(/b/, {}))
|
|
|
|
assert_equal('a1c', 'abc'.sub(/b/, "b" => 1))
|
|
|
|
assert_equal('aBc', 'abc'.sub(/b/, Hash.new {|h, k| k.upcase }))
|
2008-02-18 01:18:14 -05:00
|
|
|
assert_equal('a[\&]c', 'abc'.sub(/b/, "b" => '[\&]'))
|
2008-02-17 22:51:34 -05:00
|
|
|
assert_equal('aBcabc', 'abcabc'.sub(/b/, Hash.new {|h, k| h[k] = k.upcase }))
|
|
|
|
assert_equal('aBcdef', 'abcdef'.sub(/de|b/, "b" => "B", "de" => "DE"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gsub_hash
|
|
|
|
assert_equal('azc', 'abc'.gsub(/b/, "b" => "z"))
|
|
|
|
assert_equal('ac', 'abc'.gsub(/b/, {}))
|
|
|
|
assert_equal('a1c', 'abc'.gsub(/b/, "b" => 1))
|
|
|
|
assert_equal('aBc', 'abc'.gsub(/b/, Hash.new {|h, k| k.upcase }))
|
2008-02-18 01:18:14 -05:00
|
|
|
assert_equal('a[\&]c', 'abc'.gsub(/b/, "b" => '[\&]'))
|
2008-02-17 22:51:34 -05:00
|
|
|
assert_equal('aBcaBc', 'abcabc'.gsub(/b/, Hash.new {|h, k| h[k] = k.upcase }))
|
|
|
|
assert_equal('aBcDEf', 'abcdef'.gsub(/de|b/, "b" => "B", "de" => "DE"))
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_hash
|
|
|
|
assert_equal(S("hello").hash, S("hello").hash)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_equal(S("hello").hash, S("helLO").hash)
|
2010-12-01 00:36:39 -05:00
|
|
|
bug4104 = '[ruby-core:33500]'
|
|
|
|
assert_not_equal(S("a").hash, S("a\0").hash, bug4104)
|
2013-11-29 23:28:15 -05:00
|
|
|
bug9172 = '[ruby-core:58658] [Bug #9172]'
|
|
|
|
assert_not_equal(S("sub-setter").hash, S("discover").hash, bug9172)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2008-06-19 22:46:17 -04:00
|
|
|
def test_hash_random
|
|
|
|
str = 'abc'
|
|
|
|
a = [str.hash.to_s]
|
|
|
|
3.times {
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(["-e", "print #{str.dump}.hash"], "") do |r, e|
|
|
|
|
a += r
|
|
|
|
assert_equal([], e)
|
|
|
|
end
|
2008-06-19 22:46:17 -04:00
|
|
|
}
|
|
|
|
assert_not_equal([str.hash.to_s], a.uniq)
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_hex
|
|
|
|
assert_equal(255, S("0xff").hex)
|
|
|
|
assert_equal(-255, S("-0xff").hex)
|
|
|
|
assert_equal(255, S("ff").hex)
|
|
|
|
assert_equal(-255, S("-ff").hex)
|
|
|
|
assert_equal(0, S("-ralph").hex)
|
|
|
|
assert_equal(-15, S("-fred").hex)
|
|
|
|
assert_equal(15, S("fred").hex)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include?
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_include(S("foobar"), ?f)
|
|
|
|
assert_include(S("foobar"), S("foo"))
|
|
|
|
assert_not_include(S("foobar"), S("baz"))
|
|
|
|
assert_not_include(S("foobar"), ?z)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_index
|
|
|
|
assert_equal(0, S("hello").index(?h))
|
|
|
|
assert_equal(1, S("hello").index(S("ell")))
|
|
|
|
assert_equal(2, S("hello").index(/ll./))
|
|
|
|
|
|
|
|
assert_equal(3, S("hello").index(?l, 3))
|
|
|
|
assert_equal(3, S("hello").index(S("l"), 3))
|
|
|
|
assert_equal(3, S("hello").index(/l./, 3))
|
|
|
|
|
|
|
|
assert_nil(S("hello").index(?z, 3))
|
|
|
|
assert_nil(S("hello").index(S("z"), 3))
|
|
|
|
assert_nil(S("hello").index(/z./, 3))
|
|
|
|
|
|
|
|
assert_nil(S("hello").index(?z))
|
|
|
|
assert_nil(S("hello").index(S("z")))
|
|
|
|
assert_nil(S("hello").index(/z./))
|
2008-05-15 10:37:18 -04:00
|
|
|
|
2012-11-22 00:02:30 -05:00
|
|
|
assert_equal(0, S("").index(S("")))
|
|
|
|
assert_equal(0, S("").index(//))
|
|
|
|
assert_nil(S("").index(S("hello")))
|
|
|
|
assert_nil(S("").index(/hello/))
|
|
|
|
assert_equal(0, S("hello").index(S("")))
|
|
|
|
assert_equal(0, S("hello").index(//))
|
|
|
|
|
2012-11-22 00:23:12 -05:00
|
|
|
s = S("long") * 1000 << "x"
|
|
|
|
assert_nil(s.index(S("y")))
|
|
|
|
assert_equal(4 * 1000, s.index(S("x")))
|
|
|
|
s << "yx"
|
|
|
|
assert_equal(4 * 1000, s.index(S("x")))
|
|
|
|
assert_equal(4 * 1000, s.index(S("xyx")))
|
|
|
|
|
2008-05-15 10:37:18 -04:00
|
|
|
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($~)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2015-12-26 21:47:49 -05:00
|
|
|
def test_insert
|
|
|
|
assert_equal("Xabcd", S("abcd").insert(0, 'X'))
|
|
|
|
assert_equal("abcXd", S("abcd").insert(3, 'X'))
|
|
|
|
assert_equal("abcdX", S("abcd").insert(4, 'X'))
|
|
|
|
assert_equal("abXcd", S("abcd").insert(-3, 'X'))
|
|
|
|
assert_equal("abcdX", S("abcd").insert(-1, 'X'))
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_intern
|
|
|
|
assert_equal(:koala, S("koala").intern)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_equal(:koala, S("Koala").intern)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_length
|
|
|
|
assert_equal(0, S("").length)
|
|
|
|
assert_equal(4, S("1234").length)
|
|
|
|
assert_equal(6, S("1234\r\n").length)
|
|
|
|
assert_equal(7, S("\0011234\r\n").length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ljust
|
|
|
|
assert_equal(S("hello"), S("hello").ljust(4))
|
|
|
|
assert_equal(S("hello "), S("hello").ljust(11))
|
2009-12-08 19:50:37 -05:00
|
|
|
assert_equal(S("ababababab"), S("").ljust(10, "ab"), Bug2463)
|
|
|
|
assert_equal(S("abababababa"), S("").ljust(11, "ab"), Bug2463)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_next
|
|
|
|
assert_equal(S("abd"), S("abc").next)
|
|
|
|
assert_equal(S("z"), S("y").next)
|
|
|
|
assert_equal(S("aaa"), S("zz").next)
|
|
|
|
|
|
|
|
assert_equal(S("124"), S("123").next)
|
|
|
|
assert_equal(S("1000"), S("999").next)
|
|
|
|
|
|
|
|
assert_equal(S("2000aaa"), S("1999zzz").next)
|
|
|
|
assert_equal(S("AAAAA000"), S("ZZZZ999").next)
|
|
|
|
|
|
|
|
assert_equal(S("*+"), S("**").next)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_next!
|
|
|
|
a = S("abc")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("abd"), a.next!)
|
|
|
|
assert_equal(S("abd"), a)
|
|
|
|
assert_equal(S("abc"), b)
|
|
|
|
|
|
|
|
a = S("y")
|
|
|
|
assert_equal(S("z"), a.next!)
|
|
|
|
assert_equal(S("z"), a)
|
|
|
|
|
|
|
|
a = S("zz")
|
|
|
|
assert_equal(S("aaa"), a.next!)
|
|
|
|
assert_equal(S("aaa"), a)
|
|
|
|
|
|
|
|
a = S("123")
|
|
|
|
assert_equal(S("124"), a.next!)
|
|
|
|
assert_equal(S("124"), a)
|
|
|
|
|
|
|
|
a = S("999")
|
|
|
|
assert_equal(S("1000"), a.next!)
|
|
|
|
assert_equal(S("1000"), a)
|
|
|
|
|
|
|
|
a = S("1999zzz")
|
|
|
|
assert_equal(S("2000aaa"), a.next!)
|
|
|
|
assert_equal(S("2000aaa"), a)
|
|
|
|
|
|
|
|
a = S("ZZZZ999")
|
|
|
|
assert_equal(S("AAAAA000"), a.next!)
|
|
|
|
assert_equal(S("AAAAA000"), a)
|
|
|
|
|
|
|
|
a = S("**")
|
|
|
|
assert_equal(S("*+"), a.next!)
|
|
|
|
assert_equal(S("*+"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_oct
|
|
|
|
assert_equal(255, S("0377").oct)
|
|
|
|
assert_equal(255, S("377").oct)
|
|
|
|
assert_equal(-255, S("-0377").oct)
|
|
|
|
assert_equal(-255, S("-377").oct)
|
|
|
|
assert_equal(0, S("OO").oct)
|
|
|
|
assert_equal(24, S("030OO").oct)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace
|
|
|
|
a = S("foo")
|
|
|
|
assert_equal(S("f"), a.replace(S("f")))
|
|
|
|
|
|
|
|
a = S("foo")
|
|
|
|
assert_equal(S("foobar"), a.replace(S("foobar")))
|
|
|
|
|
|
|
|
a = S("foo")
|
|
|
|
a.taint
|
|
|
|
b = a.replace(S("xyz"))
|
|
|
|
assert_equal(S("xyz"), b)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_predicate(b, :tainted?)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
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)
|
2010-02-10 02:31:06 -05:00
|
|
|
|
|
|
|
fs = "".freeze
|
|
|
|
assert_raise(RuntimeError) { fs.replace("a") }
|
|
|
|
assert_raise(RuntimeError) { fs.replace(fs) }
|
|
|
|
assert_raise(ArgumentError) { fs.replace() }
|
|
|
|
assert_raise(RuntimeError) { fs.replace(42) }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reverse
|
|
|
|
assert_equal(S("beta"), S("ateb").reverse)
|
|
|
|
assert_equal(S("madamImadam"), S("madamImadam").reverse)
|
|
|
|
|
|
|
|
a=S("beta")
|
|
|
|
assert_equal(S("ateb"), a.reverse)
|
|
|
|
assert_equal(S("beta"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reverse!
|
|
|
|
a = S("beta")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("ateb"), a.reverse!)
|
|
|
|
assert_equal(S("ateb"), a)
|
|
|
|
assert_equal(S("beta"), b)
|
|
|
|
|
|
|
|
assert_equal(S("madamImadam"), S("madamImadam").reverse!)
|
|
|
|
|
|
|
|
a = S("madamImadam")
|
|
|
|
assert_equal(S("madamImadam"), a.reverse!) # ??
|
|
|
|
assert_equal(S("madamImadam"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rindex
|
|
|
|
assert_equal(3, S("hello").rindex(?l))
|
|
|
|
assert_equal(6, S("ell, hello").rindex(S("ell")))
|
|
|
|
assert_equal(7, S("ell, hello").rindex(/ll./))
|
|
|
|
|
|
|
|
assert_equal(3, S("hello,lo").rindex(?l, 3))
|
|
|
|
assert_equal(3, S("hello,lo").rindex(S("l"), 3))
|
|
|
|
assert_equal(3, S("hello,lo").rindex(/l./, 3))
|
|
|
|
|
|
|
|
assert_nil(S("hello").rindex(?z, 3))
|
|
|
|
assert_nil(S("hello").rindex(S("z"), 3))
|
|
|
|
assert_nil(S("hello").rindex(/z./, 3))
|
|
|
|
|
|
|
|
assert_nil(S("hello").rindex(?z))
|
|
|
|
assert_nil(S("hello").rindex(S("z")))
|
|
|
|
assert_nil(S("hello").rindex(/z./))
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
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($~)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rjust
|
|
|
|
assert_equal(S("hello"), S("hello").rjust(4))
|
|
|
|
assert_equal(S(" hello"), S("hello").rjust(11))
|
2009-12-08 19:50:37 -05:00
|
|
|
assert_equal(S("ababababab"), S("").rjust(10, "ab"), Bug2463)
|
|
|
|
assert_equal(S("abababababa"), S("").rjust(11, "ab"), Bug2463)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scan
|
|
|
|
a = S("cruel world")
|
|
|
|
assert_equal([S("cruel"), S("world")],a.scan(/\w+/))
|
|
|
|
assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../))
|
|
|
|
assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/))
|
|
|
|
|
|
|
|
res = []
|
|
|
|
a.scan(/\w+/) { |w| res << w }
|
|
|
|
assert_equal([S("cruel"), S("world") ],res)
|
|
|
|
|
|
|
|
res = []
|
|
|
|
a.scan(/.../) { |w| res << w }
|
|
|
|
assert_equal([S("cru"), S("el "), S("wor")],res)
|
|
|
|
|
|
|
|
res = []
|
|
|
|
a.scan(/(...)/) { |w| res << w }
|
|
|
|
assert_equal([[S("cru")], [S("el ")], [S("wor")]],res)
|
2010-12-06 05:01:45 -05:00
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.taint
|
|
|
|
res = []
|
|
|
|
a.scan(/./) { |w| res << w }
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_predicate(res[0], :tainted?, '[ruby-core:33338] #4087')
|
2014-03-27 22:27:14 -04:00
|
|
|
|
|
|
|
/h/ =~ a
|
|
|
|
a.scan(/x/)
|
|
|
|
assert_nil($~)
|
|
|
|
|
|
|
|
/h/ =~ a
|
|
|
|
a.scan('x')
|
|
|
|
assert_nil($~)
|
2014-03-27 22:28:18 -04:00
|
|
|
|
|
|
|
assert_equal(3, S("hello hello hello").scan("hello".taint).count(&:tainted?))
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_size
|
|
|
|
assert_equal(0, S("").size)
|
|
|
|
assert_equal(4, S("1234").size)
|
|
|
|
assert_equal(6, S("1234\r\n").size)
|
|
|
|
assert_equal(7, S("\0011234\r\n").size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_slice
|
|
|
|
assert_equal(?A, S("AooBar").slice(0))
|
|
|
|
assert_equal(?B, S("FooBaB").slice(-1))
|
|
|
|
assert_nil(S("FooBar").slice(6))
|
|
|
|
assert_nil(S("FooBar").slice(-7))
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar").slice(0,3))
|
|
|
|
assert_equal(S(S("Bar")), S("FooBar").slice(-3,3))
|
|
|
|
assert_nil(S("FooBar").slice(7,2)) # Maybe should be six?
|
|
|
|
assert_nil(S("FooBar").slice(-7,10))
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar").slice(0..2))
|
|
|
|
assert_equal(S("Bar"), S("FooBar").slice(-3..-1))
|
|
|
|
assert_equal(S(""), S("FooBar").slice(6..2))
|
|
|
|
assert_nil(S("FooBar").slice(-10..-7))
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar").slice(/^F../))
|
|
|
|
assert_equal(S("Bar"), S("FooBar").slice(/..r$/))
|
|
|
|
assert_nil(S("FooBar").slice(/xyzzy/))
|
|
|
|
assert_nil(S("FooBar").slice(/plugh/))
|
|
|
|
|
|
|
|
assert_equal(S("Foo"), S("FooBar").slice(S("Foo")))
|
|
|
|
assert_equal(S("Bar"), S("FooBar").slice(S("Bar")))
|
|
|
|
assert_nil(S("FooBar").slice(S("xyzzy")))
|
|
|
|
assert_nil(S("FooBar").slice(S("plugh")))
|
2014-05-29 22:55:26 -04:00
|
|
|
|
|
|
|
bug9882 = '[ruby-core:62842] [Bug #9882]'
|
|
|
|
substr = S("\u{30c6 30b9 30c8 2019}#{bug9882}").slice(4..-1)
|
|
|
|
assert_equal(S(bug9882).hash, substr.hash, bug9882)
|
|
|
|
assert_predicate(substr, :ascii_only?, bug9882)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_slice!
|
|
|
|
a = S("AooBar")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(?A, a.slice!(0))
|
|
|
|
assert_equal(S("ooBar"), a)
|
|
|
|
assert_equal(S("AooBar"), b)
|
|
|
|
|
|
|
|
a = S("FooBar")
|
|
|
|
assert_equal(?r,a.slice!(-1))
|
|
|
|
assert_equal(S("FooBa"), a)
|
|
|
|
|
|
|
|
a = S("FooBar")
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_nil( a.slice!(6) )
|
|
|
|
else
|
|
|
|
assert_raise(IndexError) { a.slice!(6) }
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
|
|
|
|
if @aref_slicebang_silent
|
2009-03-05 22:56:38 -05:00
|
|
|
assert_nil( a.slice!(-7) )
|
|
|
|
else
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_raise(IndexError) { a.slice!(-7) }
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
|
|
|
|
a = S("FooBar")
|
|
|
|
assert_equal(S("Foo"), a.slice!(0,3))
|
|
|
|
assert_equal(S("Bar"), a)
|
|
|
|
|
|
|
|
a = S("FooBar")
|
|
|
|
assert_equal(S("Bar"), a.slice!(-3,3))
|
|
|
|
assert_equal(S("Foo"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_nil(a.slice!(7,2)) # Maybe should be six?
|
|
|
|
else
|
|
|
|
assert_raise(IndexError) {a.slice!(7,2)} # Maybe should be six?
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_nil(a.slice!(-7,10))
|
|
|
|
else
|
|
|
|
assert_raise(IndexError) {a.slice!(-7,10)}
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
assert_equal(S("Foo"), a.slice!(0..2))
|
|
|
|
assert_equal(S("Bar"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
assert_equal(S("Bar"), a.slice!(-3..-1))
|
|
|
|
assert_equal(S("Foo"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_equal(S(""), a.slice!(6..2))
|
|
|
|
else
|
|
|
|
assert_raise(RangeError) {a.slice!(6..2)}
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_nil(a.slice!(-10..-7))
|
|
|
|
else
|
|
|
|
assert_raise(RangeError) {a.slice!(-10..-7)}
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
assert_equal(S("Foo"), a.slice!(/^F../))
|
|
|
|
assert_equal(S("Bar"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
assert_equal(S("Bar"), a.slice!(/..r$/))
|
|
|
|
assert_equal(S("Foo"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_nil(a.slice!(/xyzzy/))
|
|
|
|
else
|
|
|
|
assert_raise(IndexError) {a.slice!(/xyzzy/)}
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
if @aref_slicebang_silent
|
|
|
|
assert_nil(a.slice!(/plugh/))
|
|
|
|
else
|
|
|
|
assert_raise(IndexError) {a.slice!(/plugh/)}
|
|
|
|
end
|
|
|
|
assert_equal(S("FooBar"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
assert_equal(S("Foo"), a.slice!(S("Foo")))
|
|
|
|
assert_equal(S("Bar"), a)
|
|
|
|
|
|
|
|
a=S("FooBar")
|
|
|
|
assert_equal(S("Bar"), a.slice!(S("Bar")))
|
|
|
|
assert_equal(S("Foo"), a)
|
|
|
|
|
2008-05-15 10:37:18 -04:00
|
|
|
assert_raise(ArgumentError) { "foo".slice! }
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_split
|
|
|
|
assert_nil($;)
|
|
|
|
assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split)
|
|
|
|
assert_equal([S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" ")))
|
|
|
|
|
|
|
|
assert_equal([S(" a "), S(" b "), S(" c ")], S(" a | b | c ").split(S("|")))
|
|
|
|
|
|
|
|
assert_equal([S("a"), S("b"), S("c")], S("aXXbXXcXX").split(/X./))
|
|
|
|
|
|
|
|
assert_equal([S("a"), S("b"), S("c")], S("abc").split(//))
|
|
|
|
|
|
|
|
assert_equal([S("a|b|c")], S("a|b|c").split(S('|'), 1))
|
|
|
|
|
|
|
|
assert_equal([S("a"), S("b|c")], S("a|b|c").split(S('|'), 2))
|
|
|
|
assert_equal([S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3))
|
|
|
|
|
|
|
|
assert_equal([S("a"), S("b"), S("c"), S("")], S("a|b|c|").split(S('|'), -1))
|
|
|
|
assert_equal([S("a"), S("b"), S("c"), S(""), S("")], S("a|b|c||").split(S('|'), -1))
|
|
|
|
|
|
|
|
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))
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
assert_equal([], "".split(//, 1))
|
2008-05-19 01:20:21 -04:00
|
|
|
|
|
|
|
assert_equal("[2, 3]", [1,2,3].slice!(1,10000).inspect, "moved from btest/knownbug")
|
2015-02-23 01:26:42 -05:00
|
|
|
end
|
2008-05-19 01:20:21 -04:00
|
|
|
|
2015-02-23 01:26:42 -05:00
|
|
|
def test_split_encoding
|
2012-03-27 00:13:54 -04:00
|
|
|
bug6206 = '[ruby-dev:45441]'
|
|
|
|
Encoding.list.each do |enc|
|
|
|
|
next unless enc.ascii_compatible?
|
|
|
|
s = S("a:".force_encoding(enc))
|
|
|
|
assert_equal([enc]*2, s.split(":", 2).map(&:encoding), bug6206)
|
|
|
|
end
|
2015-02-23 01:26:42 -05:00
|
|
|
end
|
2013-07-16 05:45:00 -04:00
|
|
|
|
2015-02-23 01:26:42 -05:00
|
|
|
def test_split_wchar
|
2013-07-16 05:45:00 -04:00
|
|
|
bug8642 = '[ruby-core:56036] [Bug #8642]'
|
|
|
|
[
|
|
|
|
Encoding::UTF_16BE, Encoding::UTF_16LE,
|
|
|
|
Encoding::UTF_32BE, Encoding::UTF_32LE,
|
|
|
|
].each do |enc|
|
|
|
|
s = S("abc,def".encode(enc))
|
|
|
|
assert_equal(["abc", "def"].map {|c| c.encode(enc)},
|
|
|
|
s.split(",".encode(enc)),
|
|
|
|
"#{bug8642} in #{enc.name}")
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2015-02-23 01:26:42 -05:00
|
|
|
def test_split_invalid_sequence
|
|
|
|
bug10886 = '[ruby-core:68229] [Bug #10886]'
|
|
|
|
broken = S("\xa1".force_encoding("utf-8"))
|
|
|
|
assert_raise(ArgumentError, bug10886) {
|
|
|
|
S("a,b").split(broken)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_split_invalid_argument
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
S("a,b").split(BasicObject.new)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_squeeze
|
|
|
|
assert_equal(S("abc"), S("aaabbbbccc").squeeze)
|
|
|
|
assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" ")))
|
|
|
|
assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z")))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_squeeze!
|
|
|
|
a = S("aaabbbbccc")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("abc"), a.squeeze!)
|
|
|
|
assert_equal(S("abc"), a)
|
|
|
|
assert_equal(S("aaabbbbccc"), b)
|
|
|
|
|
|
|
|
a = S("aa bb cc")
|
|
|
|
assert_equal(S("aa bb cc"), a.squeeze!(S(" ")))
|
|
|
|
assert_equal(S("aa bb cc"), a)
|
|
|
|
|
|
|
|
a = S("BxxxTyyyWzzzzz")
|
|
|
|
assert_equal(S("BxTyWz"), a.squeeze!(S("a-z")))
|
|
|
|
assert_equal(S("BxTyWz"), a)
|
|
|
|
|
|
|
|
a=S("The quick brown fox")
|
|
|
|
assert_nil(a.squeeze!)
|
|
|
|
end
|
|
|
|
|
2012-04-01 21:34:11 -04:00
|
|
|
def test_start_with?
|
|
|
|
assert_send([S("hello"), :start_with?, S("hel")])
|
|
|
|
assert_not_send([S("hello"), :start_with?, S("el")])
|
|
|
|
assert_send([S("hello"), :start_with?, S("el"), S("he")])
|
|
|
|
|
|
|
|
bug5536 = '[ruby-core:40623]'
|
|
|
|
assert_raise(TypeError, bug5536) {S("str").start_with? :not_convertible_to_string}
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_strip
|
|
|
|
assert_equal(S("x"), S(" x ").strip)
|
|
|
|
assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip)
|
2009-09-15 01:27:29 -04:00
|
|
|
|
|
|
|
assert_equal("0b0 ".force_encoding("UTF-16BE"),
|
|
|
|
"\x00 0b0 ".force_encoding("UTF-16BE").strip)
|
|
|
|
assert_equal("0\x000b0 ".force_encoding("UTF-16BE"),
|
|
|
|
"0\x000b0 ".force_encoding("UTF-16BE").strip)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_strip!
|
|
|
|
a = S(" x ")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("x") ,a.strip!)
|
|
|
|
assert_equal(S("x") ,a)
|
|
|
|
assert_equal(S(" x "), b)
|
|
|
|
|
|
|
|
a = S(" \n\r\t x \t\r\n\n ")
|
|
|
|
assert_equal(S("x"), a.strip!)
|
|
|
|
assert_equal(S("x"), a)
|
|
|
|
|
|
|
|
a = S("x")
|
|
|
|
assert_nil(a.strip!)
|
|
|
|
assert_equal(S("x") ,a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sub
|
|
|
|
assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*')))
|
|
|
|
assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>')))
|
|
|
|
assert_equal(S("h ello"), S("hello").sub(/./) {
|
|
|
|
|s| s[0].to_s + S(' ')})
|
|
|
|
assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) {
|
|
|
|
|s| $1.upcase + S('-') + $2
|
|
|
|
})
|
2014-03-27 22:28:16 -04:00
|
|
|
assert_equal(S("h<e>llo"), S("hello").sub('e', S('<\0>')))
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\'))
|
|
|
|
assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\'))
|
|
|
|
assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1'))
|
|
|
|
assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1'))
|
|
|
|
assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1'))
|
|
|
|
assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1'))
|
|
|
|
assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1'))
|
|
|
|
|
|
|
|
assert_equal(S("a--ababababababababab"),
|
|
|
|
S("abababababababababab").sub(/(b)/, '-\9-'))
|
|
|
|
assert_equal(S("1-b-0"),
|
|
|
|
S("1b2b3b4b5b6b7b8b9b0").
|
|
|
|
sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-'))
|
|
|
|
assert_equal(S("1-b-0"),
|
|
|
|
S("1b2b3b4b5b6b7b8b9b0").
|
|
|
|
sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-'))
|
|
|
|
assert_equal(S("1-\\9-0"),
|
|
|
|
S("1b2b3b4b5b6b7b8b9b0").
|
|
|
|
sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-'))
|
|
|
|
assert_equal(S("k"),
|
|
|
|
S("1a2b3c4d5e6f7g8h9iAjBk").
|
|
|
|
sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+'))
|
|
|
|
|
|
|
|
assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\'))
|
|
|
|
assert_equal(S("ababa"), S("ababa").sub(/b/, '\&'))
|
|
|
|
assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&'))
|
|
|
|
assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&'))
|
|
|
|
assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&'))
|
|
|
|
assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&'))
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.taint
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
x = a.sub(/./, S('X'))
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_predicate(x, :tainted?)
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
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 })
|
2014-03-27 05:58:12 -04:00
|
|
|
|
|
|
|
assert_equal(S("Abc"), S("abc").sub("a", "A"))
|
|
|
|
m = nil
|
|
|
|
assert_equal(S("Abc"), S("abc").sub("a") {m = $~; "A"})
|
|
|
|
assert_equal(S("a"), m[0])
|
|
|
|
assert_equal(/a/, m.regexp)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sub!
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
a.sub!(/[aeiou]/, S('*'))
|
|
|
|
assert_equal(S("h*llo"), a)
|
|
|
|
assert_equal(S("hello"), b)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.sub!(/([aeiou])/, S('<\1>'))
|
|
|
|
assert_equal(S("h<e>llo"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.sub!(/./) { |s| s[0].to_s + S(' ')}
|
|
|
|
assert_equal(S("h ello"), a)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
a.sub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
|
|
|
|
assert_equal(S("HELL-o"), a)
|
|
|
|
|
|
|
|
a=S("hello")
|
|
|
|
assert_nil(a.sub!(/X/, S('Y')))
|
|
|
|
|
|
|
|
r = S('X')
|
|
|
|
r.taint
|
|
|
|
a.sub!(/./, r)
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_predicate(a, :tainted?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_succ
|
|
|
|
assert_equal(S("abd"), S("abc").succ)
|
|
|
|
assert_equal(S("z"), S("y").succ)
|
|
|
|
assert_equal(S("aaa"), S("zz").succ)
|
|
|
|
|
|
|
|
assert_equal(S("124"), S("123").succ)
|
|
|
|
assert_equal(S("1000"), S("999").succ)
|
2008-07-08 12:38:40 -04:00
|
|
|
assert_equal(S("2.000"), S("1.999").succ)
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2008-07-07 11:23:01 -04:00
|
|
|
assert_equal(S("No.10"), S("No.9").succ)
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal(S("2000aaa"), S("1999zzz").succ)
|
|
|
|
assert_equal(S("AAAAA000"), S("ZZZZ999").succ)
|
|
|
|
assert_equal(S("*+"), S("**").succ)
|
2007-12-24 04:53:08 -05:00
|
|
|
|
|
|
|
assert_equal("abce", "abcd".succ)
|
|
|
|
assert_equal("THX1139", "THX1138".succ)
|
2012-10-11 02:50:26 -04:00
|
|
|
assert_equal("<\<koalb>>", "<\<koala>>".succ)
|
2007-12-24 04:53:08 -05:00
|
|
|
assert_equal("2000aaa", "1999zzz".succ)
|
|
|
|
assert_equal("AAAA0000", "ZZZ9999".succ)
|
|
|
|
assert_equal("**+", "***".succ)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_succ!
|
|
|
|
a = S("abc")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("abd"), a.succ!)
|
|
|
|
assert_equal(S("abd"), a)
|
|
|
|
assert_equal(S("abc"), b)
|
|
|
|
|
|
|
|
a = S("y")
|
|
|
|
assert_equal(S("z"), a.succ!)
|
|
|
|
assert_equal(S("z"), a)
|
|
|
|
|
|
|
|
a = S("zz")
|
|
|
|
assert_equal(S("aaa"), a.succ!)
|
|
|
|
assert_equal(S("aaa"), a)
|
|
|
|
|
|
|
|
a = S("123")
|
|
|
|
assert_equal(S("124"), a.succ!)
|
|
|
|
assert_equal(S("124"), a)
|
|
|
|
|
|
|
|
a = S("999")
|
|
|
|
assert_equal(S("1000"), a.succ!)
|
|
|
|
assert_equal(S("1000"), a)
|
|
|
|
|
|
|
|
a = S("1999zzz")
|
|
|
|
assert_equal(S("2000aaa"), a.succ!)
|
|
|
|
assert_equal(S("2000aaa"), a)
|
|
|
|
|
|
|
|
a = S("ZZZZ999")
|
|
|
|
assert_equal(S("AAAAA000"), a.succ!)
|
|
|
|
assert_equal(S("AAAAA000"), a)
|
|
|
|
|
|
|
|
a = S("**")
|
|
|
|
assert_equal(S("*+"), a.succ!)
|
|
|
|
assert_equal(S("*+"), a)
|
2007-12-13 06:52:15 -05:00
|
|
|
|
2008-07-07 11:23:01 -04:00
|
|
|
a = S("No.9")
|
|
|
|
assert_equal(S("No.10"), a.succ!)
|
|
|
|
assert_equal(S("No.10"), a)
|
|
|
|
|
2007-12-13 06:52:15 -05:00
|
|
|
assert_equal("aaaaaaaaaaaa", "zzzzzzzzzzz".succ!)
|
|
|
|
assert_equal("aaaaaaaaaaaaaaaaaaaaaaaa", "zzzzzzzzzzzzzzzzzzzzzzz".succ!)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sum
|
|
|
|
n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001")
|
|
|
|
assert_equal(15, n.sum)
|
|
|
|
n += S("\001")
|
|
|
|
assert_equal(16, n.sum(17))
|
|
|
|
n[0] = 2.chr
|
2013-11-22 01:32:34 -05:00
|
|
|
assert_not_equal(15, n.sum)
|
2014-10-17 05:49:28 -04:00
|
|
|
assert_equal(17, n.sum(0))
|
|
|
|
assert_equal(17, n.sum(-1))
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
2004-10-03 02:02:55 -04:00
|
|
|
def check_sum(str, bits=16)
|
|
|
|
sum = 0
|
|
|
|
str.each_byte {|c| sum += c}
|
|
|
|
sum = sum & ((1 << bits) - 1) if bits != 0
|
|
|
|
assert_equal(sum, str.sum(bits))
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
def test_sum_2
|
2004-10-03 02:02:55 -04:00
|
|
|
assert_equal(0, "".sum)
|
|
|
|
assert_equal(294, "abc".sum)
|
|
|
|
check_sum("abc")
|
|
|
|
check_sum("\x80")
|
2014-10-17 05:49:28 -04:00
|
|
|
-3.upto(70) {|bits|
|
2004-10-03 02:02:55 -04:00
|
|
|
check_sum("xyz", bits)
|
|
|
|
}
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2010-03-18 19:11:12 -04:00
|
|
|
def test_sum_long
|
|
|
|
s8421505 = "\xff" * 8421505
|
|
|
|
assert_equal(127, s8421505.sum(31))
|
|
|
|
assert_equal(2147483775, s8421505.sum(0))
|
|
|
|
s16843010 = ("\xff" * 16843010)
|
|
|
|
assert_equal(254, s16843010.sum(32))
|
|
|
|
assert_equal(4294967550, s16843010.sum(0))
|
|
|
|
end
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
def test_swapcase
|
|
|
|
assert_equal(S("hi&LOW"), S("HI&low").swapcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_swapcase!
|
|
|
|
a = S("hi&LOW")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("HI&low"), a.swapcase!)
|
|
|
|
assert_equal(S("HI&low"), a)
|
|
|
|
assert_equal(S("hi&LOW"), b)
|
|
|
|
|
|
|
|
a = S("$^#^%$#!!")
|
|
|
|
assert_nil(a.swapcase!)
|
|
|
|
assert_equal(S("$^#^%$#!!"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_f
|
|
|
|
assert_equal(344.3, S("344.3").to_f)
|
|
|
|
assert_equal(5.9742e24, S("5.9742e24").to_f)
|
|
|
|
assert_equal(98.6, S("98.6 degrees").to_f)
|
|
|
|
assert_equal(0.0, S("degrees 100.0").to_f)
|
2010-03-17 12:21:45 -04:00
|
|
|
assert_equal([ 0.0].pack('G'), [S(" 0.0").to_f].pack('G'))
|
|
|
|
assert_equal([-0.0].pack('G'), [S("-0.0").to_f].pack('G'))
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_i
|
|
|
|
assert_equal(1480, S("1480ft/sec").to_i)
|
|
|
|
assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i)
|
2007-07-19 13:04:53 -04:00
|
|
|
assert_equal(0, " 0".to_i)
|
|
|
|
assert_equal(0, "+0".to_i)
|
|
|
|
assert_equal(0, "-0".to_i)
|
|
|
|
assert_equal(0, "--0".to_i)
|
|
|
|
assert_equal(16, "0x10".to_i(0))
|
|
|
|
assert_equal(16, "0X10".to_i(0))
|
|
|
|
assert_equal(2, "0b10".to_i(0))
|
|
|
|
assert_equal(2, "0B10".to_i(0))
|
|
|
|
assert_equal(8, "0o10".to_i(0))
|
|
|
|
assert_equal(8, "0O10".to_i(0))
|
|
|
|
assert_equal(10, "0d10".to_i(0))
|
|
|
|
assert_equal(10, "0D10".to_i(0))
|
|
|
|
assert_equal(8, "010".to_i(0))
|
|
|
|
assert_raise(ArgumentError) { "010".to_i(-10) }
|
|
|
|
2.upto(36) {|radix|
|
|
|
|
assert_equal(radix, "10".to_i(radix))
|
|
|
|
assert_equal(radix**2, "100".to_i(radix))
|
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) { "0".to_i(1) }
|
|
|
|
assert_raise(ArgumentError) { "0".to_i(37) }
|
|
|
|
assert_equal(0, "z".to_i(10))
|
|
|
|
assert_equal(12, "1_2".to_i(10))
|
|
|
|
assert_equal(0x40000000, "1073741824".to_i(10))
|
|
|
|
assert_equal(0x4000000000000000, "4611686018427387904".to_i(10))
|
2012-11-21 00:09:19 -05:00
|
|
|
assert_equal(1, "1__2".to_i(10))
|
2007-07-19 13:04:53 -04:00
|
|
|
assert_equal(1, "1_z".to_i(10))
|
2012-03-23 00:19:24 -04:00
|
|
|
|
|
|
|
bug6192 = '[ruby-core:43566]'
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16be").to_i}
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16le").to_i}
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32be").to_i}
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32le").to_i}
|
|
|
|
assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("iso-2022-jp").to_i}
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
a = S("me")
|
|
|
|
assert_equal("me", a.to_s)
|
|
|
|
assert_equal(a.__id__, a.to_s.__id__) if @cls == String
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_str
|
|
|
|
a = S("me")
|
|
|
|
assert_equal("me", a.to_s)
|
|
|
|
assert_equal(a.__id__, a.to_s.__id__) if @cls == String
|
2009-10-30 03:42:04 -04:00
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.to_str
|
|
|
|
"at"
|
|
|
|
end
|
|
|
|
assert_equal("meat", a.concat(o))
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.to_str
|
|
|
|
foo_bar()
|
|
|
|
end
|
|
|
|
assert_match(/foo_bar/, assert_raise(NoMethodError) {a.concat(o)}.message)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tr
|
|
|
|
assert_equal(S("hippo"), S("hello").tr(S("el"), S("ip")))
|
|
|
|
assert_equal(S("*e**o"), S("hello").tr(S("^aeiou"), S("*")))
|
|
|
|
assert_equal(S("hal"), S("ibm").tr(S("b-z"), S("a-z")))
|
2009-02-22 00:33:07 -05:00
|
|
|
|
|
|
|
a = "abc".force_encoding(Encoding::US_ASCII)
|
2009-02-22 08:38:24 -05:00
|
|
|
assert_equal(Encoding::US_ASCII, a.tr(S("z"), S("\u0101")).encoding, '[ruby-core:22326]')
|
|
|
|
|
|
|
|
assert_equal("a".hash, "a".tr("a", "\u0101").tr("\u0101", "a").hash, '[ruby-core:22328]')
|
|
|
|
assert_equal(true, "\u0101".tr("\u0101", "a").ascii_only?)
|
|
|
|
assert_equal(true, "\u3041".tr("\u3041", "a").ascii_only?)
|
|
|
|
assert_equal(false, "\u3041\u3042".tr("\u3041", "a").ascii_only?)
|
2012-03-16 07:06:27 -04:00
|
|
|
|
|
|
|
bug6156 = '[ruby-core:43335]'
|
|
|
|
str, range, star = %w[b a-z *].map{|s|s.encode("utf-16le")}
|
|
|
|
assert_equal(star, str.tr(range, star), bug6156)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tr!
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("hippo"), a.tr!(S("el"), S("ip")))
|
|
|
|
assert_equal(S("hippo"), a)
|
|
|
|
assert_equal(S("hello"),b)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*")))
|
|
|
|
assert_equal(S("*e**o"), a)
|
|
|
|
|
|
|
|
a = S("IBM")
|
|
|
|
assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z")))
|
|
|
|
assert_equal(S("HAL"), a)
|
|
|
|
|
|
|
|
a = S("ibm")
|
|
|
|
assert_nil(a.tr!(S("B-Z"), S("A-Z")))
|
|
|
|
assert_equal(S("ibm"), a)
|
2009-02-22 00:33:07 -05:00
|
|
|
|
|
|
|
a = "abc".force_encoding(Encoding::US_ASCII)
|
2009-02-22 08:38:24 -05:00
|
|
|
assert_nil(a.tr!(S("z"), S("\u0101")), '[ruby-core:22326]')
|
|
|
|
assert_equal(Encoding::US_ASCII, a.encoding, '[ruby-core:22326]')
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tr_s
|
|
|
|
assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
|
|
|
|
assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*")))
|
2009-02-22 08:42:22 -05:00
|
|
|
assert_equal("a".hash, "\u0101\u0101".tr_s("\u0101", "a").hash)
|
|
|
|
assert_equal(true, "\u3041\u3041".tr("\u3041", "a").ascii_only?)
|
2006-12-31 10:02:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tr_s!
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp")))
|
|
|
|
assert_equal(S("hypo"), a)
|
|
|
|
assert_equal(S("hello"), b)
|
|
|
|
|
|
|
|
a = S("hello")
|
|
|
|
assert_equal(S("h*o"), a.tr_s!(S("el"), S("*")))
|
|
|
|
assert_equal(S("h*o"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unpack
|
|
|
|
a = [S("cat"), S("wom"), S("x"), S("yy")]
|
|
|
|
assert_equal(a, S("catwomx yy ").unpack(S("A3A3A3A3")))
|
|
|
|
|
|
|
|
assert_equal([S("cat")], S("cat \000\000").unpack(S("A*")))
|
|
|
|
assert_equal([S("cwx"), S("wx"), S("x"), S("yy")],
|
|
|
|
S("cwx yy ").unpack(S("A3@1A3@2A3A3")))
|
|
|
|
assert_equal([S("cat"), S("wom"), S("x\000\000"), S("yy\000")],
|
|
|
|
S("catwomx\000\000yy\000").unpack(S("a3a3a3a3")))
|
|
|
|
assert_equal([S("cat \000\000")], S("cat \000\000").unpack(S("a*")))
|
|
|
|
assert_equal([S("ca")], S("catdog").unpack(S("a2")))
|
|
|
|
|
|
|
|
assert_equal([S("cat\000\000")],
|
|
|
|
S("cat\000\000\000\000\000dog").unpack(S("a5")))
|
|
|
|
|
|
|
|
assert_equal([S("01100001")], S("\x61").unpack(S("B8")))
|
|
|
|
assert_equal([S("01100001")], S("\x61").unpack(S("B*")))
|
|
|
|
assert_equal([S("0110000100110111")], S("\x61\x37").unpack(S("B16")))
|
|
|
|
assert_equal([S("01100001"), S("00110111")], S("\x61\x37").unpack(S("B8B8")))
|
|
|
|
assert_equal([S("0110")], S("\x60").unpack(S("B4")))
|
|
|
|
|
|
|
|
assert_equal([S("01")], S("\x40").unpack(S("B2")))
|
|
|
|
|
|
|
|
assert_equal([S("01100001")], S("\x86").unpack(S("b8")))
|
|
|
|
assert_equal([S("01100001")], S("\x86").unpack(S("b*")))
|
|
|
|
|
|
|
|
assert_equal([S("0110000100110111")], S("\x86\xec").unpack(S("b16")))
|
|
|
|
assert_equal([S("01100001"), S("00110111")], S("\x86\xec").unpack(S("b8b8")))
|
|
|
|
|
|
|
|
assert_equal([S("0110")], S("\x06").unpack(S("b4")))
|
|
|
|
assert_equal([S("01")], S("\x02").unpack(S("b2")))
|
|
|
|
|
|
|
|
assert_equal([ 65, 66, 67 ], S("ABC").unpack(S("C3")))
|
|
|
|
assert_equal([ 255, 66, 67 ], S("\377BC").unpack("C*"))
|
|
|
|
assert_equal([ 65, 66, 67 ], S("ABC").unpack("c3"))
|
|
|
|
assert_equal([ -1, 66, 67 ], S("\377BC").unpack("c*"))
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal([S("4142"), S("0a"), S("1")], S("AB\n\x10").unpack(S("H4H2H1")))
|
|
|
|
assert_equal([S("1424"), S("a0"), S("2")], S("AB\n\x02").unpack(S("h4h2h1")))
|
|
|
|
|
|
|
|
assert_equal([S("abc\002defcat\001"), S(""), S("")],
|
|
|
|
S("abc=02def=\ncat=\n=01=\n").unpack(S("M9M3M4")))
|
|
|
|
|
|
|
|
assert_equal([S("hello\n")], S("aGVsbG8K\n").unpack(S("m")))
|
|
|
|
|
|
|
|
assert_equal([S("hello\nhello\n")], S(",:&5L;&\\*:&5L;&\\*\n").unpack(S("u")))
|
|
|
|
|
|
|
|
assert_equal([0xa9, 0x42, 0x2260], S("\xc2\xa9B\xe2\x89\xa0").unpack(S("U*")))
|
|
|
|
|
|
|
|
=begin
|
|
|
|
skipping "Not tested:
|
|
|
|
D,d & double-precision float, native format\\
|
|
|
|
E & double-precision float, little-endian byte order\\
|
|
|
|
e & single-precision float, little-endian byte order\\
|
|
|
|
F,f & single-precision float, native format\\
|
|
|
|
G & double-precision float, network (big-endian) byte order\\
|
|
|
|
g & single-precision float, network (big-endian) byte order\\
|
|
|
|
I & unsigned integer\\
|
|
|
|
i & integer\\
|
|
|
|
L & unsigned long\\
|
|
|
|
l & long\\
|
|
|
|
|
|
|
|
m & string encoded in base64 (uuencoded)\\
|
|
|
|
N & long, network (big-endian) byte order\\
|
|
|
|
n & short, network (big-endian) byte-order\\
|
|
|
|
P & pointer to a structure (fixed-length string)\\
|
|
|
|
p & pointer to a null-terminated string\\
|
|
|
|
S & unsigned short\\
|
|
|
|
s & short\\
|
|
|
|
V & long, little-endian byte order\\
|
|
|
|
v & short, little-endian byte order\\
|
|
|
|
X & back up a byte\\
|
|
|
|
x & null byte\\
|
|
|
|
Z & ASCII string (null padded, count is width)\\
|
|
|
|
"
|
|
|
|
=end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_upcase
|
|
|
|
assert_equal(S("HELLO"), S("hello").upcase)
|
|
|
|
assert_equal(S("HELLO"), S("hello").upcase)
|
|
|
|
assert_equal(S("HELLO"), S("HELLO").upcase)
|
|
|
|
assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_upcase!
|
|
|
|
a = S("hello")
|
|
|
|
b = a.dup
|
|
|
|
assert_equal(S("HELLO"), a.upcase!)
|
|
|
|
assert_equal(S("HELLO"), a)
|
|
|
|
assert_equal(S("hello"), b)
|
|
|
|
|
|
|
|
a = S("HELLO")
|
|
|
|
assert_nil(a.upcase!)
|
|
|
|
assert_equal(S("HELLO"), a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_upto
|
|
|
|
a = S("aa")
|
|
|
|
start = S("aa")
|
|
|
|
count = 0
|
|
|
|
assert_equal(S("aa"), a.upto(S("zz")) {|s|
|
|
|
|
assert_equal(start, s)
|
|
|
|
start.succ!
|
|
|
|
count += 1
|
|
|
|
})
|
|
|
|
assert_equal(676, count)
|
|
|
|
end
|
|
|
|
|
2009-09-23 00:14:23 -04:00
|
|
|
def test_upto_numeric
|
|
|
|
a = S("00")
|
|
|
|
start = S("00")
|
|
|
|
count = 0
|
|
|
|
assert_equal(S("00"), a.upto(S("23")) {|s|
|
|
|
|
assert_equal(start, s, "[ruby-dev:39361]")
|
|
|
|
assert_equal(Encoding::US_ASCII, s.encoding)
|
|
|
|
start.succ!
|
|
|
|
count += 1
|
|
|
|
})
|
|
|
|
assert_equal(24, count, "[ruby-dev:39361]")
|
|
|
|
end
|
|
|
|
|
2009-11-03 10:16:55 -05:00
|
|
|
def test_upto_nonalnum
|
|
|
|
first = S("\u3041")
|
|
|
|
last = S("\u3093")
|
|
|
|
count = 0
|
|
|
|
assert_equal(first, first.upto(last) {|s|
|
|
|
|
count += 1
|
|
|
|
s.replace(last)
|
|
|
|
})
|
|
|
|
assert_equal(83, count, "[ruby-dev:39626]")
|
|
|
|
end
|
|
|
|
|
2008-05-23 21:48:21 -04:00
|
|
|
def test_mod_check
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
s = ""
|
|
|
|
s.sub!(/\A/) { s.replace "z" * 2000; "zzz" }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_frozen_check
|
|
|
|
assert_raise(RuntimeError) {
|
|
|
|
s = ""
|
|
|
|
s.sub!(/\A/) { s.freeze; "zzz" }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
class S2 < String
|
|
|
|
end
|
|
|
|
def test_str_new4
|
|
|
|
s = (0..54).to_a.join # length = 100
|
|
|
|
s2 = S2.new(s[10,90])
|
|
|
|
s3 = s2[10,80]
|
|
|
|
assert_equal((10..54).to_a.to_a.join, s2)
|
|
|
|
assert_equal((15..54).to_a.to_a.join, s3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rb_str_new4
|
|
|
|
s = "a" * 100
|
|
|
|
s2 = s[10,90]
|
|
|
|
assert_equal("a" * 90, s2)
|
|
|
|
s3 = s2[10,80]
|
|
|
|
assert_equal("a" * 80, s3)
|
|
|
|
end
|
|
|
|
|
|
|
|
class StringLike
|
|
|
|
def initialize(str)
|
|
|
|
@str = str
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_str
|
|
|
|
@str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rb_str_to_str
|
|
|
|
assert_equal("ab", "a" + StringLike.new("b"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rb_str_shared_replace
|
|
|
|
s = "a" * 100
|
|
|
|
s.succ!
|
|
|
|
assert_equal("a" * 99 + "b", s)
|
|
|
|
s = ""
|
|
|
|
s.succ!
|
|
|
|
assert_equal("", s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_times
|
|
|
|
assert_raise(ArgumentError) { "a" * (-1) }
|
|
|
|
end
|
|
|
|
|
2007-08-28 02:19:11 -04:00
|
|
|
def test_splice!
|
|
|
|
l = S("1234\n234\n34\n4\n")
|
|
|
|
assert_equal(S("1234\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
|
|
|
|
assert_equal(S("234\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
|
|
|
|
assert_equal(S("34\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
|
|
|
|
assert_equal(S("4\n"), l.slice!(/\A.*\n/), "[ruby-dev:31665]")
|
|
|
|
assert_nil(l.slice!(/\A.*\n/), "[ruby-dev:31665]")
|
|
|
|
end
|
2008-01-23 13:14:28 -05:00
|
|
|
|
2008-05-30 09:18:17 -04:00
|
|
|
def test_times2
|
2008-02-16 19:18:16 -05:00
|
|
|
s1 = ''
|
|
|
|
100.times {|n|
|
|
|
|
s2 = "a" * n
|
|
|
|
assert_equal(s1, s2)
|
|
|
|
s1 << 'a'
|
|
|
|
}
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) { "foo" * (-1) }
|
2008-02-16 19:18:16 -05:00
|
|
|
end
|
2008-05-04 01:30:08 -04:00
|
|
|
|
|
|
|
def test_respond_to
|
2009-03-05 22:56:38 -05:00
|
|
|
o = Object.new
|
2008-05-04 01:30:08 -04:00
|
|
|
def o.respond_to?(arg) [:to_str].include?(arg) ? nil : super end
|
|
|
|
def o.to_str() "" end
|
|
|
|
def o.==(other) "" == other end
|
|
|
|
assert_equal(false, "" == o)
|
|
|
|
end
|
2008-05-15 10:37:18 -04:00
|
|
|
|
|
|
|
def test_match_method
|
|
|
|
assert_equal("bar", "foobarbaz".match(/bar/).to_s)
|
|
|
|
|
2012-10-11 02:50:29 -04:00
|
|
|
o = Regexp.new('foo')
|
2008-05-15 10:37:18 -04:00
|
|
|
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
|
|
|
|
|
2013-04-22 20:52:17 -04:00
|
|
|
def test_inspect_nul
|
2013-04-23 08:24:56 -04:00
|
|
|
bug8290 = '[ruby-core:54458]'
|
2013-04-22 20:52:17 -04:00
|
|
|
s = "\0" + "12"
|
2013-04-23 08:24:56 -04:00
|
|
|
assert_equal '"\u000012"', s.inspect, bug8290
|
|
|
|
s = "\0".b + "12"
|
|
|
|
assert_equal '"\x0012"', s.inspect, bug8290
|
2013-04-22 20:52:17 -04:00
|
|
|
end
|
|
|
|
|
2008-05-15 10:37:18 -04:00
|
|
|
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) }
|
2009-05-24 18:50:11 -04:00
|
|
|
def (hyphen = Object.new).to_str; "-"; end
|
|
|
|
assert_equal(%w(foo - bar), "foo-bar".partition(hyphen), '[ruby-core:23540]')
|
2012-03-27 00:13:54 -04:00
|
|
|
|
|
|
|
bug6206 = '[ruby-dev:45441]'
|
|
|
|
Encoding.list.each do |enc|
|
|
|
|
next unless enc.ascii_compatible?
|
|
|
|
s = S("a:".force_encoding(enc))
|
|
|
|
assert_equal([enc]*3, s.partition("|").map(&:encoding), bug6206)
|
|
|
|
end
|
2013-03-24 05:32:06 -04:00
|
|
|
|
|
|
|
assert_equal(["\u30E6\u30FC\u30B6", "@", "\u30C9\u30E1.\u30A4\u30F3"],
|
|
|
|
"\u30E6\u30FC\u30B6@\u30C9\u30E1.\u30A4\u30F3".partition(/[@.]/))
|
2008-05-15 10:37:18 -04:00
|
|
|
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) }
|
2009-05-24 19:06:57 -04:00
|
|
|
def (hyphen = Object.new).to_str; "-"; end
|
|
|
|
assert_equal(%w(foo - bar), "foo-bar".rpartition(hyphen), '[ruby-core:23540]')
|
2012-03-27 00:13:54 -04:00
|
|
|
|
|
|
|
bug6206 = '[ruby-dev:45441]'
|
|
|
|
Encoding.list.each do |enc|
|
|
|
|
next unless enc.ascii_compatible?
|
|
|
|
s = S("a:".force_encoding(enc))
|
|
|
|
assert_equal([enc]*3, s.rpartition("|").map(&:encoding), bug6206)
|
|
|
|
end
|
2013-03-24 05:32:06 -04:00
|
|
|
|
|
|
|
bug8138 = '[ruby-dev:47183]'
|
|
|
|
assert_equal(["\u30E6\u30FC\u30B6@\u30C9\u30E1", ".", "\u30A4\u30F3"],
|
|
|
|
"\u30E6\u30FC\u30B6@\u30C9\u30E1.\u30A4\u30F3".rpartition(/[@.]/), bug8138)
|
2008-05-15 10:37:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_setter
|
|
|
|
assert_raise(TypeError) { $/ = 1 }
|
2014-11-23 12:03:48 -05:00
|
|
|
name = "\u{5206 884c}"
|
|
|
|
assert_separately([], <<-"end;") # do
|
|
|
|
alias $#{name} $/
|
|
|
|
assert_raise_with_message(TypeError, /\\$#{name}/) { $#{name} = 1 }
|
|
|
|
end;
|
2008-05-15 10:37:18 -04:00
|
|
|
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
|
|
|
|
|
2010-10-13 15:17:49 -04:00
|
|
|
class << o;remove_method :to_str;end
|
2008-05-15 10:37:18 -04:00
|
|
|
def o.to_str; "foo"; end
|
|
|
|
assert_nothing_raised do
|
|
|
|
c.class_eval { attr o }
|
|
|
|
end
|
|
|
|
assert_equal(:foo, c.new.foo)
|
|
|
|
end
|
2008-05-23 21:48:21 -04:00
|
|
|
|
|
|
|
def test_gsub_enumerator
|
|
|
|
assert_normal_exit %q{"abc".gsub(/./).next}, "[ruby-dev:34828]"
|
|
|
|
end
|
2008-12-02 12:26:31 -05:00
|
|
|
|
|
|
|
def test_clear_nonasciicompat
|
|
|
|
assert_equal("", "\u3042".encode("ISO-2022-JP").clear)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_try_convert
|
|
|
|
assert_equal(nil, String.try_convert(1))
|
|
|
|
assert_equal("foo", String.try_convert("foo"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_substr_negative_begin
|
|
|
|
assert_equal("\u3042", ("\u3042" * 100)[-1])
|
|
|
|
end
|
|
|
|
|
2010-10-12 17:04:35 -04:00
|
|
|
=begin
|
2008-12-02 12:26:31 -05:00
|
|
|
def test_compare_different_encoding_string
|
|
|
|
s1 = "\xff".force_encoding("UTF-8")
|
|
|
|
s2 = "\xff".force_encoding("ISO-2022-JP")
|
2010-10-12 17:04:35 -04:00
|
|
|
assert_equal([-1, 1], [s1 <=> s2, s2 <=> s1].sort)
|
2008-12-02 12:26:31 -05:00
|
|
|
end
|
2010-10-12 17:04:35 -04:00
|
|
|
=end
|
2008-12-02 12:26:31 -05:00
|
|
|
|
|
|
|
def test_casecmp
|
|
|
|
assert_equal(1, "\u3042B".casecmp("\u3042a"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_upcase2
|
|
|
|
assert_equal("\u3042AB", "\u3042aB".upcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_downcase2
|
|
|
|
assert_equal("\u3042ab", "\u3042aB".downcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rstrip
|
2015-12-26 21:50:56 -05:00
|
|
|
assert_equal(" hello", " hello ".rstrip)
|
2008-12-02 12:26:31 -05:00
|
|
|
assert_equal("\u3042", "\u3042 ".rstrip)
|
|
|
|
assert_raise(Encoding::CompatibilityError) { "\u3042".encode("ISO-2022-JP").rstrip }
|
|
|
|
end
|
|
|
|
|
2015-12-26 21:50:56 -05:00
|
|
|
def test_lstrip
|
|
|
|
assert_equal("hello ", " hello ".lstrip)
|
|
|
|
assert_equal("\u3042", " \u3042".lstrip)
|
|
|
|
end
|
|
|
|
|
2015-12-27 19:18:55 -05:00
|
|
|
def test_lstrip_bang
|
|
|
|
s1 = S(" hello ")
|
|
|
|
assert_equal("hello ", s1.lstrip!)
|
|
|
|
assert_equal("hello ", s1)
|
|
|
|
|
|
|
|
s2 = S("\u3042 ")
|
|
|
|
assert_equal(nil, s2.lstrip!)
|
|
|
|
assert_equal("\u3042 ", s2)
|
|
|
|
|
|
|
|
s3 = S(" \u3042")
|
|
|
|
assert_equal("\u3042", s3.lstrip!)
|
|
|
|
assert_equal("\u3042", s3)
|
|
|
|
end
|
|
|
|
|
2010-10-12 17:04:35 -04:00
|
|
|
=begin
|
2008-12-02 12:26:31 -05:00
|
|
|
def test_symbol_table_overflow
|
|
|
|
assert_in_out_err([], <<-INPUT, [], /symbol table overflow \(symbol [a-z]{8}\) \(RuntimeError\)/)
|
|
|
|
("aaaaaaaa".."zzzzzzzz").each {|s| s.to_sym }
|
|
|
|
INPUT
|
|
|
|
end
|
2010-10-12 17:04:35 -04:00
|
|
|
=end
|
2009-08-12 00:00:03 -04:00
|
|
|
|
|
|
|
def test_shared_force_encoding
|
|
|
|
s = "\u{3066}\u{3059}\u{3068}".gsub(//, '')
|
|
|
|
h = {}
|
|
|
|
h[s] = nil
|
|
|
|
k = h.keys[0]
|
|
|
|
assert_equal(s, k, '[ruby-dev:39068]')
|
|
|
|
assert_equal(Encoding::UTF_8, k.encoding, '[ruby-dev:39068]')
|
|
|
|
s.dup.force_encoding(Encoding::ASCII_8BIT).gsub(//, '')
|
|
|
|
k = h.keys[0]
|
|
|
|
assert_equal(s, k, '[ruby-dev:39068]')
|
|
|
|
assert_equal(Encoding::UTF_8, k.encoding, '[ruby-dev:39068]')
|
|
|
|
end
|
2010-05-28 05:37:06 -04:00
|
|
|
|
|
|
|
def test_ascii_incomat_inspect
|
2010-11-22 04:33:32 -05:00
|
|
|
bug4081 = '[ruby-core:33283]'
|
2010-05-28 05:37:06 -04:00
|
|
|
[Encoding::UTF_16LE, Encoding::UTF_16BE,
|
|
|
|
Encoding::UTF_32LE, Encoding::UTF_32BE].each do |e|
|
2010-06-02 10:06:25 -04:00
|
|
|
assert_equal('"abc"', "abc".encode(e).inspect)
|
|
|
|
assert_equal('"\\u3042\\u3044\\u3046"', "\u3042\u3044\u3046".encode(e).inspect)
|
2010-11-22 04:33:32 -05:00
|
|
|
assert_equal('"ab\\"c"', "ab\"c".encode(e).inspect, bug4081)
|
2010-05-28 05:37:06 -04:00
|
|
|
end
|
2010-11-22 07:30:58 -05:00
|
|
|
begin
|
2012-10-11 02:50:29 -04:00
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
2010-11-22 07:30:58 -05:00
|
|
|
ext = Encoding.default_external
|
|
|
|
Encoding.default_external = "us-ascii"
|
2012-10-11 02:50:29 -04:00
|
|
|
$VERBOSE = verbose
|
2010-11-22 07:30:58 -05:00
|
|
|
i = "abc\"\\".force_encoding("utf-8").inspect
|
|
|
|
ensure
|
2012-10-11 02:50:29 -04:00
|
|
|
$VERBOSE = nil
|
2010-11-22 07:30:58 -05:00
|
|
|
Encoding.default_external = ext
|
2012-10-11 02:50:29 -04:00
|
|
|
$VERBOSE = verbose
|
2010-11-22 07:30:58 -05:00
|
|
|
end
|
|
|
|
assert_equal('"abc\\"\\\\"', i, bug4081)
|
2010-05-28 05:37:06 -04:00
|
|
|
end
|
2010-08-27 03:33:19 -04:00
|
|
|
|
2010-12-11 14:03:26 -05:00
|
|
|
def test_dummy_inspect
|
|
|
|
assert_equal('"\e\x24\x42\x22\x4C\x22\x68\e\x28\x42"',
|
|
|
|
"\u{ffe2}\u{2235}".encode("cp50220").inspect)
|
|
|
|
end
|
|
|
|
|
2010-08-27 03:33:19 -04:00
|
|
|
def test_prepend
|
|
|
|
assert_equal(S("hello world!"), "world!".prepend("hello "))
|
|
|
|
|
|
|
|
foo = Object.new
|
|
|
|
def foo.to_str
|
|
|
|
"b"
|
|
|
|
end
|
|
|
|
assert_equal(S("ba"), "a".prepend(foo))
|
|
|
|
|
|
|
|
a = S("world")
|
|
|
|
b = S("hello ")
|
|
|
|
a.prepend(b)
|
|
|
|
assert_equal(S("hello world"), a)
|
|
|
|
assert_equal(S("hello "), b)
|
|
|
|
end
|
2011-03-07 03:44:45 -05:00
|
|
|
|
|
|
|
def u(str)
|
|
|
|
str.force_encoding(Encoding::UTF_8)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_byteslice
|
|
|
|
assert_equal("h", "hello".byteslice(0))
|
|
|
|
assert_equal(nil, "hello".byteslice(5))
|
|
|
|
assert_equal("o", "hello".byteslice(-1))
|
|
|
|
assert_equal(nil, "hello".byteslice(-6))
|
|
|
|
|
|
|
|
assert_equal("", "hello".byteslice(0, 0))
|
|
|
|
assert_equal("hello", "hello".byteslice(0, 6))
|
|
|
|
assert_equal("hello", "hello".byteslice(0, 6))
|
|
|
|
assert_equal("", "hello".byteslice(5, 1))
|
|
|
|
assert_equal("o", "hello".byteslice(-1, 6))
|
|
|
|
assert_equal(nil, "hello".byteslice(-6, 1))
|
|
|
|
assert_equal(nil, "hello".byteslice(0, -1))
|
|
|
|
|
|
|
|
assert_equal("h", "hello".byteslice(0..0))
|
|
|
|
assert_equal("", "hello".byteslice(5..0))
|
|
|
|
assert_equal("o", "hello".byteslice(4..5))
|
|
|
|
assert_equal(nil, "hello".byteslice(6..0))
|
|
|
|
assert_equal("", "hello".byteslice(-1..0))
|
|
|
|
assert_equal("llo", "hello".byteslice(-3..5))
|
|
|
|
|
|
|
|
assert_equal(u("\x81"), "\u3042".byteslice(1))
|
|
|
|
assert_equal(u("\x81\x82"), "\u3042".byteslice(1, 2))
|
|
|
|
assert_equal(u("\x81\x82"), "\u3042".byteslice(1..2))
|
|
|
|
|
|
|
|
assert_equal(u("\x82")+("\u3042"*9), ("\u3042"*10).byteslice(2, 28))
|
2013-02-25 07:18:48 -05:00
|
|
|
|
|
|
|
bug7954 = '[ruby-dev:47108]'
|
2013-10-01 03:27:14 -04:00
|
|
|
assert_equal(false, "\u3042".byteslice(0, 2).valid_encoding?, bug7954)
|
|
|
|
assert_equal(false, ("\u3042"*10).byteslice(0, 20).valid_encoding?, bug7954)
|
2011-03-07 03:44:45 -05:00
|
|
|
end
|
2013-09-02 03:11:41 -04:00
|
|
|
|
|
|
|
def test_unknown_string_option
|
2013-09-02 10:46:25 -04:00
|
|
|
str = nil
|
|
|
|
assert_nothing_raised(SyntaxError) do
|
2013-09-02 03:11:41 -04:00
|
|
|
eval(%{
|
2013-09-02 10:46:25 -04:00
|
|
|
str = begin"hello"end
|
2013-09-02 03:11:41 -04:00
|
|
|
})
|
|
|
|
end
|
2013-09-02 10:46:25 -04:00
|
|
|
assert_equal "hello", str
|
2013-09-05 05:07:48 -04:00
|
|
|
end
|
2013-09-26 03:58:28 -04:00
|
|
|
|
|
|
|
def test_eq_tilde_can_be_overridden
|
2013-10-01 03:27:11 -04:00
|
|
|
assert_separately([], <<-RUBY)
|
2013-09-26 03:58:28 -04:00
|
|
|
class String
|
2013-10-01 01:48:57 -04:00
|
|
|
undef =~
|
2013-09-26 03:58:28 -04:00
|
|
|
def =~(str)
|
|
|
|
"foo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-01 03:27:11 -04:00
|
|
|
assert_equal("foo", "" =~ //)
|
2013-09-26 03:58:28 -04:00
|
|
|
RUBY
|
|
|
|
end
|
2014-03-11 18:28:33 -04:00
|
|
|
|
|
|
|
class Bug9581 < String
|
|
|
|
def =~ re; :foo end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_regexp_match_subclass
|
|
|
|
s = Bug9581.new("abc")
|
|
|
|
r = /abc/
|
|
|
|
assert_equal(:foo, s =~ r)
|
|
|
|
assert_equal(:foo, s.send(:=~, r))
|
|
|
|
assert_equal(:foo, s.send(:=~, /abc/))
|
|
|
|
assert_equal(:foo, s =~ /abc/, "should not use optimized instruction")
|
|
|
|
end
|
2014-04-08 23:44:55 -04:00
|
|
|
|
|
|
|
def test_LSHIFT_neary_long_max
|
|
|
|
return unless @cls == String
|
2014-05-20 02:41:06 -04:00
|
|
|
assert_ruby_status([], <<-'end;', '[ruby-core:61886] [Bug #9709]', timeout: 20)
|
2014-04-08 23:44:55 -04:00
|
|
|
begin
|
|
|
|
a = "a" * 0x4000_0000
|
|
|
|
a << "a" * 0x1_0000
|
|
|
|
rescue NoMemoryError
|
|
|
|
end
|
|
|
|
end;
|
2014-10-06 03:06:49 -04:00
|
|
|
end if [0].pack("l!").bytesize < [nil].pack("p").bytesize
|
|
|
|
# enable only when string size range is smaller than memory space
|
2015-12-07 10:10:00 -05:00
|
|
|
|
|
|
|
def test_uplus_minus
|
|
|
|
str = "foo"
|
|
|
|
assert_equal(false, str.frozen?)
|
|
|
|
assert_equal(false, (+str).frozen?)
|
|
|
|
assert_equal(true, (-str).frozen?)
|
|
|
|
|
|
|
|
assert_equal(str.object_id, (+str).object_id)
|
|
|
|
assert_not_equal(str.object_id, (-str).object_id)
|
|
|
|
|
|
|
|
str = "bar".freeze
|
|
|
|
assert_equal(true, str.frozen?)
|
|
|
|
assert_equal(false, (+str).frozen?)
|
|
|
|
assert_equal(true, (-str).frozen?)
|
|
|
|
|
|
|
|
assert_not_equal(str.object_id, (+str).object_id)
|
|
|
|
assert_equal(str.object_id, (-str).object_id)
|
|
|
|
end
|
2004-10-03 02:02:55 -04:00
|
|
|
end
|
2012-03-02 09:05:28 -05:00
|
|
|
|
|
|
|
class TestString2 < TestString
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
@cls = S2
|
|
|
|
end
|
|
|
|
end
|