Skip to content

Commit d797c39

Browse files
committed
Improve specs for Kernel#warn and Warning.warn
* Move specs in the right file, this is behavior of Kernel#warn.
1 parent 61764dc commit d797c39

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

core/kernel/warn_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,70 @@ def o.to_sym; :deprecated; end
212212
-> { warn('foo', **h) }.should complain("foo\n")
213213
end
214214

215+
ruby_version_is '3.0' do
216+
it "calls Warning.warn without keyword arguments if Warning.warn does not accept keyword arguments" do
217+
verbose = $VERBOSE
218+
$VERBOSE = false
219+
class << Warning
220+
alias_method :_warn, :warn
221+
def warn(message)
222+
ScratchPad.record(message)
223+
end
224+
end
225+
226+
begin
227+
ScratchPad.clear
228+
Kernel.warn("Chunky bacon!")
229+
ScratchPad.recorded.should == "Chunky bacon!\n"
230+
231+
Kernel.warn("Deprecated bacon!", category: :deprecated)
232+
ScratchPad.recorded.should == "Deprecated bacon!\n"
233+
ensure
234+
class << Warning
235+
remove_method :warn
236+
alias_method :warn, :_warn
237+
remove_method :_warn
238+
end
239+
$VERBOSE = verbose
240+
end
241+
end
242+
243+
it "calls Warning.warn with category: nil if Warning.warn accepts keyword arguments" do
244+
Warning.should_receive(:warn).with("Chunky bacon!\n", category: nil)
245+
verbose = $VERBOSE
246+
$VERBOSE = false
247+
begin
248+
Kernel.warn("Chunky bacon!")
249+
ensure
250+
$VERBOSE = verbose
251+
end
252+
end
253+
254+
it "calls Warning.warn with given category keyword converted to a symbol" do
255+
Warning.should_receive(:warn).with("Chunky bacon!\n", category: :deprecated)
256+
verbose = $VERBOSE
257+
$VERBOSE = false
258+
begin
259+
Kernel.warn("Chunky bacon!", category: 'deprecated')
260+
ensure
261+
$VERBOSE = verbose
262+
end
263+
end
264+
end
265+
266+
ruby_version_is ''...'3.0' do
267+
it "calls Warning.warn with no keyword arguments" do
268+
Warning.should_receive(:warn).with("Chunky bacon!\n")
269+
verbose = $VERBOSE
270+
$VERBOSE = false
271+
begin
272+
Kernel.warn("Chunky bacon!")
273+
ensure
274+
$VERBOSE = verbose
275+
end
276+
end
277+
end
278+
215279
it "does not call Warning.warn if self is the Warning module" do
216280
# RubyGems redefines Kernel#warn so we need to use a subprocess and disable RubyGems here
217281
code = <<-RUBY

0 commit comments

Comments
 (0)