1require_relative '../../spec_helper'
2require 'stringio'
3
4describe "StringIO#initialize when passed [Object, mode]" do
5  before :each do
6    @io = StringIO.allocate
7  end
8
9  it "uses the passed Object as the StringIO backend" do
10    @io.send(:initialize, str = "example", "r")
11    @io.string.should equal(str)
12  end
13
14  it "sets the mode based on the passed mode" do
15    io = StringIO.allocate
16    io.send(:initialize, "example", "r")
17    io.closed_read?.should be_false
18    io.closed_write?.should be_true
19
20    io = StringIO.allocate
21    io.send(:initialize, "example", "rb")
22    io.closed_read?.should be_false
23    io.closed_write?.should be_true
24
25    io = StringIO.allocate
26    io.send(:initialize, "example", "r+")
27    io.closed_read?.should be_false
28    io.closed_write?.should be_false
29
30    io = StringIO.allocate
31    io.send(:initialize, "example", "rb+")
32    io.closed_read?.should be_false
33    io.closed_write?.should be_false
34
35    io = StringIO.allocate
36    io.send(:initialize, "example", "w")
37    io.closed_read?.should be_true
38    io.closed_write?.should be_false
39
40    io = StringIO.allocate
41    io.send(:initialize, "example", "wb")
42    io.closed_read?.should be_true
43    io.closed_write?.should be_false
44
45    io = StringIO.allocate
46    io.send(:initialize, "example", "w+")
47    io.closed_read?.should be_false
48    io.closed_write?.should be_false
49
50    io = StringIO.allocate
51    io.send(:initialize, "example", "wb+")
52    io.closed_read?.should be_false
53    io.closed_write?.should be_false
54
55    io = StringIO.allocate
56    io.send(:initialize, "example", "a")
57    io.closed_read?.should be_true
58    io.closed_write?.should be_false
59
60    io = StringIO.allocate
61    io.send(:initialize, "example", "ab")
62    io.closed_read?.should be_true
63    io.closed_write?.should be_false
64
65    io = StringIO.allocate
66    io.send(:initialize, "example", "a+")
67    io.closed_read?.should be_false
68    io.closed_write?.should be_false
69
70    io = StringIO.allocate
71    io.send(:initialize, "example", "ab+")
72    io.closed_read?.should be_false
73    io.closed_write?.should be_false
74  end
75
76  it "allows passing the mode as an Integer" do
77    io = StringIO.allocate
78    io.send(:initialize, "example", IO::RDONLY)
79    io.closed_read?.should be_false
80    io.closed_write?.should be_true
81
82    io = StringIO.allocate
83    io.send(:initialize, "example", IO::RDWR)
84    io.closed_read?.should be_false
85    io.closed_write?.should be_false
86
87    io = StringIO.allocate
88    io.send(:initialize, "example", IO::WRONLY)
89    io.closed_read?.should be_true
90    io.closed_write?.should be_false
91
92    io = StringIO.allocate
93    io.send(:initialize, "example", IO::WRONLY | IO::TRUNC)
94    io.closed_read?.should be_true
95    io.closed_write?.should be_false
96
97    io = StringIO.allocate
98    io.send(:initialize, "example", IO::RDWR | IO::TRUNC)
99    io.closed_read?.should be_false
100    io.closed_write?.should be_false
101
102    io = StringIO.allocate
103    io.send(:initialize, "example", IO::WRONLY | IO::APPEND)
104    io.closed_read?.should be_true
105    io.closed_write?.should be_false
106
107    io = StringIO.allocate
108    io.send(:initialize, "example", IO::RDWR | IO::APPEND)
109    io.closed_read?.should be_false
110    io.closed_write?.should be_false
111  end
112
113  it "raises a #{frozen_error_class} when passed a frozen String in truncate mode as StringIO backend" do
114    io = StringIO.allocate
115    lambda { io.send(:initialize, "example".freeze, IO::TRUNC) }.should raise_error(frozen_error_class)
116  end
117
118  it "tries to convert the passed mode to a String using #to_str" do
119    obj = mock('to_str')
120    obj.should_receive(:to_str).and_return("r")
121    @io.send(:initialize, "example", obj)
122
123    @io.closed_read?.should be_false
124    @io.closed_write?.should be_true
125  end
126
127  it "raises an Errno::EACCES error when passed a frozen string with a write-mode" do
128    (str = "example").freeze
129    lambda { @io.send(:initialize, str, "r+") }.should raise_error(Errno::EACCES)
130    lambda { @io.send(:initialize, str, "w") }.should raise_error(Errno::EACCES)
131    lambda { @io.send(:initialize, str, "a") }.should raise_error(Errno::EACCES)
132  end
133end
134
135describe "StringIO#initialize when passed [Object]" do
136  before :each do
137    @io = StringIO.allocate
138  end
139
140  it "uses the passed Object as the StringIO backend" do
141    @io.send(:initialize, str = "example")
142    @io.string.should equal(str)
143  end
144
145  it "sets the mode to read-write" do
146    @io.send(:initialize, "example")
147    @io.closed_read?.should be_false
148    @io.closed_write?.should be_false
149  end
150
151  it "tries to convert the passed Object to a String using #to_str" do
152    obj = mock('to_str')
153    obj.should_receive(:to_str).and_return("example")
154    @io.send(:initialize, obj)
155    @io.string.should == "example"
156  end
157
158  it "automatically sets the mode to read-only when passed a frozen string" do
159    (str = "example").freeze
160    @io.send(:initialize, str)
161    @io.closed_read?.should be_false
162    @io.closed_write?.should be_true
163  end
164end
165
166describe "StringIO#initialize when passed no arguments" do
167  before :each do
168    @io = StringIO.allocate
169  end
170
171  it "is private" do
172    StringIO.should have_private_instance_method(:initialize)
173  end
174
175  it "sets the mode to read-write" do
176    @io.send(:initialize, "example")
177    @io.closed_read?.should be_false
178    @io.closed_write?.should be_false
179  end
180
181  it "uses an empty String as the StringIO backend" do
182    @io.send(:initialize)
183    @io.string.should == ""
184  end
185end
186