3 # -*- ruby -*- vim:set ft=ruby et sw=2 sts=2:
17 @string = StringIO.new(*args)
20 @string.read(length) or raise EOFError, "end of file reached", caller
25 def sysseek(offset, whence = IO::SEEK_SET)
26 @string.seek(offset,whence)
29 DELEGATORS = [:close_read,:close_write,:closed_read?,:closed_write?,:reopen]
30 def_delegators :@string, *DELEGATORS
31 # def close_read() @string.close_read end
32 # def close_write() @string.close_write end
33 # def closed_read?() @string.closed_read? end
34 # def closed_write?()@string.closed_write? end
35 # def close() @string.close end
38 require File.join(File.dirname(__FILE__),'rot13_filter')
39 require File.join(File.dirname(__FILE__),'io_loop')
40 require File.join(File.dirname(__FILE__),'read_joiner')
43 def initialize(test,initial,*args)
45 @string1 = initial.dup
46 @string2 = initial.dup
47 @s1 = StringIO.new(@string1,*args)
48 @s2 = SimpleIO.new(@string2,*args)
57 @test.assert_equal @string1, @string2
60 def method_missing(*args)
61 value1 = @s1.__send__(*args) rescue $!.class
62 value2 = @s2.__send__(*args) rescue $!.class
63 value2 = value1 if value1 == @s1 && value2 == @s2
64 @test.assert_equal value1, value2
70 class IOMixinsTest < Test::Unit::TestCase
73 SimpleIO.open(":)\n") do |io|
74 assert_nothing_raised { io.gets }
75 assert_raise(NotImplementedError) { io.fcntl }
79 def test_enough_methods
80 junk = Object.instance_methods | Enumerable.instance_methods
81 good = IO.instance_methods(false) & StringIO.instance_methods(false)
82 mine = SimpleIO.instance_methods
83 assert_equal [], good - junk - mine
88 io = IOLoop.open(string)
89 assert_equal ?f, io.getc
91 assert_equal ?g, io.getc
92 io.putc "lqqqqq" # Should use first character
93 assert_equal "o\n", io.gets
95 assert_equal "flo\n", io.readline
96 assert_equal "baz\n", io.readline
98 io.instance_variable_set("@closed_read",false)
99 assert_equal true, io.closed_read?
100 assert_raise(IOError) { io.read }
101 io = IOLoop.open("foo")
102 assert_raise(IOError) { io.read }
106 File.open(RUBY_PLATFORM =~ /win32/ ? "NUL:" : "/dev/null")
117 def assert_identical_response(object1,object2,method,*args,&block)
118 value1 = object1.send(method,*args,&block) rescue $!.class
119 value2 = object2.send(method,*args,&block) rescue $!.class
120 assert_equal value1, value2, "#{object1.class} gave #{value1.inspect} while #{object2.class} gave #{value2.inspect}"
123 def initialize_both(*args)
124 @both = PairTester.new(self,*args)
127 def test_simple_io_empty_read
128 [lambda {empty_file},lambda {empty_string}].each do |l|
129 assert_identical_response l.call,empty_simple,:read
130 assert_identical_response l.call,empty_simple,:read,0
131 assert_identical_response l.call,empty_simple,:read,2
135 def test_simple_io_read
136 initialize_both("Hello\nworld!")
148 @both.read(4, "buffer")
170 def test_simple_io_gets
171 # io = SimpleIO.open(StringIO.new(<<-EOF))
186 initialize_both(string)
193 def test_simple_io_write
194 initialize_both("x","w")
197 @both.eof # => IOError
198 @both.print("Hello ")
204 @both.printf("%4d",17)
205 @both.putc(1..3) # => TypeError
206 @both.putc(:bad) # => TypeError
215 def test_simple_io_seek
216 initialize_both("Look ma!","r+")
220 @both.seek(3,IO::SEEK_SET)
224 @both.seek(-2,IO::SEEK_CUR)
227 @both.seek(-3,IO::SEEK_END)
232 def test_missing_methods
233 readable = Class.new do
236 writable = Class.new do
249 streamable = Class.new(writable) do
262 assert_raise(NotImplementedError) {readable.new.close}
263 assert_raise(NotImplementedError) {readable.new.closed?}
265 assert_equal false, w.closed_write?
266 assert_nothing_thrown {w.close}
267 assert_raise(IOError) {w.close}
268 assert_equal true, w.closed?
270 assert_equal false, s.closed?
271 assert_nothing_thrown {s.close_read}
272 assert_nothing_thrown {s.close}
273 assert_raise(IOError) {s.close_write}
274 assert_raise(IOError) {s.close}
275 assert_equal true, s.closed?
278 def test_open_readers
279 assert_raise(EOFError) { SimpleIO.open("") {|io|io.sysread(1)}}
280 assert_equal "READ", SimpleIO.read("README",4)
281 assert_equal "x" * 1025, SimpleIO.read("x"*1025)
282 assert_equal ["foo\n","bar"], SimpleIO.readlines("foo\nbar")
283 assert_raise(LocalJumpError) { SimpleIO.foreach("foo\nbar") }
287 string = "Puhaxl onpba!"
288 stringio = StringIO.open(string,"r")
289 filtered = Rot13Filter.open(stringio)
290 assert_equal "Chunky bacon!", filtered.read
291 stringio = StringIO.open(string,"r+")
292 filtered = Rot13Filter.open(stringio)
293 filtered.write("Rotten")
294 assert_equal "Ebggra onpba!", string
298 joiner = ReadJoiner.new(StringIO.new("fo"),StringIO.new("o\nbar\n"))
299 assert_equal "foo\n", joiner.readline
300 assert_nil joiner.close
301 assert_equal true, joiner.closed?