summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-24 18:46:15 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-24 18:46:15 +0000
commit3f9b0936aa846bdf6984019ca40bc629fe05d929 (patch)
tree59852ade00f43a1b6325bce662512c18ca596bcf /test
parent7f0dd3a5a23d0009862e0f1213e536fff1a94727 (diff)
String#{lines,chars,codepoints,bytes} now return an array.
* string.c (rb_str_each_line, rb_str_lines): String#lines now returns an array instead of an enumerator. Passing a block is deprecated but still supported for backwards compatibility. Based on the patch by yhara. [Feature #6670] * string.c (rb_str_each_char, rb_str_chars): Ditto for String#chars. * string.c (rb_str_each_codepoint, rb_str_codepoints): Ditto for String#codepoints. * string.c (rb_str_each_byte, rb_str_bytes): Ditto for String#bytes. * NEWS: Add notes for the above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37838 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_string.rb119
1 files changed, 113 insertions, 6 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index cf63048750..e4e6c39663 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -626,36 +626,121 @@ class TestString < Test::Unit::TestCase
end
def test_each_byte
+ s = S("ABC")
+
res = []
- S("ABC").each_byte {|x| res << x }
+ assert_equal s.object_id, s.each_byte {|x| res << x }.object_id
assert_equal(65, res[0])
assert_equal(66, res[1])
assert_equal(67, res[2])
+
+ assert_equal 65, s.each_byte.next
+ end
+
+ def test_bytes
+ s = S("ABC")
+ assert_equal [65, 66, 67], s.bytes
+
+ if RUBY_VERSION >= "2.1.0"
+ 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
end
def test_each_codepoint
+ # Single byte optimization
+ assert_equal 65, S("ABC").each_codepoint.next
+
+ s = S("\u3042\u3044\u3046")
+
res = []
- S("ABC").codepoints.each {|x| res << x}
- assert_equal([65, 66, 67], res)
+ 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
+
+ if RUBY_VERSION >= "2.1.0"
+ 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
+
+ if RUBY_VERSION >= "2.1.0"
+ 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
end
def test_each_line
save = $/
$/ = "\n"
res=[]
- S("hello\nworld").lines.each {|x| res << x}
+ S("hello\nworld").each_line {|x| res << x}
assert_equal(S("hello\n"), res[0])
assert_equal(S("world"), res[1])
res=[]
- S("hello\n\n\nworld").lines(S('')).each {|x| res << x}
+ S("hello\n\n\nworld").each_line(S('')) {|x| res << x}
assert_equal(S("hello\n\n\n"), res[0])
assert_equal(S("world"), res[1])
$/ = "!"
res=[]
- S("hello!world").lines.each {|x| res << x}
+ S("hello!world").each_line {|x| res << x}
assert_equal(S("hello!"), res[0])
assert_equal(S("world"), res[1])
@@ -671,6 +756,28 @@ class TestString < Test::Unit::TestCase
s = nil
"foo\nbar".each_line(nil) {|s2| s = s2 }
assert_equal("foo\nbar", s)
+
+ assert_equal "hello\n", S("hello\nworld").each_line.next
+ assert_equal "hello\nworld", S("hello\nworld").each_line(nil).next
+ end
+
+ def test_lines
+ s = S("hello\nworld")
+ assert_equal ["hello\n", "world"], s.lines
+ assert_equal ["hello\nworld"], s.lines(nil)
+
+ if RUBY_VERSION >= "2.1.0"
+ 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
end
def test_empty?