1 require 'rubicon_testcase'
3 class TestStringSubclass < String; end # for test_to_s/test_to_str
5 # Helper class to test String#=~.
17 class TestString < RubiconTestCase
29 S("Foo")[/Bar/] = S("")
30 @aref_re_silent = true
32 @aref_re_silent = false
36 @aref_slicebang_silent = true
38 @aref_slicebang_silent = false
47 def casetest(a, b, rev=false) # TODO: rename
57 assert_equal(S("Hello"), S("hello").capitalize)
58 assert_equal(S("Hello"), S("hELLO").capitalize)
59 assert_equal(S("Hello"), S("Hello").capitalize)
60 assert_equal(S("123abc"), S("123ABC").capitalize)
63 def test_capitalize_bang
65 assert_equal(S("Hello"), a.capitalize!)
66 assert_equal(S("Hello"), a)
69 assert_nil a.capitalize!
70 assert_equal(S("Hello"), a)
73 assert_equal S("Hello"), a.capitalize!
74 assert_equal S("Hello"), a
77 assert_equal S("123abc"), a.capitalize!
78 assert_equal S("123abc"), a
81 def test_capitalize_bang_multibyte
82 # TODO: flunk "No capitalize! multibyte tests yet"
87 assert_equal(0, S("123abc").casecmp(S("123ABC")))
88 assert_equal(0, S("123AbC").casecmp(S("123aBc")))
89 assert_equal(0, S("123ABC").casecmp(S("123ABC")))
91 assert_equal(1, S("1X3ABC").casecmp(S("123ABC")))
92 assert_equal(1, S("123AXC").casecmp(S("123ABC")))
93 assert_equal(1, S("123ABX").casecmp(S("123ABC")))
94 assert_equal(1, S("123ABCX").casecmp(S("123ABC")))
96 assert_equal(-1, S("1#3ABC").casecmp(S("123ABC")))
97 assert_equal(-1, S("123A#C").casecmp(S("123ABC")))
98 assert_equal(-1, S("123AB#").casecmp(S("123ABC")))
99 assert_equal(-1, S("123AB").casecmp(S("123ABC")))
101 assert_raises TypeError do
102 'foo'.casecmp Object.new
108 assert_not_equal s.object_id, s.center(0).object_id
110 assert_equal S(""), S("") .center(0)
111 assert_equal S("@"), S("@").center(0)
113 assert_equal S(" "), S("") .center(1)
114 assert_equal S("@"), S("@").center(1)
115 assert_equal S("@ "), S("@").center(2)
116 assert_equal S(" @ "), S("@").center(3)
117 assert_equal S(" @ "), S("@").center(4)
118 assert_equal S(" @ "), S("@").center(5)
120 assert_equal S(" @@ "), S("@@").center(5)
122 assert_equal S(""), S("") .center(0, 'X')
123 assert_equal S("@"), S("@").center(0, 'X')
124 assert_equal S("X"), S("") .center(1, 'X')
125 assert_equal S("@"), S("@").center(1, 'X')
126 assert_equal S("@X"), S("@").center(2, 'X')
127 assert_equal S("X@X"), S("@").center(3, 'X')
128 assert_equal S("X@XX"), S("@").center(4, 'X')
129 assert_equal S("XX@XX"), S("@").center(5, 'X')
131 assert_equal S("X@XY"), S("@").center(4, 'XY')
132 assert_equal S("XY@XY"), S("@").center(5, 'XY')
133 assert_equal S("XY@XYX"), S("@").center(6, 'XY')
134 assert_equal S("XYX@XYX"), S("@").center(7, 'XY')
136 assert_raises ArgumentError, "Zero width padding not allowed" do
137 S("").center 0, S("")
142 assert_equal(S("hello"), S("hello").chomp("\n"))
143 assert_equal(S("hello"), S("hello\n").chomp("\n"))
147 assert_equal(S("hello"), S("hello").chomp)
148 assert_equal(S("hello"), S("hello\n").chomp)
151 assert_equal(S("hello"), S("hello").chomp)
152 assert_equal(S("hello"), S("hello!").chomp)
164 assert_equal(S("hello"), a)
165 assert_equal(nil, a.chomp!(S("\n")))
169 assert_equal(S("hello"), a)
174 assert_equal(S("hello"), a)
178 assert_equal(S("hello"), a)
183 assert_equal(S("hello"), a)
187 assert_equal(S("hello"), a)
193 assert_equal(S("hello"), a.chomp!)
194 assert_equal(S("hello\n"), b)
198 assert_equal(S(""), S("").chop)
199 assert_equal(S(""), S("h").chop)
200 assert_equal(S("hell"), S("hello").chop)
201 assert_equal(S("hello"), S("hello\r\n").chop)
202 assert_equal(S("hello\n"), S("hello\n\r").chop)
203 assert_equal(S(""), S("\r\n").chop)
211 assert_equal(S("hell"), a)
213 a = S("hello\r\n").chop!
214 assert_equal(S("hello"), a)
216 a = S("hello\n\r").chop!
217 assert_equal(S("hello\n"), a)
220 assert_equal(S(""), a)
224 assert_equal("RUBY", S("RUBY"))
228 assert_equal(S("world!"), S("world").concat(33))
229 assert_equal(S("world!"), S("world").concat(S('!')))
234 assert_equal(5, a.count(S("lo")))
235 assert_equal(0, a.count(S("lo"), S("h")))
236 assert_equal(2, a.count(S("lo"), S("o")))
237 assert_equal(8, a.count(S("^l")))
238 assert_equal(4, a.count(S("hello"), S("^l")))
239 assert_equal(4, a.count(S("ej-m")))
240 assert_equal(2, a.count(S("aeiou"), S("^e")))
244 assert_equal(S('aaGUC/JkO9/Sc'), S("mypassword").crypt(S("aa")))
245 assert(S('aaGUC/JkO9/Sc') != S("mypassword").crypt(S("ab")))
247 # "salt" should be at least 2 characters
248 assert_raise(ArgumentError) { S("mypassword").crypt("a")}
253 assert_equal(S("heo"), a.delete(S("l"), S("lo")))
254 assert_equal(S("hello"), a.delete(S("lo"), S("h")))
255 assert_equal(S("he"), a.delete(S("lo")))
256 assert_equal(S("hell"), a.delete(S("aeiou"), S("^e")))
257 assert_equal(S("ho"), a.delete(S("ej-m")))
262 a.delete!(S("l"), S("lo"))
263 assert_equal(S("heo"), a)
267 assert_equal(S("he"), a)
270 a.delete!(S("aeiou"), S("^e"))
271 assert_equal(S("hell"), a)
275 assert_equal(S("ho"), a)
278 assert_nil(a.delete!(S("z")))
283 assert_equal(S("he"), a)
284 assert_equal(S("hello"), b)
288 assert_equal(S("hello"), S("helLO").downcase)
289 assert_equal(S("hello"), S("hello").downcase)
290 assert_equal(S("hello"), S("HELLO").downcase)
291 assert_equal(S("abc hello 123"), S("abc HELLO 123").downcase)
294 def test_downcase_bang
296 assert_equal(S("hello"), a.downcase!)
297 assert_equal(S("hello"), a)
300 assert_nil(a.downcase!)
301 assert_equal(S("hello"), a)
304 def test_downcase_bang_multibyte
305 # TODO: flunk "No downcase! multibyte tests yet"
309 a= S("Test") << 1 << 2 << 3 << 9 << 13 << 10
310 assert_equal(S('"Test\\001\\002\\003\\t\\r\\n"'), a.dump)
316 S("hello\nworld").each {|x| res << x}
317 assert_equal(S("hello\n"), res[0])
318 assert_equal(S("world"), res[1])
321 S("hello\n\n\nworld").each(S('')) {|x| res << x}
322 assert_equal(S("hello\n\n\n"), res[0])
323 assert_equal(S("world"), res[1])
327 S("hello!world").each {|x| res << x}
328 assert_equal(S("hello!"), res[0])
329 assert_equal(S("world"), res[1])
336 S("ABC").each_byte {|x| res << x }
337 assert_equal(65, res[0])
338 assert_equal(66, res[1])
339 assert_equal(67, res[2])
345 S("hello\nworld").each {|x| res << x}
346 assert_equal(S("hello\n"), res[0])
347 assert_equal(S("world"), res[1])
350 S("hello\n\n\nworld").each(S('')) {|x| res << x}
351 assert_equal(S("hello\n\n\n"), res[0])
352 assert_equal(S("world"), res[1])
356 S("hello!world").each {|x| res << x}
357 assert_equal(S("hello!"), res[0])
358 assert_equal(S("world"), res[1])
365 assert(!S("not").empty?)
370 assert a.eql?(S("hello"))
375 assert_equal(false, S("foo") == :foo)
376 assert_equal(false, S("foo") == :foo)
378 assert(S("abcdef") == S("abcdef"))
380 assert(S("CAT") != S('cat'))
381 assert(S("CaT") != S('cAt'))
385 assert_equal(false, S("foo") === :foo)
386 casetest(S("abcdef"), S("abcdef"))
387 casetest(S("CAT"), S('cat'), true) # Reverse the test - we don't want to
388 casetest(S("CaT"), S('cAt'), true) # find these in the case.
393 assert_raises TypeError do
394 assert_equal 10, S("FeeFieFoo-Fum") =~ S("Fum")
397 # "str =~ regexp" same as "regexp =~ str"
398 assert_equal 10, S("FeeFieFoo-Fum") =~ /Fum$/
399 assert_equal nil, S("FeeFieFoo-Fum") =~ /FUM$/
401 # "str =~ obj" calls "obj =~ str"
402 assert_equal ["aaa", 123], "aaa" =~ MatchDefiner.new(123)
403 assert_equal ["bbb", :foo], "bbb" =~ MatchDefiner.new(:foo)
404 assert_equal ["ccc", nil], "ccc" =~ MatchDefiner.new(nil)
406 # default Object#=~ method.
407 assert_equal false, "a string" =~ Object.new
411 assert_equal(S("h*ll*"), S("hello").gsub(/[aeiou]/, S('*')))
412 assert_equal(S("h<e>ll<o>"), S("hello").gsub(/([aeiou])/, S('<\1>')))
413 assert_equal(S("104 101 108 108 111 "),
414 S("hello").gsub(/./) { |s| s[0].to_s + S(' ')})
415 assert_equal(S("HELL-o"),
416 S("hello").gsub(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 })
420 assert(a.gsub(/./, S('X')).tainted?)
426 a.gsub!(/[aeiou]/, S('*'))
427 assert_equal(S("h*ll*"), a)
428 assert_equal(S("hello"), b)
431 a.gsub!(/([aeiou])/, S('<\1>'))
432 assert_equal(S("h<e>ll<o>"), a)
435 a.gsub!(/./) { |s| s[0].to_s + S(' ')}
436 assert_equal(S("104 101 108 108 111 "), a)
439 a.gsub!(/(hell)(.)/) { |s| $1.upcase + S('-') + $2 }
440 assert_equal(S("HELL-o"), a)
448 assert_nil(a.sub!(S('X'), S('Y')))
452 assert_equal(S("hello").hash, S("hello").hash)
453 assert_not_equal(S("hello").hash, S("helLO").hash)
457 assert_equal(0, S("0").hex, "0")
458 assert_equal(0, S("0x0").hex, "0x0")
459 assert_equal(255, S("0xff").hex, "0xff")
460 assert_equal(-255, S("-0xff").hex, "-0xff")
461 assert_equal(255, S("0xFF").hex, "0xFF")
462 assert_equal(-255, S("-0xFF").hex, "-0xFF")
463 assert_equal(255, S("0Xff").hex, "0Xff")
464 assert_equal(255, S("ff").hex, "ff")
465 assert_equal(-255, S("-ff").hex, "-ff")
466 assert_equal(255, S("FF").hex, "FF")
467 assert_equal(-255, S("-FF").hex, "-FF")
468 assert_equal(0, S("-ralph").hex, '-ralph')
469 assert_equal(-15, S("-fred").hex, '-fred')
470 assert_equal(15, S("fred").hex, 'fred')
471 assert_equal(-15, S("-Fred").hex, '-Fred')
472 assert_equal(15, S("Fred").hex, 'Fred')
476 assert_equal true, S("foobar").include?(S("foo"))
477 assert_equal false, S("foobar").include?(S("baz"))
479 assert_equal true, S("foobar").include?(?f)
480 assert_equal false, S("foobar").include?(?z)
482 assert_raises TypeError do
488 assert_equal(65, S("AooBar")[0])
489 assert_equal(66, S("FooBaB")[-1])
490 assert_equal(nil, S("FooBar")[6])
491 assert_equal(nil, S("FooBar")[-7])
493 assert_equal(S("Foo"), S("FooBar")[0,3])
494 assert_equal(S("Bar"), S("FooBar")[-3,3])
495 assert_equal(S(""), S("FooBar")[6,2])
496 assert_equal(nil, S("FooBar")[-7,10])
498 assert_equal(S("Foo"), S("FooBar")[0..2])
499 assert_equal(S("Foo"), S("FooBar")[0...3])
500 assert_equal(S("Bar"), S("FooBar")[-3..-1])
501 assert_equal("", S("FooBar")[6..2])
502 assert_equal(nil, S("FooBar")[-10..-7])
504 assert_equal(S("Foo"), S("FooBar")[/^F../])
505 assert_equal(S("Bar"), S("FooBar")[/..r$/])
506 assert_equal(nil, S("FooBar")[/xyzzy/])
507 assert_equal(nil, S("FooBar")[/plugh/])
509 assert_equal(S("Foo"), S("FooBar")[S("Foo")])
510 assert_equal(S("Bar"), S("FooBar")[S("Bar")])
511 assert_equal(nil, S("FooBar")[S("xyzzy")])
512 assert_equal(nil, S("FooBar")[S("plugh")])
515 assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 1])
516 assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, 2])
517 assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, 3])
518 assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
519 assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
520 assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
523 # TODO: figure out why there were two test_index's and how to consolidate
525 assert_equal 0, S("hello").index(?h)
526 assert_equal 3, S("hello").index(?l, 3)
528 assert_nil S("hello").index(?z)
529 assert_nil S("hello").index(?z, 3)
531 assert_equal 1, S("hello").index(S("ell"))
532 assert_equal 3, S("hello").index(S("l"), 3)
534 assert_nil S("hello").index(/z./)
535 assert_nil S("hello").index(S("z"), 3)
537 assert_equal 2, S("hello").index(/ll./)
538 assert_equal 3, S("hello").index(/l./, 3)
540 assert_nil S("hello").index(S("z"))
541 assert_nil S("hello").index(/z./, 3)
543 # flunk "No backref tests" # HACK uncomment
546 def test_index_equals
549 assert_equal(S("AooBar"), s)
552 assert_equal(S("AooBaB"), s)
553 assert_raises(IndexError) { s[-7] = S("xyz") }
554 assert_equal(S("AooBaB"), s)
556 assert_equal(S("ABCooBaB"), s)
560 assert_equal(S("ABar"),s)
562 assert_equal(S("FooBar"), s)
564 assert_equal(S("FooFoo"), s)
565 assert_raise(IndexError) { s[7,3] = S("Bar") }
566 assert_raise(IndexError) { s[-7,3] = S("Bar") }
570 assert_equal(S("ABar"), s)
572 assert_equal(S("AFoo"), s)
574 assert_equal(S("FooFoo"), s)
575 assert_raise(RangeError) { s[7..10] = S("Bar") }
576 assert_raise(RangeError) { s[-7..-10] = S("Bar") }
580 assert_equal(S("BarBar"), s)
582 assert_equal(S("BarFoo"), s)
584 s[/xyzzy/] = S("None")
585 assert_equal(S("BarFoo"), s)
587 assert_raise(IndexError) { s[/xyzzy/] = S("None") }
590 s[/([A-Z]..)([A-Z]..)/, 1] = S("Foo")
591 assert_equal(S("FooFoo"), s)
592 s[/([A-Z]..)([A-Z]..)/, 2] = S("Bar")
593 assert_equal(S("FooBar"), s)
594 assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, 3] = "None" }
595 s[/([A-Z]..)([A-Z]..)/, -1] = S("Foo")
596 assert_equal(S("FooFoo"), s)
597 s[/([A-Z]..)([A-Z]..)/, -2] = S("Bar")
598 assert_equal(S("BarFoo"), s)
599 assert_raise(IndexError) { s[/([A-Z]..)([A-Z]..)/, -3] = "None" }
603 s[S("Foo")] = S("Bar")
604 assert_equal(S("BarBar"), s)
607 s[0..s.size] = S("another string")
608 assert_equal(S("another string"), s)
612 assert_equal S("BCAD"), S("AD").insert(0, S("BC"))
613 assert_equal S("ABCD"), S("AD").insert(1, S("BC"))
614 assert_equal S("ADBC"), S("AD").insert(2, S("BC"))
616 assert_raises(IndexError) { S("AD").insert(3, S("BC")) }
618 assert_equal S("ADBC"), S("AD").insert(-1, S("BC"))
619 assert_equal S("ABCD"), S("AD").insert(-2, S("BC"))
620 assert_equal S("BCAD"), S("AD").insert(-3, S("BC"))
622 assert_raises(IndexError) { S("AD").insert(-4, S("BC")) }
626 assert_equal S("BCAD"), s
629 # Need to make sure that we get back exactly what we want, intern is safe
630 # for this. (test/unit calls inspect on results, which is useless for
634 assert_equal :'""', S("").inspect.intern
635 assert_equal :'"string"', S("string").inspect.intern
636 assert_equal :'"\""', S("\"").inspect.intern
637 assert_equal :'"\\\\"', S("\\").inspect.intern
638 assert_equal :'"\n"', S("\n").inspect.intern
639 assert_equal :'"\r"', S("\r").inspect.intern
640 assert_equal :'"\t"', S("\t").inspect.intern
641 assert_equal :'"\f"', S("\f").inspect.intern
642 assert_equal :'"\001"', S("\001").inspect.intern
643 assert_equal :'"\010"', S("\010").inspect.intern
644 assert_equal :'"\177"', S("\177").inspect.intern
645 assert_equal :'"\377"', S("\377").inspect.intern
647 assert_equal :'"\\#{1}"', (S("#") + S("{1}")).inspect.intern
649 assert_equal :'"\\#$f"', (S("#") + S("$f")).inspect.intern
650 assert_equal :'"\\#@f"', (S("#") + S("@f")).inspect.intern
654 assert_equal(:koala, S("koala").intern)
655 assert(:koala != S("Koala").intern)
658 assert_raise(ArgumentError) { S("").intern }
659 assert_raise(ArgumentError) { S("with\0null\0inside").intern }
663 assert_equal(0, S("").length)
664 assert_equal(4, S("1234").length)
665 assert_equal(6, S("1234\r\n").length)
666 assert_equal(7, S("\0011234\r\n").length)
670 assert_equal S(""), S("").ljust(-1)
671 assert_equal S(""), S("").ljust(0)
672 assert_equal S(" "), S("").ljust(1)
673 assert_equal S(" "), S("").ljust(2)
675 assert_equal S("@"), S("@").ljust(0)
676 assert_equal S("@"), S("@").ljust(1)
677 assert_equal S("@ "), S("@").ljust(2)
678 assert_equal S("@ "), S("@").ljust(3)
680 assert_equal S("@@"), S("@@").ljust(1)
681 assert_equal S("@@"), S("@@").ljust(2)
682 assert_equal S("@@ "), S("@@").ljust(3)
683 assert_equal S("@@ "), S("@@").ljust(4)
685 assert_equal(S("@X"), S("@").ljust(2, "X"))
686 assert_equal(S("@XX"), S("@").ljust(3, "X"))
688 assert_equal(S("@X"), S("@").ljust(2, "XY"))
689 assert_equal(S("@XY"), S("@").ljust(3, "XY"))
690 assert_equal(S("@XY"), S("@").ljust(3, "XY"))
691 assert_equal(S("@XYX"), S("@").ljust(4, "XY"))
693 assert_equal S("@@"), S("@@").ljust(1, "XY")
694 assert_equal S("@@"), S("@@").ljust(2, "XY")
695 assert_equal S("@@X"), S("@@").ljust(3, "XY")
696 assert_equal S("@@XY"), S("@@").ljust(4, "XY")
699 assert_raises ArgumentError do
706 assert_equal(S("hello"), a.lstrip)
707 assert_equal(S(" hello"), a)
708 assert_equal(S("hello "), S(" hello ").lstrip)
709 assert_equal(S("hello"), S("hello").lstrip)
715 assert_equal(S("abc"), a.lstrip!)
716 assert_equal(S("abc"), a)
717 assert_equal(S(" abc"), b)
721 assert_equal(S("world!"), S("world") << 33)
722 assert_equal(S("world!"), S("world") << S('!'))
729 assert_kind_of MatchData, m
730 assert_equal S("cruel"), m.to_s
733 assert_kind_of MatchData, m
734 assert_equal S("cruel"), m.to_s
736 assert_raises TypeError do
741 def o.to_str() return '\w+' end
743 assert_kind_of MatchData, m
744 assert_equal S("cruel"), m.to_s
748 assert_equal(S("abd"), S("abc").next)
749 assert_equal(S("z"), S("y").next)
750 assert_equal(S("aaa"), S("zz").next)
752 assert_equal(S("124"), S("123").next)
753 assert_equal(S("1000"), S("999").next)
755 assert_equal(S("2000aaa"), S("1999zzz").next)
756 assert_equal(S("AAAAA000"), S("ZZZZ999").next)
758 assert_equal(S("*+"), S("**").next)
764 assert_equal(S("abd"), a.next!)
765 assert_equal(S("abd"), a)
766 assert_equal(S("abc"), b)
769 assert_equal(S("z"), a.next!)
770 assert_equal(S("z"), a)
773 assert_equal(S("aaa"), a.next!)
774 assert_equal(S("aaa"), a)
777 assert_equal(S("124"), a.next!)
778 assert_equal(S("124"), a)
781 assert_equal(S("1000"), a.next!)
782 assert_equal(S("1000"), a)
785 assert_equal(S("2000aaa"), a.next!)
786 assert_equal(S("2000aaa"), a)
789 assert_equal(S("AAAAA000"), a.next!)
790 assert_equal(S("AAAAA000"), a)
793 assert_equal(S("*+"), a.next!)
794 assert_equal(S("*+"), a)
798 assert_equal(0, S("0").oct, "0")
799 assert_equal(255, S("0377").oct, "0377")
800 assert_equal(-255, S("-0377").oct, "-0377")
801 assert_equal(255, S("377").oct, "377")
802 assert_equal(-255, S("-377").oct, "-377")
803 assert_equal(24, S("030X").oct, "030X")
804 assert_equal(-24, S("-030X").oct, "-030X")
805 assert_equal(0, S("ralph").oct, "ralph")
806 assert_equal(0, S("-ralph").oct, "-ralph")
810 assert_equal(S("00123"), S("%05d") % 123)
811 assert_equal(S("123 |00000001"), S("%-5s|%08x") % [123, 1])
812 x = S("%3s %-4s%%foo %.0s%5d %#x%c%3.1f %b %x %X %#b %#x %#X") %
827 assert_equal(S(' hi 123 %foo 456 0x0A3.1 1011 ab AB 0b1011 0xab 0XAB'), x)
834 assert_equal S(''), s3
835 assert_not_equal s1.object_id, s3.object_id
836 assert_not_equal s2.object_id, s3.object_id
841 assert_equal S('yo'), s3
842 assert_not_equal s1.object_id, s3.object_id
847 assert_equal S('yo'), s3
848 assert_not_equal s2.object_id, s3.object_id
853 assert_equal S('yodel'), s3
854 assert_equal false, s3.tainted?
860 assert_equal true, s3.tainted?
866 assert_equal true, s3.tainted?
873 assert_equal true, s3.tainted?
878 assert_equal S("f"), a.replace(S("f"))
881 assert_equal S("foobar"), a.replace(S("foobar"))
885 b = a.replace S("xyz")
886 assert_equal S("xyz"), b
888 assert b.tainted?, "Replaced string should be tainted"
893 assert_equal(S("ateb"), a.reverse)
894 assert_equal(S("beta"), a)
897 def test_reverse_bang
899 assert_equal(S("ateb"), a.reverse!)
900 assert_equal(S("ateb"), a)
905 assert_equal 0, S('').rindex(S(''))
906 assert_equal 3, S('foo').rindex(S(''))
907 assert_nil S('').rindex(S('x'))
909 assert_equal 6, S("ell, hello").rindex(S("ell"))
910 assert_equal 3, S("hello,lo").rindex(S("l"), 3)
912 assert_nil S("hello").rindex(S("z"))
913 assert_nil S("hello").rindex(S("z"), 3)
916 assert_nil S('').rindex(0)
918 assert_equal 3, S("hello").rindex(?l)
919 assert_equal 3, S("hello,lo").rindex(?l, 3)
921 assert_nil S("hello").rindex(?z)
922 assert_nil S("hello").rindex(?z, 3)
924 assert_nil S('').rindex(256)
926 assert_nil S('').rindex(-1)
929 assert_equal 0, S('').rindex(//)
930 assert_equal 5, S('hello').rindex(//)
932 assert_nil S('').rindex(/x/)
934 assert_equal 7, S("ell, hello").rindex(/ll./)
935 assert_equal 3, S("hello,lo").rindex(/l./, 3)
937 assert_nil S("hello").rindex(/z./, 3)
938 assert_nil S("hello").rindex(/z./)
942 assert_equal S(""), S("").rjust(-1)
943 assert_equal S(""), S("").rjust(0)
944 assert_equal S(" "), S("").rjust(1)
945 assert_equal S(" "), S("").rjust(2)
947 assert_equal S("@"), S("@").rjust(0)
948 assert_equal S("@"), S("@").rjust(1)
949 assert_equal S(" @"), S("@").rjust(2)
950 assert_equal S(" @"), S("@").rjust(3)
952 assert_equal S("@@"), S("@@").rjust(1)
953 assert_equal S("@@"), S("@@").rjust(2)
954 assert_equal S(" @@"), S("@@").rjust(3)
955 assert_equal S(" @@"), S("@@").rjust(4)
957 assert_equal(S("X@"), S("@").rjust(2, "X"))
958 assert_equal(S("XX@"), S("@").rjust(3, "X"))
960 assert_equal(S("X@"), S("@").rjust(2, "XY"))
961 assert_equal(S("XY@"), S("@").rjust(3, "XY"))
962 assert_equal(S("XY@"), S("@").rjust(3, "XY"))
963 assert_equal(S("XYX@"), S("@").rjust(4, "XY"))
965 assert_equal S("@@"), S("@@").rjust(1, "XY")
966 assert_equal S("@@"), S("@@").rjust(2, "XY")
967 assert_equal S("X@@"), S("@@").rjust(3, "XY")
968 assert_equal S("XY@@"), S("@@").rjust(4, "XY")
971 assert_raises ArgumentError do
978 assert_equal(S("hello"), a.rstrip)
979 assert_equal(S("hello "), a)
980 assert_equal(S(" hello"), S(" hello ").rstrip)
981 assert_equal(S("hello"), S("hello").rstrip)
987 assert_equal(S("abc"), a.rstrip!)
988 assert_equal(S("abc"), a)
989 assert_equal(S("abc "), b)
994 assert_equal([S("cruel"), S("world")],a.scan(/\w+/))
995 assert_equal([S("cru"), S("el "), S("wor")],a.scan(/.../))
996 assert_equal([[S("cru")], [S("el ")], [S("wor")]],a.scan(/(...)/))
999 a.scan(/\w+/) { |w| res << w }
1000 assert_equal([S("cruel"), S("world") ],res)
1003 a.scan(/.../) { |w| res << w }
1004 assert_equal([S("cru"), S("el "), S("wor")],res)
1007 a.scan(/(...)/) { |w| res << w }
1008 assert_equal([[S("cru")], [S("el ")], [S("wor")]],res)
1012 assert_equal(0, S("").size)
1013 assert_equal(4, S("1234").size)
1014 assert_equal(6, S("1234\r\n").size)
1015 assert_equal(7, S("\0011234\r\n").size)
1019 assert_equal(65, S("AooBar").slice(0))
1020 assert_equal(66, S("FooBaB").slice(-1))
1021 assert_nil(S("FooBar").slice(6))
1022 assert_nil(S("FooBar").slice(-7))
1024 assert_equal(S("Foo"), S("FooBar").slice(0,3))
1025 assert_equal(S(S("Bar")), S("FooBar").slice(-3,3))
1026 assert_nil(S("FooBar").slice(7,2)) # Maybe should be six?
1027 assert_nil(S("FooBar").slice(-7,10))
1029 assert_equal(S("Foo"), S("FooBar").slice(0..2))
1030 assert_equal(S("Bar"), S("FooBar").slice(-3..-1))
1031 # Version.less_than("1.8.2") do
1032 # assert_nil(S("FooBar").slice(6..2))
1034 # Version.greater_or_equal("1.8.2") do
1035 assert_equal("", S("FooBar").slice(6..2))
1037 assert_nil(S("FooBar").slice(-10..-7))
1039 assert_equal(S("Foo"), S("FooBar").slice(/^F../))
1040 assert_equal(S("Bar"), S("FooBar").slice(/..r$/))
1041 assert_nil(S("FooBar").slice(/xyzzy/))
1042 assert_nil(S("FooBar").slice(/plugh/))
1044 assert_equal(S("Foo"), S("FooBar").slice(S("Foo")))
1045 assert_equal(S("Bar"), S("FooBar").slice(S("Bar")))
1046 assert_nil(S("FooBar").slice(S("xyzzy")))
1047 assert_nil(S("FooBar").slice(S("plugh")))
1053 assert_equal(65, a.slice!(0))
1054 assert_equal(S("ooBar"), a)
1055 assert_equal(S("AooBar"), b)
1058 assert_equal(?r,a.slice!(-1))
1059 assert_equal(S("FooBa"), a)
1062 if @aref_slicebang_silent
1063 assert_nil( a.slice!(6) )
1065 assert_raises(:IndexError) { a.slice!(6) }
1067 assert_equal(S("FooBar"), a)
1069 if @aref_slicebang_silent
1070 assert_nil( a.slice!(-7) )
1072 assert_raises(:IndexError) { a.slice!(-7) }
1074 assert_equal(S("FooBar"), a)
1077 assert_equal(S("Foo"), a.slice!(0,3))
1078 assert_equal(S("Bar"), a)
1081 assert_equal(S("Bar"), a.slice!(-3,3))
1082 assert_equal(S("Foo"), a)
1085 if @aref_slicebang_silent
1086 assert_nil(a.slice!(7,2)) # Maybe should be six?
1088 assert_raises(:IndexError) {a.slice!(7,2)} # Maybe should be six?
1090 assert_equal(S("FooBar"), a)
1091 if @aref_slicebang_silent
1092 assert_nil(a.slice!(-7,10))
1094 assert_raises(:IndexError) {a.slice!(-7,10)}
1096 assert_equal(S("FooBar"), a)
1099 assert_equal(S("Foo"), a.slice!(0..2))
1100 assert_equal(S("Bar"), a)
1103 assert_equal(S("Bar"), a.slice!(-3..-1))
1104 assert_equal(S("Foo"), a)
1107 if @aref_slicebang_silent
1108 # Version.less_than("1.8.2") do
1109 # assert_nil(a.slice!(6..2))
1111 # Version.greater_or_equal("1.8.2") do
1112 assert_equal("", a.slice!(6..2))
1115 assert_raises(:RangeError) {a.slice!(6..2)}
1117 assert_equal(S("FooBar"), a)
1118 if @aref_slicebang_silent
1119 assert_nil(a.slice!(-10..-7))
1121 assert_raises(:RangeError) {a.slice!(-10..-7)}
1123 assert_equal(S("FooBar"), a)
1126 assert_equal(S("Foo"), a.slice!(/^F../))
1127 assert_equal(S("Bar"), a)
1130 assert_equal(S("Bar"), a.slice!(/..r$/))
1131 assert_equal(S("Foo"), a)
1134 if @aref_slicebang_silent
1135 assert_nil(a.slice!(/xyzzy/))
1137 assert_raises(:IndexError) {a.slice!(/xyzzy/)}
1139 assert_equal(S("FooBar"), a)
1140 if @aref_slicebang_silent
1141 assert_nil(a.slice!(/plugh/))
1143 assert_raises(:IndexError) {a.slice!(/plugh/)}
1145 assert_equal(S("FooBar"), a)
1148 assert_equal(S("Foo"), a.slice!(S("Foo")))
1149 assert_equal(S("Bar"), a)
1152 assert_equal(S("Bar"), a.slice!(S("Bar")))
1153 assert_equal(S("Foo"), a)
1157 assert_equal( 1, S("abcdef") <=> S("ABCDEF"))
1158 assert_equal(-1, S("ABCDEF") <=> S("abcdef"))
1160 assert_equal( 1, S("abcdef") <=> S("abcde") )
1161 assert_equal( 0, S("abcdef") <=> S("abcdef"))
1162 assert_equal(-1, S("abcde") <=> S("abcdef"))
1166 original_dollar_semi = $;.nil? ? $; : $;.dup
1168 assert_equal [], S("").split
1169 assert_equal [], S("").split(nil)
1170 assert_equal [], S("").split(' ')
1171 assert_equal [], S("").split(nil, 1)
1172 assert_equal [], S("").split(' ', 1)
1175 arr = str.split(nil, 1)
1176 assert_equal ["a"], arr
1177 assert_equal str.object_id, arr.first.object_id
1179 # Tests of #split's behavior with a pattern of ' ' or $; == nil
1181 assert_equal [S("a"), S("b")], S("a b") .split
1182 assert_equal [S("a"), S("b")], S("a b") .split
1183 assert_equal [S("a"), S("b")], S(" a b") .split
1184 assert_equal [S("a"), S("b")], S(" a b ") .split
1185 assert_equal [S("a"), S("b")], S(" a b ") .split
1186 assert_equal [S("a"), S("b")], S(" a b ") .split
1187 assert_equal [S("a"), S("b")], S(" a b ") .split
1188 assert_equal [S("a"), S("b")], S(" a b ") .split
1190 assert_equal [S("a"), S("b")], S("a\tb") .split
1191 assert_equal [S("a"), S("b")], S("a\t\tb") .split
1192 assert_equal [S("a"), S("b")], S("a\nb") .split
1193 assert_equal [S("a"), S("b")], S("a\n\nb") .split
1194 assert_equal [S("a"), S("b")], S("a\rb") .split
1195 assert_equal [S("a"), S("b")], S("a\r\rb") .split
1197 assert_equal [S("a"), S("b")], S("a\t b") .split
1198 assert_equal [S("a"), S("b")], S("a\t b") .split
1199 assert_equal [S("a"), S("b")], S("a\t\nb") .split
1200 assert_equal [S("a"), S("b")], S("a\r\nb") .split
1201 assert_equal [S("a"), S("b")], S("a\r\n\r\nb").split
1203 assert_equal [S("a"), S("b"), S("c")], S(" a b\t c ").split
1205 assert_equal [S("a"), S("b")], S("a b") .split(S(" "))
1206 assert_equal [S("a"), S("b")], S("a b") .split(S(" "))
1207 assert_equal [S("a"), S("b")], S(" a b") .split(S(" "))
1208 assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1209 assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1210 assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1211 assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1212 assert_equal [S("a"), S("b")], S(" a b ") .split(S(" "))
1214 assert_equal [S("a"), S("b")], S("a\tb") .split(S(" "))
1215 assert_equal [S("a"), S("b")], S("a\nb") .split(S(" "))
1216 assert_equal [S("a"), S("b")], S("a\rb") .split(S(" "))
1217 assert_equal [S("a"), S("b")], S("a\vb") .split(S(" "))
1218 assert_equal [S("a"), S("b")], S("a\t\tb") .split(S(" "))
1219 assert_equal [S("a"), S("b")], S("a\n\nb") .split(S(" "))
1220 assert_equal [S("a"), S("b")], S("a\r\rb") .split(S(" "))
1221 assert_equal [S("a"), S("b")], S("a\v\vb") .split(S(" "))
1222 assert_equal [S("a"), S("b")], S("a\v\vb") .split(S(" "))
1224 assert_equal [S("a"), S("b")], S("a\t b") .split(S(" "))
1225 assert_equal [S("a"), S("b")], S("a\t b") .split(S(" "))
1226 assert_equal [S("a"), S("b")], S("a\t\nb") .split(S(" "))
1227 assert_equal [S("a"), S("b")], S("a\r\nb") .split(S(" "))
1228 assert_equal [S("a"), S("b")], S("a\r\n\r\nb").split(S(" "))
1230 assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), -2)
1231 assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), -1)
1232 assert_equal [S("a"), S("b")], S(" a b ").split(S(" "), 0)
1233 assert_equal [S(" a b ")], S(" a b ").split(S(" "), 1)
1234 assert_equal [S("a"), S("b ")], S(" a b ").split(S(" "), 2)
1235 assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), 3)
1236 assert_equal [S("a"), S("b"), S("")], S(" a b ").split(S(" "), 4)
1238 assert_equal [S("a"), S("b"), S("c")], S(" a b\t c ").split(S(" "))
1240 assert_equal [S("a"), S("b")], S("a b").split(nil)
1242 # These tests are all for various patterns.
1244 assert_equal [S("a b")], S("a b") .split(S(" "))
1245 assert_equal [S("a"), S("b")], S("a b") .split(S(" "))
1246 assert_equal [S(" a b")], S(" a b") .split(S(" "))
1247 assert_equal [S(" a b ")], S(" a b ") .split(S(" "))
1248 assert_equal [S(""), S("a b ")], S(" a b ") .split(S(" "))
1249 assert_equal [S(""), S("a"), S("b ")], S(" a b ") .split(S(" "))
1250 assert_equal [S(""), S("a b")], S(" a b ") .split(S(" "))
1251 assert_equal [S(""), S("a"), S("b")], S(" a b ").split(S(" "))
1254 assert_equal [S("a"), S("b")], S("a|b") .split
1255 assert_equal [S("a"), S(""), S("b")], S("a||b") .split
1256 assert_equal [S(""), S("a"), S("b")], S("|a|b") .split
1257 assert_equal [S(""), S("a"), S("b")], S("|a|b|") .split
1258 assert_equal [S(""), S(""), S("a"), S("b")], S("||a|b|") .split
1259 assert_equal [S(""), S(""), S("a"), S(""), S("b")], S("||a||b|") .split
1260 assert_equal [S(""), S(""), S("a"), S("b")], S("||a|b||") .split
1261 assert_equal [S(""), S(""), S("a"), S(""), S("b")], S("||a||b||").split
1263 assert_equal [S("a"), S("b"), S("c")], S("a|b|c") .split(S("|"))
1264 assert_equal [S("a"), S(""), S("c")], S("a||c") .split(S("|"))
1265 assert_equal [S(""), S("a"), S("b"), S("c")], S("|a|b|c").split(S("|"))
1267 assert_equal [S("a b")], S("a b").split(nil)
1268 assert_equal [S("a"), S("b")], S("a|b").split(nil)
1272 assert_equal [S("a"), S("b"), S("c")], S("abc").split(//)
1273 assert_equal [S("a"), S("b"), S("c")], S("abc").split(//i)
1275 assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), -1)
1276 assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 0)
1277 assert_equal [S("a|b|c")], S("a|b|c").split(S('|'), 1)
1278 assert_equal [S("a"), S("b|c")], S("a|b|c").split(S('|'), 2)
1279 assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 3)
1280 assert_equal [S("a"), S("b"), S("c")], S("a|b|c").split(S('|'), 4)
1282 assert_equal [S("a"), S("b")], S("a|b|").split(S('|'))
1283 assert_equal([S("a"), S("b"), S("")], S("a|b|").split(S('|'), -1))
1285 assert_equal [S("a"), S(""), S("b"), S("c")], S("a||b|c|").split(S('|'))
1286 assert_equal([S("a"), S(""), S("b"), S("c"), S("")],
1287 S("a||b|c|").split(S('|'), -1))
1289 assert_equal [S("a"), S("b")], S("a b") .split(/ /)
1290 assert_equal [S("a"), S(""), S("b")], S("a b") .split(/ /)
1291 assert_equal [S(""), S("a"), S("b")], S(" a b") .split(/ /)
1292 assert_equal [S(""), S("a"), S("b")], S(" a b ") .split(/ /)
1293 assert_equal [S(""), S(""), S("a"), S("b")], S(" a b ").split(/ /)
1294 assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1295 S(" a b ").split(/ /))
1296 assert_equal([S(""), S(""), S("a"), S("b")],
1297 S(" a b ").split(/ /))
1298 assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1299 S(" a b ").split(/ /))
1301 assert_equal [S("a b")], S("a b") .split(/ /)
1302 assert_equal [S("a"), S("b")], S("a b") .split(/ /)
1303 assert_equal [S(" a b")], S(" a b") .split(/ /)
1304 assert_equal [S(" a b ")], S(" a b ") .split(/ /)
1305 assert_equal [S(""), S("a b ")], S(" a b ") .split(/ /)
1306 assert_equal [S(""), S("a"), S("b ")], S(" a b ") .split(/ /)
1307 assert_equal [S(""), S("a b")], S(" a b ") .split(/ /)
1308 assert_equal [S(""), S("a"), S("b")], S(" a b ").split(/ /)
1310 assert_equal([S("a"), S("b")],
1311 S("a@b") .split(/@/))
1312 assert_equal([S("a"), S(""), S("b")],
1313 S("a@@b") .split(/@/))
1314 assert_equal([S(""), S("a"), S("b")],
1315 S("@a@b") .split(/@/))
1316 assert_equal([S(""), S("a"), S("b")],
1317 S("@a@b@") .split(/@/))
1318 assert_equal([S(""), S(""), S("a"), S("b")],
1319 S("@@a@b@") .split(/@/))
1320 assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1321 S("@@a@@b@") .split(/@/))
1322 assert_equal([S(""), S(""), S("a"), S("b")],
1323 S("@@a@b@@") .split(/@/))
1324 assert_equal([S(""), S(""), S("a"), S(""), S("b")],
1325 S("@@a@@b@@").split(/@/))
1327 assert_equal [S("a"), S("b"), S("c")], S("a@b@c") .split(/@/)
1328 assert_equal [S("a"), S(""), S("c")], S("a@@c") .split(/@/)
1329 assert_equal [S(""), S("a"), S("b"), S("c")], S("@a@b@c").split(/@/)
1333 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1334 S('ab1cd2ef').split(/(\d)/))
1336 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1337 S('ab1cd2ef').split(/(\d)/, -2))
1338 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1339 S('ab1cd2ef').split(/(\d)/, -1))
1340 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1341 S('ab1cd2ef').split(/(\d)/, 0))
1342 assert_equal([S('ab1cd2ef')],
1343 S('ab1cd2ef').split(/(\d)/, 1))
1344 assert_equal([S('ab'), S('1'), S('cd2ef')],
1345 S('ab1cd2ef').split(/(\d)/, 2))
1346 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1347 S('ab1cd2ef').split(/(\d)/, 3))
1348 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1349 S('ab1cd2ef').split(/(\d)/, 4))
1350 assert_equal([S('ab'), S('1'), S('cd'), S('2'), S('ef')],
1351 S('ab1cd2ef').split(/(\d)/, 5))
1355 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1356 S('ab12cd34ef').split(/(\d)(\d)/))
1358 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1359 S('ab12cd34ef').split(/(\d)(\d)/, -2))
1360 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1361 S('ab12cd34ef').split(/(\d)(\d)/, -1))
1362 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1363 S('ab12cd34ef').split(/(\d)(\d)/, 0))
1364 assert_equal([S('ab12cd34ef')],
1365 S('ab12cd34ef').split(/(\d)(\d)/, 1))
1366 assert_equal([S('ab'), S('1'), S('2'), S('cd34ef')],
1367 S('ab12cd34ef').split(/(\d)(\d)/, 2))
1368 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1369 S('ab12cd34ef').split(/(\d)(\d)/, 3))
1370 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1371 S('ab12cd34ef').split(/(\d)(\d)/, 4))
1372 assert_equal([S('ab'), S('1'), S('2'), S('cd'), S('3'), S('4'), S('ef')],
1373 S('ab12cd34ef').split(/(\d)(\d)/, 5))
1375 # nested grouping (puke)
1377 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1378 S('ab12cd34ef').split(/(\d(\d))/))
1380 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1381 S('ab12cd34ef').split(/(\d(\d))/, -2))
1382 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1383 S('ab12cd34ef').split(/(\d(\d))/, -1))
1384 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1385 S('ab12cd34ef').split(/(\d(\d))/, 0))
1386 assert_equal([S('ab12cd34ef')],
1387 S('ab12cd34ef').split(/(\d(\d))/, 1))
1388 assert_equal([S('ab'), S('12'), S('2'), S('cd34ef')],
1389 S('ab12cd34ef').split(/(\d(\d))/, 2))
1390 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1391 S('ab12cd34ef').split(/(\d(\d))/, 3))
1392 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1393 S('ab12cd34ef').split(/(\d(\d))/, 4))
1394 assert_equal([S('ab'), S('12'), S('2'), S('cd'), S('34'), S('4'), S('ef')],
1395 S('ab12cd34ef').split(/(\d(\d))/, 5))
1397 # split any zero-length match
1399 assert_equal [S('a'), S('b'), S('c')], S('abc').split(/[z]|/)
1402 $; = original_dollar_semi
1405 def test_split_icky1
1406 # HACK soo bizarre and ugly.
1407 assert_equal([S('a'), S('b'), S('c'), S('z')],
1408 S('abcz').split(/([z])|/),
1409 "S('abcz').split(/([z])|/)")
1412 def test_split_icky2
1413 assert_equal([S('c'), S('z'), S('a')],
1414 S('cza').split(/([z]|)/),
1415 "S('cz').split(/([z]|)/)")
1416 assert_equal([S('a'), S(''), S('b'), S(''), S('c'), S('z')],
1417 S('abcz').split(/([z]|)/),
1418 "S('abcz').split(/([z]|)/)")
1422 assert_equal(S("abc"), S("aaabbbbccc").squeeze)
1423 assert_equal(S("aa bb cc"), S("aa bb cc").squeeze(S(" ")))
1424 assert_equal(S("BxTyWz"), S("BxxxTyyyWzzzzz").squeeze(S("a-z")))
1427 def test_squeeze_bang
1430 assert_equal(S("abc"), a.squeeze!)
1431 assert_equal(S("abc"), a)
1432 assert_equal(S("aaabbbbccc"), b)
1435 assert_equal(S("aa bb cc"), a.squeeze!(S(" ")))
1436 assert_equal(S("aa bb cc"), a)
1438 a = S("BxxxTyyyWzzzzz")
1439 assert_equal(S("BxTyWz"), a.squeeze!(S("a-z")))
1440 assert_equal(S("BxTyWz"), a)
1442 a=S("The quick brown fox")
1443 assert_nil(a.squeeze!)
1447 assert_equal(S("x"), S(" x ").strip)
1448 assert_equal(S("x"), S(" \n\r\t x \t\r\n\n ").strip)
1454 assert_equal(S("x") ,a.strip!)
1455 assert_equal(S("x") ,a)
1456 assert_equal(S(" x "), b)
1458 a = S(" \n\r\t x \t\r\n\n ")
1459 assert_equal(S("x"), a.strip!)
1460 assert_equal(S("x"), a)
1463 assert_nil(a.strip!)
1464 assert_equal(S("x") ,a)
1468 assert_equal(S("h*llo"), S("hello").sub(/[aeiou]/, S('*')))
1469 assert_equal(S("h<e>llo"), S("hello").sub(/([aeiou])/, S('<\1>')))
1470 assert_equal(S("104 ello"), S("hello").sub(/./) { |s| s[0].to_s + S(' ')})
1471 assert_equal(S("HELL-o"), S("hello").sub(/(hell)(.)/) {
1472 |s| $1.upcase + S('-') + $2
1475 assert_equal(S("a\\aba"), S("ababa").sub(/b/, '\\'))
1476 assert_equal(S("ab\\aba"), S("ababa").sub(/(b)/, '\1\\'))
1477 assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\1'))
1478 assert_equal(S("ababa"), S("ababa").sub(/(b)/, '\\1'))
1479 assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\1'))
1480 assert_equal(S("a\\1aba"), S("ababa").sub(/(b)/, '\\\\1'))
1481 assert_equal(S("a\\baba"), S("ababa").sub(/(b)/, '\\\\\1'))
1483 assert_equal(S("a--ababababababababab"),
1484 S("abababababababababab").sub(/(b)/, '-\9-'))
1485 assert_equal(S("1-b-0"),
1486 S("1b2b3b4b5b6b7b8b9b0").
1487 sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\9-'))
1488 assert_equal(S("1-b-0"),
1489 S("1b2b3b4b5b6b7b8b9b0").
1490 sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\9-'))
1491 assert_equal(S("1-\\9-0"),
1492 S("1b2b3b4b5b6b7b8b9b0").
1493 sub(/(b).(b).(b).(b).(b).(b).(b).(b).(b)/, '-\\\9-'))
1494 assert_equal(S("k"),
1495 S("1a2b3c4d5e6f7g8h9iAjBk").
1496 sub(/.(.).(.).(.).(.).(.).(.).(.).(.).(.).(.).(.)/, '\+'))
1498 assert_equal(S("ab\\aba"), S("ababa").sub(/b/, '\&\\'))
1499 assert_equal(S("ababa"), S("ababa").sub(/b/, '\&'))
1500 assert_equal(S("ababa"), S("ababa").sub(/b/, '\\&'))
1501 assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\&'))
1502 assert_equal(S("a\\&aba"), S("ababa").sub(/b/, '\\\\&'))
1503 assert_equal(S("a\\baba"), S("ababa").sub(/b/, '\\\\\&'))
1507 assert(a.sub(/./, S('X')).tainted?)
1512 assert_nil a.sub!(/X/, S('Y'))
1515 assert_nil a.sub!(/X/) { |s| assert_nil s }
1518 a.sub!(/[aeiou]/, S('*'))
1519 assert_equal(S("h*llo"), a)
1522 a.sub!(/([aeiou])/, S('<\1>'))
1523 assert_equal(S("h<e>llo"), a)
1528 assert_equal S('h'), s
1532 assert_equal(S("104 ello"), a)
1535 a.sub!(/(hell)(.)/) do |s|
1536 assert_equal S('hell'), $1
1537 assert_equal S('o'), $2
1538 $1.upcase + S('-') + $2
1540 assert_equal(S("HELL-o"), a)
1549 assert_equal(S("abd"), S("abc").succ)
1550 assert_equal(S("z"), S("y").succ)
1551 assert_equal(S("aaa"), S("zz").succ)
1553 assert_equal(S("124"), S("123").succ)
1554 assert_equal(S("1000"), S("999").succ)
1556 assert_equal(S("2000aaa"), S("1999zzz").succ)
1557 assert_equal(S("AAAAA000"), S("ZZZZ999").succ)
1558 assert_equal(S("*+"), S("**").succ)
1560 assert_equal(S("\001\000"), S("\377").succ)
1565 assert_equal(S("abd"), a.succ!)
1566 assert_equal(S("abd"), a)
1569 assert_equal(S("z"), a.succ!)
1570 assert_equal(S("z"), a)
1573 assert_equal(S("aaa"), a.succ!)
1574 assert_equal(S("aaa"), a)
1577 assert_equal(S("124"), a.succ!)
1578 assert_equal(S("124"), a)
1581 assert_equal(S("1000"), a.succ!)
1582 assert_equal(S("1000"), a)
1585 assert_equal(S("2000aaa"), a.succ!)
1586 assert_equal(S("2000aaa"), a)
1589 assert_equal(S("AAAAA000"), a.succ!)
1590 assert_equal(S("AAAAA000"), a)
1593 assert_equal(S("*+"), a.succ!)
1594 assert_equal(S("*+"), a)
1597 assert_equal(S("\001\000"), a.succ!)
1598 assert_equal(S("\001\000"), a)
1602 n = S("\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001")
1603 assert_equal(15, n.sum)
1604 assert_equal(15, n.sum(32))
1606 assert_equal(16, n.sum(17))
1607 assert_equal(16, n.sum(32))
1611 # basic test of all reasonable "bit sizes"
1612 str = S("\xFF" * 257)
1613 2.upto(32) do |bits|
1614 assert_equal(65535 % (2**bits), str.sum(bits))
1617 # with 16 bits the result is modulo 65536
1618 assert_equal( 255, S("\xFF").sum)
1619 assert_equal( 510, S("\xFF\xFF").sum)
1620 assert_equal(65535, S("\xFF" * 257).sum)
1621 assert_equal( 254, S("\xFF" * 258).sum)
1623 # with 32 bits the result is modulo 2**32
1624 assert_equal( 255, S("\xFF").sum(32))
1625 assert_equal( 510, S("\xFF\xFF").sum(32))
1626 assert_equal(65535, S("\xFF" * 257).sum(32))
1627 assert_equal(65790, S("\xFF" * 258).sum(32))
1628 # the following case takes 16MB and takes a long time to compute,
1629 # so it is commented out.
1630 #assert_equal( 254, S("\xFF" * (257 * 65537 + 1)).sum(32))
1634 assert_equal(S("hi&LOW"), S("HI&low").swapcase)
1637 def test_swapcase_bang
1640 assert_equal(S("HI&low"), a.swapcase!)
1641 assert_equal(S("HI&low"), a)
1642 assert_equal(S("hi&LOW"), b)
1645 assert_nil(a.swapcase!)
1646 assert_equal(S("$^#^%$#!!"), a)
1649 def test_swapcase_bang_multibyte
1650 # TODO: flunk "No multibyte tests yet"
1654 assert_equal(S(""), S("HO") * 0)
1655 assert_equal(S("HOHO"), S("HO") * 2)
1658 def test_times_negative
1659 assert_raises ArgumentError do
1665 assert_equal(344.3, S("344.3").to_f)
1666 assert_equal(5.9742e24, S("5.9742e24").to_f)
1667 assert_equal(98.6, S("98.6 degrees").to_f)
1668 assert_equal(0.0, S("degrees 100.0").to_f)
1672 assert_equal(1480, S("1480ft/sec").to_i)
1673 assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i)
1678 assert_equal("me", a.to_s)
1679 assert_equal(a.object_id, a.to_s.object_id)
1681 b = TestStringSubclass.new("me")
1682 assert_equal("me", b.to_s)
1683 assert_not_equal(b.object_id, b.to_s.object_id)
1688 assert_equal("me", a.to_str)
1689 assert_equal(a.object_id, a.to_str.object_id)
1691 b = TestStringSubclass.new("me")
1692 assert_equal("me", b.to_str)
1693 assert_not_equal(b.object_id, b.to_str.object_id)
1697 assert_equal(:alpha, S("alpha").to_sym)
1698 assert_equal(:alp_ha, S("alp_ha").to_sym)
1699 assert_equal(:"9alpha", S("9alpha").to_sym)
1700 assert_equal(:"9al pha", S("9al pha").to_sym)
1704 assert_equal S("hippo"), S("hello").tr(S("el"), S("ip"))
1705 assert_equal S("*e**o"), S("hello").tr(S("^aeiou"), S("*"))
1706 assert_equal S("hal"), S("ibm").tr(S("b-z"), S("a-z"))
1708 assert_equal S("www"), S("abc").tr(S("a-c"), S("w"))
1709 assert_equal S("wxx"), S("abc").tr(S("a-c"), S("w-x"))
1710 assert_equal S("wxy"), S("abc").tr(S("a-c"), S("w-y"))
1711 assert_equal S("wxy"), S("abc").tr(S("a-c"), S("w-z"))
1713 assert_equal S("wbc"), S("abc").tr(S("a"), S("w-x"))
1714 assert_equal S("wxc"), S("abc").tr(S("a-b"), S("w-y"))
1720 assert_equal(S("hippo"), a.tr!(S("el"), S("ip")))
1721 assert_equal(S("hippo"), a)
1722 assert_equal(S("hello"),b)
1725 assert_equal(S("*e**o"), a.tr!(S("^aeiou"), S("*")))
1726 assert_equal(S("*e**o"), a)
1729 assert_equal(S("HAL"), a.tr!(S("B-Z"), S("A-Z")))
1730 assert_equal(S("HAL"), a)
1733 assert_nil(a.tr!(S("B-Z"), S("A-Z")))
1734 assert_equal(S("ibm"), a)
1738 assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
1739 assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*")))
1745 assert_equal(S("hypo"), a.tr_s!(S("el"), S("yp")))
1746 assert_equal(S("hypo"), a)
1747 assert_equal(S("hello"), b)
1750 assert_equal(S("h*o"), a.tr_s!(S("el"), S("*")))
1751 assert_equal(S("h*o"), a)
1755 assert_equal(S("HELLO"), S("hello").upcase)
1756 assert_equal(S("HELLO"), S("hello").upcase)
1757 assert_equal(S("HELLO"), S("HELLO").upcase)
1758 assert_equal(S("ABC HELLO 123"), S("abc HELLO 123").upcase)
1761 def test_upcase_bang
1764 assert_equal(S("HELLO"), a.upcase!)
1765 assert_equal(S("HELLO"), a)
1766 assert_equal(S("hello"), b)
1769 assert_nil(a.upcase!)
1770 assert_equal(S("HELLO"), a)
1773 def test_upcase_bang_multibyte
1774 # TODO: flunk "No multibyte tests yet"
1781 val = a.upto S("zz") do |s|
1782 assert_equal(start, s)
1787 assert_equal(S("aa"), val)
1788 assert_equal(676, count)
1792 require 'test/unit' if $0 == __FILE__