1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Kernel#puts" do
before :each do
@stdout = $stdout
@name = tmp("kernel_puts.txt")
$stdout = new_io @name
end
after :each do
$stdout.close
$stdout = @stdout
rm_r @name
end
it "is a private method" do
Kernel.should have_private_instance_method(:puts)
end
it "writes just a newline when given no args" do
$stdout.should_receive(:write).with("\n")
Kernel.puts.should == nil
end
ruby_version_is ""..."1.9" do
it "writes nil with a newline when given nil as an arg" do
$stdout.should_receive(:write).with("nil")
$stdout.should_receive(:write).with("\n")
Kernel.puts(nil).should == nil
end
end
# Declared intentional in
# http://redmine.ruby-lang.org/issues/show/1748
ruby_version_is "1.9" do
it "writes a newline when given nil as an arg" do
$stdout.should_receive(:write).with('')
$stdout.should_receive(:write).with("\n")
Kernel.puts(nil).should == nil
end
end
it "calls to_s before writing non-string objects" do
object = mock('hola')
object.should_receive(:to_s).and_return("hola")
$stdout.should_receive(:write).with("hola")
$stdout.should_receive(:write).with("\n")
Kernel.puts(object).should == nil
end
it "writes each arg if given several" do
$stdout.should_receive(:write).with("1")
$stdout.should_receive(:write).with("two")
$stdout.should_receive(:write).with("3")
$stdout.should_receive(:write).with("\n").exactly(3).times
Kernel.puts(1, "two", 3).should == nil
end
it "flattens a nested array before writing it" do
$stdout.should_receive(:write).with("1")
$stdout.should_receive(:write).with("2")
$stdout.should_receive(:write).with("3")
$stdout.should_receive(:write).with("\n").exactly(3).times
Kernel.puts([1, 2, [3]]).should == nil
end
it "writes [...] for a recursive array arg" do
x = []
x << 2 << x
$stdout.should_receive(:write).with("2")
$stdout.should_receive(:write).with("[...]")
$stdout.should_receive(:write).with("\n").exactly(2).times
Kernel.puts(x).should == nil
end
it "writes a newline after objects that do not end in newlines" do
$stdout.should_receive(:write).with("5")
$stdout.should_receive(:write).with("\n")
Kernel.puts(5).should == nil
end
it "does not write a newline after objects that end in newlines" do
$stdout.should_receive(:write).with("5\n")
Kernel.puts("5\n").should == nil
end
it "ignores the $/ separator global" do
$/ = ":"
$stdout.should_receive(:write).with("5")
$stdout.should_receive(:write).with("\n")
Kernel.puts(5).should == nil
$/ = "\n"
end
end
describe "Kernel.puts" do
it "needs to be reviewed for spec completeness"
end
|