Skip to content

Commit 87b1631

Browse files
edgibbsandrykonchin
authored andcommitted
Add specs for Kernel#p return values
Kernel#p's return value had no coverage for 3 scenarios: - nil for no arguments - the argument itself (not a 1-element Array) for a single argument - a new Array for multiple arguments That passthrough is what lets `p` be used inline where `puts` cannot, since `puts` always returns nil. The nil cases are asserted on the existing "prints nothing" examples, as `core/io/puts_spec.rb` does for IO#puts. The passthrough cases get their own examples, as `core/io/output_spec.rb` does for IO#<<. Specs are under Kernel#p only, following a1f57e8. Matchers follow #1350 (.should == nil, .should.equal?).
1 parent bb3e38b commit 87b1631

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

core/kernel/p_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,32 @@
6969
end
7070

7171
it "prints nothing if no argument is given" do
72-
-> { p }.should output("")
72+
-> { p.should == nil }.should output("")
7373
end
7474

7575
it "prints nothing if called splatting an empty Array" do
76-
-> { p(*[]) }.should output("")
76+
-> { p(*[]).should == nil }.should output("")
77+
end
78+
79+
it "returns the argument if a single argument is given" do
80+
o = mock("Inspector Gadget")
81+
o.should_receive(:inspect).any_number_of_times.and_return "Next time, Gadget, NEXT TIME!"
82+
83+
-> { p(o).should.equal?(o) }.should output("Next time, Gadget, NEXT TIME!\n")
84+
end
85+
86+
it "returns the argument if called splatting a single-element Array" do
87+
o = mock("Inspector Gadget")
88+
o.should_receive(:inspect).any_number_of_times.and_return "Next time, Gadget, NEXT TIME!"
89+
90+
-> { p(*[o]).should.equal?(o) }.should output("Next time, Gadget, NEXT TIME!\n")
91+
end
92+
93+
it "returns an Array of the arguments if multiple arguments are given" do
94+
o = mock("Inspector Gadget")
95+
o.should_receive(:inspect).any_number_of_times.and_return "Next time, Gadget, NEXT TIME!"
96+
97+
-> { p(o, o).should == [o, o] }.should output("Next time, Gadget, NEXT TIME!\nNext time, Gadget, NEXT TIME!\n")
7798
end
7899

79100
# Not sure how to spec this, but wanted to note the behavior here

0 commit comments

Comments
 (0)