Skip to content

Commit dc54574

Browse files
committed
1 parent e20f1e4 commit dc54574

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+457
-174
lines changed

spec/ruby/core/encoding/converter/primitive_convert_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
-> { @ec.primitive_convert("","") }.should_not raise_error
1515
end
1616

17+
it "raises FrozenError when the destination buffer is a frozen String" do
18+
-> { @ec.primitive_convert("", "".freeze) }.should raise_error(FrozenError)
19+
end
20+
1721
it "accepts nil for the destination byte offset" do
1822
-> { @ec.primitive_convert("","", nil) }.should_not raise_error
1923
end

spec/ruby/core/exception/case_compare_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626
end
2727

2828
it "returns true if receiver is generic and arg is kind of SystemCallError" do
29-
unknown_error_number = Errno.constants.size
3029
e = SystemCallError.new('foo', @example_errno)
3130
SystemCallError.===(e).should == true
3231
end
3332

3433
it "returns false if receiver is generic and arg is not kind of SystemCallError" do
35-
unknown_error_number = Errno.constants.size
3634
e = Object.new
3735
SystemCallError.===(e).should == false
3836
end

spec/ruby/core/module/const_get_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
-> { ConstantSpecs.const_get("CS_CONST1", false) }.should raise_error(NameError)
106106
end
107107

108-
it "returns a constant whose module is defined the the toplevel" do
108+
it "returns a constant whose module is defined the toplevel" do
109109
ConstantSpecs.const_get("ConstantSpecsTwo::Foo").should == :cs_two_foo
110110
ConstantSpecsThree.const_get("ConstantSpecsTwo::Foo").should == :cs_three_foo
111111
end

spec/ruby/core/module/define_method_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ class << klass
133133
klass.should have_public_instance_method(:baz)
134134
end
135135
end
136+
137+
it "sets the method owner for a dynamically added method with a different original owner" do
138+
mixin_module = Module.new do
139+
def bar; end
140+
end
141+
142+
foo = Object.new
143+
foo.singleton_class.define_method(:bar, mixin_module.instance_method(:bar))
144+
145+
foo.method(:bar).owner.should == foo.singleton_class
146+
end
136147
end
137148

138149
describe "passed a block" do

spec/ruby/core/module/module_function_spec.rb

Lines changed: 115 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,62 @@ def foo
155155

156156
m.foo.should == ["m", "super_m"]
157157
end
158+
159+
context "methods created with define_method" do
160+
context "passed a block" do
161+
it "creates duplicates of the given instance methods" do
162+
m = Module.new do
163+
define_method :test1 do; end
164+
module_function :test1
165+
end
166+
167+
m.respond_to?(:test1).should == true
168+
end
169+
end
170+
171+
context "passed a method" do
172+
it "creates duplicates of the given instance methods" do
173+
module_with_method = Module.new do
174+
def test1; end
175+
end
176+
177+
c = Class.new do
178+
extend module_with_method
179+
end
180+
181+
m = Module.new do
182+
define_method :test2, c.method(:test1)
183+
module_function :test2
184+
end
185+
186+
m.respond_to?(:test2).should == true
187+
end
188+
end
189+
190+
context "passed an unbound method" do
191+
it "creates duplicates of the given instance methods" do
192+
module_with_method = Module.new do
193+
def test1; end
194+
end
195+
196+
m = Module.new do
197+
define_method :test2, module_with_method.instance_method(:test1)
198+
module_function :test2
199+
end
200+
201+
m.respond_to?(:test2).should == true
202+
end
203+
end
204+
end
158205
end
159206

160207
describe "Module#module_function as a toggle (no arguments) in a Module body" do
161208
it "makes any subsequently defined methods module functions with the normal semantics" do
162-
m = Module.new {
209+
m = Module.new do
163210
module_function
164211
def test1() end
165212
def test2() end
166-
}
213+
end
167214

168215
m.respond_to?(:test1).should == true
169216
m.respond_to?(:test2).should == true
@@ -187,13 +234,13 @@ def test2() end
187234

188235
it "stops creating module functions if the body encounters another toggle " \
189236
"like public/protected/private without arguments" do
190-
m = Module.new {
237+
m = Module.new do
191238
module_function
192239
def test1() end
193240
def test2() end
194241
public
195242
def test3() end
196-
}
243+
end
197244

