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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
# -*- encoding: utf-8 -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "StringIO#puts when passed an Array" do
before(:each) do
@io = StringIO.new
end
it "writes each element of the passed Array to self, separated by a newline" do
@io.puts([1, 2, 3, 4])
@io.string.should == "1\n2\n3\n4\n"
@io.puts([1, 2], [3, 4])
@io.string.should == "1\n2\n3\n4\n1\n2\n3\n4\n"
end
it "flattens nested Arrays" do
@io.puts([1, [2, [3, [4]]]])
@io.string.should == "1\n2\n3\n4\n"
end
it "handles self-recursive arrays correctly" do
(ary = [5])
ary << ary
@io.puts(ary)
@io.string.should == "5\n[...]\n"
end
it "does not honor the global output record separator $\\" do
begin
old_rs, $\ = $\, "test"
@io.puts([1, 2, 3, 4])
@io.string.should == "1\n2\n3\n4\n"
ensure
$\ = old_rs
end
end
it "first tries to convert each Array element to an Array using #to_ary" do
obj = mock("Object")
obj.should_receive(:to_ary).and_return(["to_ary"])
@io.puts([obj])
@io.string.should == "to_ary\n"
end
it "then tries to convert each Array element to a String using #to_s" do
obj = mock("Object")
obj.should_receive(:to_s).and_return("to_s")
@io.puts([obj])
@io.string.should == "to_s\n"
end
end
describe "StringIO#puts when passed 1 or more objects" do
before(:each) do
@io = StringIO.new
end
it "does not honor the global output record separator $\\" do
begin
old_rs, $\ = $\, "test"
@io.puts(1, 2, 3, 4)
@io.string.should == "1\n2\n3\n4\n"
ensure
$\ = old_rs
end
end
it "does not put a \\n after each Objects that end in a newline" do
@io.puts("1\n", "2\n", "3\n")
@io.string.should == "1\n2\n3\n"
end
it "first tries to convert each Object to an Array using #to_ary" do
obj = mock("Object")
obj.should_receive(:to_ary).and_return(["to_ary"])
@io.puts(obj)
@io.string.should == "to_ary\n"
end
it "then tries to convert each Object to a String using #to_s" do
obj = mock("Object")
obj.should_receive(:to_s).and_return("to_s")
@io.puts(obj)
@io.string.should == "to_s\n"
end
it "prints a newline when passed an empty string" do
@io.puts ''
@io.string.should == "\n"
end
ruby_version_is ""..."1.9" do
it "prints a newline when passed nil" do
@io.puts nil
@io.string.should == "nil\n"
end
end
ruby_version_is "1.9" do
it "prints a newline when passed nil" do
@io.puts nil
@io.string.should == "\n"
end
end
end
describe "StringIO#puts when passed no arguments" do
before(:each) do
@io = StringIO.new
end
it "returns nil" do
@io.puts.should be_nil
end
it "prints a newline" do
@io.puts
@io.string.should == "\n"
end
it "does not honor the global output record separator $\\" do
begin
old_rs, $\ = $\, "test"
@io.puts
@io.string.should == "\n"
ensure
$\ = old_rs
end
end
end
describe "StringIO#puts when in append mode" do
before(:each) do
@io = StringIO.new("example", "a")
end
it "appends the passed argument to the end of self" do
@io.puts(", just testing")
@io.string.should == "example, just testing\n"
@io.puts(" and more testing")
@io.string.should == "example, just testing\n and more testing\n"
end
it "correctly updates self's position" do
@io.puts(", testing")
@io.pos.should eql(17)
end
end
describe "StringIO#puts when self is not writable" do
it "raises an IOError" do
io = StringIO.new("test", "r")
lambda { io.puts }.should raise_error(IOError)
io = StringIO.new("test")
io.close_write
lambda { io.puts }.should raise_error(IOError)
end
end
describe "StringIO#puts when passed an encoded string" do
it "stores the bytes unmodified" do
io = StringIO.new("")
io.puts "\x00\x01\x02"
io.puts "æåø"
io.string.should == "\x00\x01\x02\næåø\n"
end
end
|