Skip to content

Commit

Permalink
Improve specs for Kernel#warn and Warning.warn
Browse files Browse the repository at this point in the history
* Move specs in the right file, this is behavior of Kernel#warn.
  • Loading branch information
eregon committed Nov 29, 2021
1 parent 61764dc commit d797c39
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/kernel/warn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,70 @@ def o.to_sym; :deprecated; end
-> { warn('foo', **h) }.should complain("foo\n")
end

ruby_version_is '3.0' do
it "calls Warning.warn without keyword arguments if Warning.warn does not accept keyword arguments" do
verbose = $VERBOSE
$VERBOSE = false
class << Warning
alias_method :_warn, :warn
def warn(message)
ScratchPad.record(message)
end
end

begin
ScratchPad.clear
Kernel.warn("Chunky bacon!")
ScratchPad.recorded.should == "Chunky bacon!\n"

Kernel.warn("Deprecated bacon!", category: :deprecated)
ScratchPad.recorded.should == "Deprecated bacon!\n"
ensure
class << Warning
remove_method :warn
alias_method :warn, :_warn
remove_method :_warn
end
$VERBOSE = verbose
end
end

it "calls Warning.warn with category: nil if Warning.warn accepts keyword arguments" do
Warning.should_receive(:warn).with("Chunky bacon!\n", category: nil)
verbose = $VERBOSE
$VERBOSE = false
begin
Kernel.warn("Chunky bacon!")
ensure
$VERBOSE = verbose
end
end

it "calls Warning.warn with given category keyword converted to a symbol" do
Warning.should_receive(:warn).with("Chunky bacon!\n", category: :deprecated)
verbose = $VERBOSE
$VERBOSE = false
begin
Kernel.warn("Chunky bacon!", category: 'deprecated')
ensure
$VERBOSE = verbose
end
end
end

ruby_version_is ''...'3.0' do
it "calls Warning.warn with no keyword arguments" do
Warning.should_receive(:warn).with("Chunky bacon!\n")
verbose = $VERBOSE
$VERBOSE = false
begin
Kernel.warn("Chunky bacon!")
ensure
$VERBOSE = verbose
end
end
end

it "does not call Warning.warn if self is the Warning module" do
# RubyGems redefines Kernel#warn so we need to use a subprocess and disable RubyGems here
code = <<-RUBY
Expand Down

0 comments on commit d797c39

Please sign in to comment.