198245
m.respond_to?(:test1).should == true
199246
m.respond_to?(:test2).should == true
@@ -202,84 +249,131 @@ def test3() end
202249

203250
it "does not stop creating module functions if the body encounters " \
204251
"public/protected/private WITH arguments" do
205-
m = Module.new {
252+
m = Module.new do
206253
def foo() end
207254
module_function
208255
def test1() end
209256
def test2() end
210257
public :foo
211258
def test3() end
212-
}
259+
end
213260

214261
m.respond_to?(:test1).should == true
215262
m.respond_to?(:test2).should == true
216263
m.respond_to?(:test3).should == true
217264
end
218265

219266
it "does not affect module_evaled method definitions also if outside the eval itself" do
220-
m = Module.new {
267+
m = Module.new do
221268
module_function
222269
module_eval { def test1() end }
223270
module_eval " def test2() end "
224-
}
271+
end
225272

226273
m.respond_to?(:test1).should == false
227274
m.respond_to?(:test2).should == false
228275
end
229276

230277
it "has no effect if inside a module_eval if the definitions are outside of it" do
231-
m = Module.new {
278+
m = Module.new do
232279
module_eval { module_function }
233280
def test1() end
234281
def test2() end
235-
}
282+
end
236283

237284
m.respond_to?(:test1).should == false
238285
m.respond_to?(:test2).should == false
239286
end
240287

241288
it "functions normally if both toggle and definitions inside a module_eval" do
242-
m = Module.new {
243-
module_eval {
289+
m = Module.new do
290+
module_eval do
244291
module_function
245292
def test1() end
246293
def test2() end
247-
}
248-
}
294+
end
295+
end
249296

250297
m.respond_to?(:test1).should == true
251298
m.respond_to?(:test2).should == true
252299
end
253300

254-
it "affects evaled method definitions also even when outside the eval itself" do
255-
m = Module.new {
301+
it "affects eval'ed method definitions also even when outside the eval itself" do
302+
m = Module.new do
256303
module_function
257304
eval "def test1() end"
258-
}
305+
end
259306

260307
m.respond_to?(:test1).should == true
261308
end
262309

263310
it "doesn't affect definitions when inside an eval even if the definitions are outside of it" do
264-
m = Module.new {
311+
m = Module.new do
265312
eval "module_function"
266313
def test1() end
267-
}
314+
end
268315

269316
m.respond_to?(:test1).should == false
270317
end
271318

272319
it "functions normally if both toggle and definitions inside a eval" do
273-
m = Module.new {
320+
m = Module.new do
274321
eval <<-CODE
275322
module_function
276323
277324
def test1() end
278325
def test2() end
279326
CODE
280-
}
327+
end
281328

282329
m.respond_to?(:test1).should == true
283330
m.respond_to?(:test2).should == true
284331
end
332+
333+
context "methods are defined with define_method" do
334+
context "passed a block" do
335+
it "makes any subsequently defined methods module functions with the normal semantics" do
336+
m = Module.new do
337+
module_function
338+
define_method :test1 do; end
339+
end
340+
341+
m.respond_to?(:test1).should == true
342+
end
343+
end
344+
345+
context "passed a method" do
346+
it "makes any subsequently defined methods module functions with the normal semantics" do
347+
module_with_method = Module.new do
348+
def test1; end
349+
end
350+
351+
c = Class.new do
352+
extend module_with_method
353+
end
354+
355+
m = Module.new do
356+
module_function
357+
define_method :test2, c.method(:test1)
358+
end
359+
360+
m.respond_to?(:test2).should == true
361+
end
362+
end
363+
364+
context "passed an unbound method" do
365+
it "makes any subsequently defined methods module functions with the normal semantics" do
366+
module_with_method = Module.new do
367+
def test1; end
368+
end
369+
370+
m = Module.new do
371+
module_function
372+
define_method :test2, module_with_method.instance_method(:test1)
373+
end
374+
375+
m.respond_to?(:test2).should == true
376+
end
377+
end
378+
end
285379
end

spec/ruby/core/sizedqueue/append_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
22
require_relative '../../shared/queue/enque'
33
require_relative '../../shared/sizedqueue/enque'
4+
require_relative '../../shared/types/rb_num2dbl_fails'
45

