Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 296 lines (243 sloc) 7.546 kb
957c2c9 @brixen Standardize require lines.
brixen authored
1 require File.expand_path('../../../spec_helper', __FILE__)
2 require File.expand_path('../fixtures/classes.rb', __FILE__)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
3
4 # TODO: Add missing String#[]= specs:
5 # String#[range] = obj
6 # String#[re] = obj
7 # String#[re, idx] = obj
8 # String#[str] = obj
9
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
10 ruby_version_is ""..."1.9" do
11 describe "String#[]= with index" do
12 it "sets the code of the character at idx to char modulo 256" do
13 a = "hello"
14 a[0] = ?b
15 a.should == "bello"
16 a[-1] = ?a
17 a.should == "bella"
18 a[-1] = 0
19 a.should == "bell\x00"
20 a[-5] = 0
21 a.should == "\x00ell\x00"
22
23 a = "x"
24 a[0] = ?y
25 a.should == "y"
26 a[-1] = ?z
27 a.should == "z"
28
29 a[0] = 255
30 a[0].should == 255
31 a[0] = 256
32 a[0].should == 0
33 a[0] = 256 * 3 + 42
34 a[0].should == 42
35 a[0] = -214
36 a[0].should == 42
37 end
356fa4f @brixen Finally cleaned up trailing whitespace in spec files.
brixen authored
38
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
39 it "sets the code to char % 256" do
40 str = "Hello"
41
42 str[0] = ?a + 256 * 3
43 str[0].should == ?a
44 str[0] = -200
45 str[0].should == 56
46 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
47
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
48 it "raises an IndexError without changing self if idx is outside of self" do
49 a = "hello"
326a538 @brixen Fixed use of guards in the specs.
brixen authored
50
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
51 lambda { a[20] = ?a }.should raise_error(IndexError)
52 a.should == "hello"
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
53
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
54 lambda { a[-20] = ?a }.should raise_error(IndexError)
55 a.should == "hello"
627d205 @brixen Removed specs mocking #respond_to? and #method_missing.
brixen authored
56
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
57 lambda { ""[0] = ?a }.should raise_error(IndexError)
58 lambda { ""[-1] = ?a }.should raise_error(IndexError)
59 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
60
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
61 it "calls to_int on index" do
62 str = "hello"
63 str[0.5] = ?c
64 str.should == "cello"
326a538 @brixen Fixed use of guards in the specs.
brixen authored
65
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
66 obj = mock('-1')
67 obj.should_receive(:to_int).and_return(-1)
68 str[obj] = ?y
69 str.should == "celly"
70 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
71
5e9f23c @runpaint String#[]=Fixnum Version guard: unsupported on 1.9
runpaint authored
72 it "doesn't call to_int on char" do
73 obj = mock('x')
74 obj.should_not_receive(:to_int)
75 lambda { "hi"[0] = obj }.should raise_error(TypeError)
76 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
77
680e962 @runpaint String: 1.9 methods raise RuntimeError when frozen
runpaint authored
78 it "raises a TypeError when self is frozen" do
79 a = "hello"
80 a.freeze
81
82 lambda { a[0] = ?b }.should raise_error(TypeError)
83 end
84
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
85 end
86 end
87
88 describe "String#[]= with String" do
89 it "replaces the char at idx with other_str" do
90 a = "hello"
91 a[0] = "bam"
92 a.should == "bamello"
93 a[-2] = ""
94 a.should == "bamelo"
95 end
96
97 it "taints self if other_str is tainted" do
98 a = "hello"
99 a[0] = "".taint
100 a.tainted?.should == true
326a538 @brixen Fixed use of guards in the specs.
brixen authored
101
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
102 a = "hello"
103 a[0] = "x".taint
104 a.tainted?.should == true
105 end
106
107 it "raises an IndexError without changing self if idx is outside of self" do
108 str = "hello"
109
110 lambda { str[20] = "bam" }.should raise_error(IndexError)
111 str.should == "hello"
326a538 @brixen Fixed use of guards in the specs.
brixen authored
112
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
113 lambda { str[-20] = "bam" }.should raise_error(IndexError)
114 str.should == "hello"
115
116 lambda { ""[-1] = "bam" }.should raise_error(IndexError)
117 end
118
2cacb9a @runpaint String#[]=: Fix for ""[0]= on 1.9 vs. 1.8 (bug #1750)
runpaint authored
119 ruby_version_is ""..."1.9" do
120 it "raises an IndexError when setting the zero'th element of an empty String" do
121 lambda { ""[0] = "bam" }.should raise_error(IndexError)
122 end
123 end
124
125 # Behaviour verfieid correct by matz in
126 # http://redmine.ruby-lang.org/issues/show/1750
127 ruby_version_is "1.9" do
128 it "allows assignment to the zero'th element of an empty String" do
129 str = ""
130 str[0] = "bam"
131 str.should == "bam"
132 end
133 end
134
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
135 it "raises IndexError if the string index doesn't match a position in the string" do
136 str = "hello"
137 lambda { str['y'] = "bam" }.should raise_error(IndexError)
138 str.should == "hello"
139 end
140
680e962 @runpaint String: 1.9 methods raise RuntimeError when frozen
runpaint authored
141 ruby_version_is ""..."1.9" do
142 it "raises a TypeError when self is frozen" do
143 a = "hello"
144 a.freeze
145
146 lambda { a[0] = "bam" }.should raise_error(TypeError)
147 end
148 end
149
150 ruby_version_is "1.9" do
151 it "raises a RuntimeError when self is frozen" do
152 a = "hello"
153 a.freeze
326a538 @brixen Fixed use of guards in the specs.
brixen authored
154
680e962 @runpaint String: 1.9 methods raise RuntimeError when frozen
runpaint authored
155 lambda { a[0] = "bam" }.should raise_error(RuntimeError)
156 end
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
157 end
158
159 it "calls to_int on index" do
160 str = "hello"
161 str[0.5] = "hi "
162 str.should == "hi ello"
627d205 @brixen Removed specs mocking #respond_to? and #method_missing.
brixen authored
163
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
164 obj = mock('-1')
165 obj.should_receive(:to_int).and_return(-1)
166 str[obj] = "!"
167 str.should == "hi ell!"
168 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
169
627d205 @brixen Removed specs mocking #respond_to? and #method_missing.
brixen authored
170 it "calls #to_str to convert other to a String" do
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
171 other_str = mock('-test-')
627d205 @brixen Removed specs mocking #respond_to? and #method_missing.
brixen authored
172 other_str.should_receive(:to_str).and_return("-test-")
173
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
174 a = "abc"
175 a[1] = other_str
176 a.should == "a-test-c"
177 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
178
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
179 it "raises a TypeError if other_str can't be converted to a String" do
414ffe0 @runpaint String#[]= Fix TypeError tests for 1.9: :sym -> []
runpaint authored
180 lambda { "test"[1] = [] }.should raise_error(TypeError)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
181 lambda { "test"[1] = mock('x') }.should raise_error(TypeError)
182 lambda { "test"[1] = nil }.should raise_error(TypeError)
183 end
184 end
185
f7dd752 Add String#[]= with Regexp specs
Evan Phoenix authored
186 describe "String#[]= matching with a Regexp" do
187 it "replaces the matched text with the rhs" do
188 str = "hello"
189 str[/lo/] = "x"
190 str.should == "helx"
191 end
192
193 it "raises IndexError if the regexp index doesn't match a position in the string" do
194 str = "hello"
195 lambda { str[/y/] = "bam" }.should raise_error(IndexError)
196 str.should == "hello"
197 end
198
199 describe "with 3 arguments" do
200 it "uses the 2nd of 3 arguments as which capture should be replaced" do
201 str = "aaa bbb ccc"
202 str[/a (bbb) c/, 1] = "ddd"
203 str.should == "aaa ddd ccc"
204 end
205
206 it "allows the specified capture to be negative and count from the end" do
207 str = "abcd"
208 str[/(a)(b)(c)(d)/, -2] = "e"
209 str.should == "abed"
210 end
211
212 it "raises IndexError if the specified capture isn't available" do
213 str = "aaa bbb ccc"
214 lambda { str[/a (bbb) c/, 2] = "ddd" }.should raise_error(IndexError)
215 lambda { str[/a (bbb) c/, -2] = "ddd" }.should raise_error(IndexError)
216 end
217 end
218
219 end
220
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
221 describe "String#[]= with index, count" do
222 it "starts at idx and overwrites count characters before inserting the rest of other_str" do
223 a = "hello"
224 a[0, 2] = "xx"
225 a.should == "xxllo"
226 a = "hello"
227 a[0, 2] = "jello"
228 a.should == "jellollo"
229 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
230
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
231 it "counts negative idx values from end of the string" do
232 a = "hello"
233 a[-1, 0] = "bob"
234 a.should == "hellbobo"
235 a = "hello"
236 a[-5, 0] = "bob"
237 a.should == "bobhello"
238 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
239
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
240 it "overwrites and deletes characters if count is more than the length of other_str" do
241 a = "hello"
242 a[0, 4] = "x"
243 a.should == "xo"
244 a = "hello"
245 a[0, 5] = "x"
246 a.should == "x"
247 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
248
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
249 it "deletes characters if other_str is an empty string" do
250 a = "hello"
251 a[0, 2] = ""
252 a.should == "llo"
253 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
254
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
255 it "deletes characters up to the maximum length of the existing string" do
256 a = "hello"
257 a[0, 6] = "x"
258 a.should == "x"
259 a = "hello"
260 a[0, 100] = ""
261 a.should == ""
262 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
263
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
264 it "appends other_str to the end of the string if idx == the length of the string" do
265 a = "hello"
266 a[5, 0] = "bob"
267 a.should == "hellobob"
268 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
269
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
270 it "taints self if other_str is tainted" do
271 a = "hello"
272 a[0, 0] = "".taint
273 a.tainted?.should == true
326a538 @brixen Fixed use of guards in the specs.
brixen authored
274
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
275 a = "hello"
276 a[1, 4] = "x".taint
277 a.tainted?.should == true
278 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
279
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
280 it "raises an IndexError if |idx| is greater than the length of the string" do
281 lambda { "hello"[6, 0] = "bob" }.should raise_error(IndexError)
282 lambda { "hello"[-6, 0] = "bob" }.should raise_error(IndexError)
283 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
284
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
285 it "raises an IndexError if count < 0" do
286 lambda { "hello"[0, -1] = "bob" }.should raise_error(IndexError)
287 lambda { "hello"[1, -1] = "bob" }.should raise_error(IndexError)
288 end
326a538 @brixen Fixed use of guards in the specs.
brixen authored
289
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
290 it "raises a TypeError if other_str is a type other than String" do
291 lambda { "hello"[0, 2] = nil }.should raise_error(TypeError)
414ffe0 @runpaint String#[]= Fix TypeError tests for 1.9: :sym -> []
runpaint authored
292 lambda { "hello"[0, 2] = [] }.should raise_error(TypeError)
6c1c34a @brixen Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
293 lambda { "hello"[0, 2] = 33 }.should raise_error(TypeError)
294 end
295 end
Something went wrong with that request. Please try again.