summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/shared/concat.rb
blob: 435158496e62c9f644f751346b1335c3508f5ee4 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
describe :string_concat, shared: true do
  it "concatenates the given argument to self and returns self" do
    str = 'hello '
    str.send(@method, 'world').should equal(str)
    str.should == "hello world"
  end

  it "converts the given argument to a String using to_str" do
    obj = mock('world!')
    obj.should_receive(:to_str).and_return("world!")
    a = 'hello '.send(@method, obj)
    a.should == 'hello world!'
  end

  it "raises a TypeError if the given argument can't be converted to a String" do
    -> { 'hello '.send(@method, [])        }.should raise_error(TypeError)
    -> { 'hello '.send(@method, mock('x')) }.should raise_error(TypeError)
  end

  it "raises a #{frozen_error_class} when self is frozen" do
    a = "hello"
    a.freeze

    -> { a.send(@method, "")     }.should raise_error(frozen_error_class)
    -> { a.send(@method, "test") }.should raise_error(frozen_error_class)
  end

  it "returns a String when given a subclass instance" do
    a = "hello"
    a.send(@method, StringSpecs::MyString.new(" world"))
    a.should == "hello world"
    a.should be_an_instance_of(String)
  end

  it "returns an instance of same class when called on a subclass" do
    str = StringSpecs::MyString.new("hello")
    str.send(@method, " world")
    str.should == "hello world"
    str.should be_an_instance_of(StringSpecs::MyString)
  end

  ruby_version_is ''...'2.7' do
    it "taints self if other is tainted" do
      "x".send(@method, "".taint).tainted?.should == true
      "x".send(@method, "y".taint).tainted?.should == true
    end

    it "untrusts self if other is untrusted" do
      "x".send(@method, "".untrust).untrusted?.should == true
      "x".send(@method, "y".untrust).untrusted?.should == true
    end
  end

  describe "with Integer" do
    it "concatenates the argument interpreted as a codepoint" do
      b = "".send(@method, 33)
      b.should == "!"

      b.encode!(Encoding::UTF_8)
      b.send(@method, 0x203D)
      b.should == "!\u203D"
    end

    # #5855
    it "returns a BINARY string if self is US-ASCII and the argument is between 128-255 (inclusive)" do
      a = ("".encode(Encoding::US_ASCII).send(@method, 128))
      a.encoding.should == Encoding::BINARY
      a.should == 128.chr

      a = ("".encode(Encoding::US_ASCII).send(@method, 255))
      a.encoding.should == Encoding::BINARY
      a.should == 255.chr
    end

    it "raises RangeError if the argument is an invalid codepoint for self's encoding" do
      -> { "".encode(Encoding::US_ASCII).send(@method, 256) }.should raise_error(RangeError)
      -> { "".encode(Encoding::EUC_JP).send(@method, 0x81)  }.should raise_error(RangeError)
    end

    it "raises RangeError if the argument is negative" do
      -> { "".send(@method, -200)          }.should raise_error(RangeError)
      -> { "".send(@method, -bignum_value) }.should raise_error(RangeError)
    end

    it "doesn't call to_int on its argument" do
      x = mock('x')
      x.should_not_receive(:to_int)

      -> { "".send(@method, x) }.should raise_error(TypeError)
    end

    it "raises a #{frozen_error_class} when self is frozen" do
      a = "hello"
      a.freeze

      -> { a.send(@method, 0)  }.should raise_error(frozen_error_class)
      -> { a.send(@method, 33) }.should raise_error(frozen_error_class)
    end
  end
end

describe :string_concat_encoding, shared: true do
  describe "when self is in an ASCII-incompatible encoding incompatible with the argument's encoding" do
    it "uses self's encoding if both are empty" do
      "".encode("UTF-16LE").send(@method, "").encoding.should == Encoding::UTF_16LE
    end

    it "uses self's encoding if the argument is empty" do
      "x".encode("UTF-16LE").send(@method, "").encoding.should == Encoding::UTF_16LE
    end

    it "uses the argument's encoding if self is empty" do
      "".encode("UTF-16LE").send(@method, "x".encode("UTF-8")).encoding.should == Encoding::UTF_8
    end

    it "raises Encoding::CompatibilityError if neither are empty" do
      -> { "x".encode("UTF-16LE").send(@method, "y".encode("UTF-8")) }.should raise_error(Encoding::CompatibilityError)
    end
  end

  describe "when the argument is in an ASCII-incompatible encoding incompatible with self's encoding" do
    it "uses self's encoding if both are empty" do
      "".encode("UTF-8").send(@method, "".encode("UTF-16LE")).encoding.should == Encoding::UTF_8
    end

    it "uses self's encoding if the argument is empty" do
      "x".encode("UTF-8").send(@method, "".encode("UTF-16LE")).encoding.should == Encoding::UTF_8
    end

    it "uses the argument's encoding if self is empty" do
      "".encode("UTF-8").send(@method, "x".encode("UTF-16LE")).encoding.should == Encoding::UTF_16LE
    end

    it "raises Encoding::CompatibilityError if neither are empty" do
      -> { "x".encode("UTF-8").send(@method, "y".encode("UTF-16LE")) }.should raise_error(Encoding::CompatibilityError)
    end
  end

  describe "when self and the argument are in different ASCII-compatible encodings" do
    it "uses self's encoding if both are ASCII-only" do
      "abc".encode("UTF-8").send(@method, "123".encode("SHIFT_JIS")).encoding.should == Encoding::UTF_8
    end

    it "uses self's encoding if the argument is ASCII-only" do
      "\u00E9".encode("UTF-8").send(@method, "123".encode("ISO-8859-1")).encoding.should == Encoding::UTF_8
    end

    it "uses the argument's encoding if self is ASCII-only" do
      "abc".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")).encoding.should == Encoding::ISO_8859_1
    end

    it "raises Encoding::CompatibilityError if neither are ASCII-only" do
      -> { "\u00E9".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")) }.should raise_error(Encoding::CompatibilityError)
    end
  end

  describe "when self is BINARY and argument is US-ASCII" do
    it "uses BINARY encoding" do
      "abc".encode("BINARY").send(@method, "123".encode("US-ASCII")).encoding.should == Encoding::BINARY
    end
  end
end