Skip to content

Commit

Permalink
Merge pull request #1074 from herwinw/signal_argument_coercion
Browse files Browse the repository at this point in the history
Add specs for argument coercion (#to_int, #to_str) in Signal
  • Loading branch information
andrykonchin authored Sep 27, 2023
2 parents db5f026 + e8c660f commit ee20cf3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/signal/signame_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@
Signal.signame(-1).should == nil
end

it "calls #to_int on an object to convert to an Integer" do
obj = mock('signal')
obj.should_receive(:to_int).and_return(0)
Signal.signame(obj).should == "EXIT"
end

it "raises a TypeError when the passed argument can't be coerced to Integer" do
-> { Signal.signame("hello") }.should raise_error(TypeError)
end

it "raises a TypeError when the passed argument responds to #to_int but does not return an Integer" do
obj = mock('signal')
obj.should_receive(:to_int).and_return('not an int')
-> { Signal.signame(obj) }.should raise_error(TypeError)
end

platform_is_not :windows do
it "the original should take precedence over alias when looked up by number" do
Signal.signame(Signal.list["ABRT"]).should == "ABRT"
Expand Down
19 changes: 19 additions & 0 deletions core/signal/trap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@
Signal.trap(:HUP, @saved_trap).should equal(@proc)
end

it "calls #to_str on an object to convert to a String" do
obj = mock("signal")
obj.should_receive(:to_str).exactly(2).times.and_return("HUP")
Signal.trap obj, @proc
Signal.trap(obj, @saved_trap).should equal(@proc)
end

it "accepts Integer values" do
hup = Signal.list["HUP"]
Signal.trap hup, @proc
Signal.trap(hup, @saved_trap).should equal(@proc)
end

it "does not call #to_int on an object to convert to an Integer" do
obj = mock("signal")
obj.should_not_receive(:to_int)
-> { Signal.trap obj, @proc }.should raise_error(ArgumentError, /bad signal type/)
end

it "raises ArgumentError when passed unknown signal" do
-> { Signal.trap(300) { } }.should raise_error(ArgumentError, "invalid signal number (300)")
-> { Signal.trap("USR10") { } }.should raise_error(ArgumentError, "unsupported signal `SIGUSR10'")
Expand Down

0 comments on commit ee20cf3

Please sign in to comment.