56
describe "SizedQueue#<<" do
67
it_behaves_like :queue_enq, :<<, -> { SizedQueue.new(10) }
@@ -9,3 +10,9 @@
910
describe "SizedQueue#<<" do
1011
it_behaves_like :sizedqueue_enq, :<<, -> n { SizedQueue.new(n) }
1112
end
13+
14+
describe "SizedQueue operations with timeout" do
15+
ruby_version_is "3.2" do
16+
it_behaves_like :rb_num2dbl_fails, nil, -> v { q = SizedQueue.new(1); q.send(:<<, 1, timeout: v) }
17+
end
18+
end

spec/ruby/core/sizedqueue/enq_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
22
require_relative '../../shared/queue/enque'
33
require_relative '../../shared/sizedqueue/enque'
4+
require_relative '../../shared/types/rb_num2dbl_fails'
45

56
describe "SizedQueue#enq" do
67
it_behaves_like :queue_enq, :enq, -> { SizedQueue.new(10) }
@@ -9,3 +10,9 @@
910
describe "SizedQueue#enq" do
1011
it_behaves_like :sizedqueue_enq, :enq, -> n { SizedQueue.new(n) }
1112
end
13+
14+
describe "SizedQueue operations with timeout" do
15+
ruby_version_is "3.2" do
16+
it_behaves_like :rb_num2dbl_fails, nil, -> v { q = SizedQueue.new(1); q.enq(1, timeout: v) }
17+
end
18+
end

spec/ruby/core/sizedqueue/push_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
22
require_relative '../../shared/queue/enque'
33
require_relative '../../shared/sizedqueue/enque'
4+
require_relative '../../shared/types/rb_num2dbl_fails'
45

56
describe "SizedQueue#push" do
67
it_behaves_like :queue_enq, :push, -> { SizedQueue.new(10) }
@@ -9,3 +10,9 @@
910
describe "SizedQueue#push" do
1011
it_behaves_like :sizedqueue_enq, :push, -> n { SizedQueue.new(n) }
1112
end
13+
14+
describe "SizedQueue operations with timeout" do
15+
ruby_version_is "3.2" do
16+
it_behaves_like :rb_num2dbl_fails, nil, -> v { q = SizedQueue.new(1); q.push(1, timeout: v) }
17+
end
18+
end

spec/ruby/core/string/encode_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
encoded.encode("UTF-8").should == "ちfoofoo"
8080
end
8181

82+
it "replace multiple invalid bytes at the end with a single replacement character" do
83+
"\xE3\x81\x93\xE3\x81".encode("UTF-8", invalid: :replace).should == "\u3053\ufffd"
84+
end
85+
8286
it "replaces invalid encoding in source using a specified replacement even when a fallback is given" do
8387
encoded = "ち\xE3\x81\xFF".encode("UTF-16LE", invalid: :replace, replace: "foo", fallback: -> c { "bar" })
8488
encoded.should == "\u3061foofoo".encode("UTF-16LE")

spec/ruby/core/string/index_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@
167167
it "handles a substring in a subset encoding" do
168168
'été'.index('t'.force_encoding(Encoding::US_ASCII)).should == 1
169169
end
170+
171+
it "raises an Encoding::CompatibilityError if the encodings are incompatible" do
172+
str = 'abc'.force_encoding("ISO-2022-JP")
173+
pattern = 'b'.force_encoding("EUC-JP")
174+
175+
-> { str.index(pattern) }.should raise_error(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP")
176+
end
170177
end
171178

172179
describe "String#index with Regexp" do
@@ -312,6 +319,17 @@
312319
"われわわれ".index(/わ/, 3).should == 3
313320
end
314321

322+
ruby_bug "#19763", ""..."3.3.0" do
323+
it "raises an Encoding::CompatibilityError if the encodings are incompatible" do
324+
re = Regexp.new "れ".encode(Encoding::EUC_JP)
325+
-> do
326+
"あれ".index re
327+
end.should raise_error(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)")
328+
end
329+
end
330+
331+
# The exception message was incorrectly "incompatible character encodings: UTF-8 and EUC-JP" before 3.3.0
332+
# Still test that the right exception class is used before that.
315333
it "raises an Encoding::CompatibilityError if the encodings are incompatible" do
316334
re = Regexp.new "れ".encode(Encoding::EUC_JP)
317335
-> do

0 commit comments

Comments
 (0)