diff options
author | Jean Boussier <[email protected]> | 2024-08-01 11:14:19 +0200 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2024-09-09 15:04:51 +0200 |
commit | 16f241f0aa047ed77ccea6b6c361b421a72d0454 (patch) | |
tree | 6eccf0e125885d7221343b7026cfb5703745f153 /test/ruby/test_string.rb | |
parent | 966901b39dd7b37eec68dd6e8a76a79827dc522f (diff) |
Implement String#append_as_bytes(String | Integer, ...)
[Feature #20594]
A handy method to construct a string out of multiple chunks.
Contrary to `String#concat`, it doesn't do any encoding negociation,
and simply append the content as bytes regardless of whether this
result in a broken string or not.
It's the caller responsibility to check for `String#valid_encoding?`
in cases where it's needed.
When passed integers, only the lower byte is considered, like in
`String#setbyte`.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/11552
Diffstat (limited to 'test/ruby/test_string.rb')
-rw-r--r-- | test/ruby/test_string.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 7b3d63dd1b..8658097ae4 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -3630,6 +3630,55 @@ CODE assert_bytesplice_raise(ArgumentError, S("hello"), 0..-1, "bye", 0, 3) end + def test_append_bytes_into_binary + buf = S("".b) + assert_equal Encoding::BINARY, buf.encoding + + buf.append_as_bytes(S("hello")) + assert_equal "hello".b, buf + assert_equal Encoding::BINARY, buf.encoding + + buf.append_as_bytes(S("こんにちは")) + assert_equal S("helloこんにちは".b), buf + assert_equal Encoding::BINARY, buf.encoding + end + + def test_append_bytes_into_utf8 + buf = S("") + assert_equal Encoding::UTF_8, buf.encoding + + buf.append_as_bytes(S("hello")) + assert_equal S("hello"), buf + assert_equal Encoding::UTF_8, buf.encoding + assert_predicate buf, :ascii_only? + assert_predicate buf, :valid_encoding? + + buf.append_as_bytes(S("こんにちは")) + assert_equal S("helloこんにちは"), buf + assert_equal Encoding::UTF_8, buf.encoding + refute_predicate buf, :ascii_only? + assert_predicate buf, :valid_encoding? + + buf.append_as_bytes(S("\xE2\x82".b)) + assert_equal S("helloこんにちは\xE2\x82"), buf + assert_equal Encoding::UTF_8, buf.encoding + refute_predicate buf, :valid_encoding? + + buf.append_as_bytes(S("\xAC".b)) + assert_equal S("helloこんにちは€"), buf + assert_equal Encoding::UTF_8, buf.encoding + assert_predicate buf, :valid_encoding? + end + + def test_append_bytes_into_utf32 + buf = S("abc".encode(Encoding::UTF_32LE)) + assert_equal Encoding::UTF_32LE, buf.encoding + + buf.append_as_bytes("def") + assert_equal Encoding::UTF_32LE, buf.encoding + refute_predicate buf, :valid_encoding? + end + def test_chilled_string chilled_string = eval('"chilled"') |