File: append_as_bytes_spec.rb

package info (click to toggle)
jruby 9.4.12.0%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 89,468 kB
  • sloc: ruby: 550,612; java: 276,882; yacc: 25,873; ansic: 6,285; xml: 6,172; sh: 1,775; sed: 94; makefile: 76; jsp: 48; tcl: 40; exp: 12
file content (46 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require_relative '../../spec_helper'

describe "String#append_bytes" do
  ruby_version_is "3.4" do
    it "doesn't allow to mutate frozen strings" do
      str = "hello".freeze
      -> { str.append_as_bytes("\xE2\x82") }.should raise_error(FrozenError)
    end

    it "allows creating broken strings" do
      str = +"hello"
      str.append_as_bytes("\xE2\x82")
      str.valid_encoding?.should == false

      str.append_as_bytes("\xAC")
      str.valid_encoding?.should == true

      str = "abc".encode(Encoding::UTF_32LE)
      str.append_as_bytes("def")
      str.encoding.should == Encoding::UTF_32LE
      str.valid_encoding?.should == false
    end

    it "never changes the receiver encoding" do
      str = "".b
      str.append_as_bytes("€")
      str.encoding.should == Encoding::BINARY
    end

    it "accepts variadic String or Integer arguments" do
      str = "hello".b
      str.append_as_bytes("\xE2\x82", 12, 43, "\xAC")
      str.encoding.should == Encoding::BINARY
      str.should == "hello\xE2\x82\f+\xAC".b
    end

    it "only accepts strings or integers, and doesn't attempt to cast with #to_str or #to_int" do
      to_str = mock("to_str")
      to_str.should_not_receive(:to_str)
      to_str.should_not_receive(:to_int)

      str = +"hello"
      -> { str.append_as_bytes(to_str) }.should raise_error(TypeError, "wrong argument type MockObject (expected String or Integer)")
    end
  end
end