/
testStringIO.rb
206 lines (170 loc) · 4.02 KB
/
testStringIO.rb
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require 'test/minirunit'
require 'stringio'
string = <<EOF
one
two
three
EOF
strio = StringIO.new(string)
lines = []
3.times {lines << strio.readline}
test_equal(["one\n", "two\n", "three\n"], lines)
##### gets #####
strio = StringIO.new("a")
test_equal("a", strio.gets)
test_equal(nil, strio.gets)
test_equal(true, strio.eof?)
$/ = ':'
strio = StringIO.new("a:b")
test_equal('a:', strio.gets)
s = StringIO.new
$\=':'
s.puts(1,2,3)
s.print(1,2,3)
# $_ is getting lost or not working
#$_='G'
#s.print
s.printf("a %s b\n", 1)
test_equal(<<EOF, s.string)
1
2
3
123:a 1 b
EOF
s = StringIO.new("12345")
test_equal(5, s.size)
###### StringIO#new, StringIO#open ######
io = StringIO.new("foo")
test_equal(102, io.getc)
test_equal(1, io.pos)
test_equal(3, io.size)
io << "bar"
test_equal(4, io.size)
test_equal(4, io.pos)
io.rewind
test_equal("fbar", io.gets)
# JRUBY-214: new's arg 0 should have to_str called if not String, else TypeError is thrown
test_exception(TypeError) { StringIO.new(Object.new) }
StringIO.open("foo"){|io|
test_equal("foo", io.string)
}
# JRUBY-214: open's arg 0 should have to_str called if not String, else TypeError is thrown
test_exception(TypeError) { StringIO.open(Object.new){|io|} }
###### close, reopen, close_read?, close_write? ######
s = StringIO.open("A")
test_equal(false, s.closed?)
s.close_read
test_equal(false, s.closed?)
s.close_write
test_equal(true, s.closed?)
test_exception(IOError) { s.puts("HEH") }
test_exception(IOError) { s.putc('a') }
#s.seek(0)
#s.pos=0
#test_equal("A", s.string)
# JRUBY-214: reopen's arg 0 should have to_str called if not String, else TypeError is thrown
s = StringIO.open("A")
s.close_read
s.close_write
test_exception(TypeError) { s.reopen(Object.new) }
###### fcntl ######
test_exception(NotImplementedError) { StringIO.new("").fcntl() }
###### read ######
io = StringIO.new("A")
test_equal(false, io.eof?)
test_equal("A", io.read(1))
test_equal(true, io.eof?)
test_equal(nil, io.read(1))
#JRUBY-114: read with buffer sets buffer to value read (previously appended to buffer)
io = StringIO.new("A")
buf = "abc"
test_equal("A", io.read(1, buf))
test_equal("A", buf)
###### write ######
io = StringIO.new("a")
io.getc
test_equal(2, io.write("bc"))
io.rewind
test_equal("abc", io.string)
###### Misc. ######
$/="\n"
saved_stdin = $stdin
$stdin = StringIO.new("HEH\nWorld\n")
test_equal("HEH\n", gets)
$stdin = saved_stdin
n = StringIO.new
old_stdout = $stdout
$stdout = n
test_equal($>, $stdout)
puts "HEL\nEEEE\n"
n.rewind
test_equal("HEL\n", n.gets)
$stdout = old_stdout
n = StringIO.new
$> = n
puts "HEL\nEEEE\n"
n.rewind
test_equal("HEL\n", n.gets)
n = StringIO.new("123\n456\n789\n")
test_equal("123\n456\n789\n", n.gets(nil))
$/="\n"
saved_stdin = $stdin
$stdin = StringIO.new("HEH\nWorld\n")
#test_equal("HEH\n", gets)
$stdin = saved_stdin
n = StringIO.new
old_stdout = $stdout
$stdout = n
test_equal($>, $stdout)
puts "HEL\nEEEE\n"
n.rewind
test_equal("HEL\n", n.gets)
$stdout = old_stdout
n = StringIO.new
$> = n
puts "HEL\nEEEE\n"
n.rewind
test_equal("HEL\n", n.gets)
n = StringIO.new("123\n456\n789\n")
test_equal("123\n456\n789\n", n.gets(nil))
n = StringIO.new
n.puts
n.rewind
test_equal("\n", n.gets)
test_equal(nil, n.gets)
test_equal(true, n.eof?)
n = StringIO.new
buf = ""
s = StringIO.new(buf, "r+")
s.puts "HEH"
test_equal("HEH\n", buf)
n = StringIO.new
n.puts "test\n"
test_equal("test\n",n.string)
class Foo
def to_int
65
end
end
s = StringIO.new("abc", File::WRONLY)
s.putc(Foo.new)
test_equal("Abc", s.string)
test_exception(TypeError) { s.putc('') }
test_exception(Errno::EINVAL) { s.seek(-40) }
s = StringIO.new("foo\n", "r")
s.gets
s.gets
test_equal(nil ,$_)
# JRUBY-2629
test_exception(IOError) { StringIO.allocate.gets }
test_exception(IOError) { StringIO.allocate << 5 }
test_exception(IOError) { StringIO.allocate.close }
# JRUBY-4683
s = StringIO.new("1234567890")
test_equal("12345", s.read(5))
s.pos = 0
test_equal("1234567890", s.read(10)) # reaches EOF
s.pos = 0
test_equal("1234567890", s.read(11)) # EOF flag is cleared
s.pos = 0
test_equal("1234567890", s.read(10))