rubyzip/test/ioextras/abstract_input_stream_test.rb

103 lines
2.4 KiB
Ruby
Raw Normal View History

require 'test_helper'
require 'zip/ioextras'
class AbstractInputStreamTest < MiniTest::Test
# AbstractInputStream subclass that provides a read method
TEST_LINES = ["Hello world#{$/}",
"this is the second line#{$/}",
2015-03-21 16:27:44 +08:00
'this is the last line']
TEST_STRING = TEST_LINES.join
class TestAbstractInputStream
include ::Zip::IOExtras::AbstractInputStream
def initialize(aString)
super()
@contents = aString
@readPointer = 0
end
def sysread(charsToRead, _buf = nil)
retVal = @contents[@readPointer, charsToRead]
@readPointer += charsToRead
2015-03-23 00:27:29 +08:00
retVal
end
def produce_input
sysread(100)
end
def input_finished?
2015-06-08 15:30:12 +08:00
@contents[@readPointer].nil?
end
end
def setup
@io = TestAbstractInputStream.new(TEST_STRING)
end
def test_gets
assert_equal(TEST_LINES[0], @io.gets)
assert_equal(1, @io.lineno)
assert_equal(TEST_LINES[0].length, @io.pos)
assert_equal(TEST_LINES[1], @io.gets)
assert_equal(2, @io.lineno)
assert_equal(TEST_LINES[2], @io.gets)
assert_equal(3, @io.lineno)
assert_nil(@io.gets)
assert_equal(4, @io.lineno)
end
2015-03-25 00:02:54 +08:00
def test_gets_multi_char_seperator
2015-03-21 16:27:44 +08:00
assert_equal('Hell', @io.gets('ll'))
assert_equal("o world#{$/}this is the second l", @io.gets('d l'))
end
LONG_LINES = [
'x' * 48 + "\r\n",
'y' * 49 + "\r\n",
2015-03-21 16:25:58 +08:00
'rest'
]
2015-03-25 00:02:54 +08:00
def test_gets_mulit_char_seperator_split
io = TestAbstractInputStream.new(LONG_LINES.join)
assert_equal(LONG_LINES[0], io.gets("\r\n"))
assert_equal(LONG_LINES[1], io.gets("\r\n"))
assert_equal(LONG_LINES[2], io.gets("\r\n"))
end
2015-03-25 00:02:54 +08:00
def test_gets_with_sep_and_index
io = TestAbstractInputStream.new(LONG_LINES.join)
assert_equal('x', io.gets("\r\n", 1))
assert_equal('x' * 47 + "\r", io.gets("\r\n", 48))
assert_equal("\n", io.gets(nil, 1))
assert_equal('yy', io.gets(nil, 2))
end
2015-03-25 00:02:54 +08:00
def test_gets_with_index
assert_equal(TEST_LINES[0], @io.gets(100))
assert_equal('this', @io.gets(4))
end
def test_each_line
lineNumber = 0
2015-03-21 16:10:37 +08:00
@io.each_line do |line|
assert_equal(TEST_LINES[lineNumber], line)
lineNumber += 1
2015-03-21 16:10:37 +08:00
end
end
def test_readlines
assert_equal(TEST_LINES, @io.readlines)
end
def test_readline
test_gets
begin
@io.readline
2019-09-26 20:58:43 +08:00
raise 'EOFError expected'
rescue EOFError
end
end
end