2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
require 'test_helper'
|
|
|
|
require 'zip/ioextras'
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
class AbstractInputStreamTest < MiniTest::Test
|
2014-01-21 05:31:06 +08:00
|
|
|
# AbstractInputStream subclass that provides a read method
|
|
|
|
|
2019-09-27 04:46:00 +08:00
|
|
|
TEST_LINES = ["Hello world#{$INPUT_RECORD_SEPARATOR}",
|
|
|
|
"this is the second line#{$INPUT_RECORD_SEPARATOR}",
|
2015-03-21 16:27:44 +08:00
|
|
|
'this is the last line']
|
2014-01-21 05:31:06 +08:00
|
|
|
TEST_STRING = TEST_LINES.join
|
|
|
|
class TestAbstractInputStream
|
|
|
|
include ::Zip::IOExtras::AbstractInputStream
|
|
|
|
|
2020-02-18 07:13:46 +08:00
|
|
|
def initialize(string)
|
2014-01-21 05:31:06 +08:00
|
|
|
super()
|
2020-02-18 07:13:46 +08:00
|
|
|
@contents = string
|
2020-02-19 18:52:49 +08:00
|
|
|
@read_ptr = 0
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2020-02-18 07:13:46 +08:00
|
|
|
def sysread(chars_to_read, _buf = nil)
|
2020-02-19 18:52:49 +08:00
|
|
|
ret_val = @contents[@read_ptr, chars_to_read]
|
|
|
|
@read_ptr += chars_to_read
|
|
|
|
ret_val
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def produce_input
|
|
|
|
sysread(100)
|
|
|
|
end
|
|
|
|
|
|
|
|
def input_finished?
|
2020-02-19 18:52:49 +08:00
|
|
|
@contents[@read_ptr].nil?
|
2014-01-21 05:31:06 +08:00
|
|
|
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)
|
2017-01-08 14:31:51 +08:00
|
|
|
assert_nil(@io.gets)
|
2014-01-21 05:31:06 +08:00
|
|
|
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'))
|
2019-09-27 04:46:00 +08:00
|
|
|
assert_equal("o world#{$INPUT_RECORD_SEPARATOR}this is the second l", @io.gets('d l'))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
LONG_LINES = [
|
2015-03-23 01:03:50 +08:00
|
|
|
'x' * 48 + "\r\n",
|
|
|
|
'y' * 49 + "\r\n",
|
2015-03-21 16:25:58 +08:00
|
|
|
'rest'
|
2014-01-21 05:31:06 +08:00
|
|
|
]
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_gets_mulit_char_seperator_split
|
2014-01-21 05:31:06 +08:00
|
|
|
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
|
2014-01-21 05:31:06 +08:00
|
|
|
io = TestAbstractInputStream.new(LONG_LINES.join)
|
|
|
|
assert_equal('x', io.gets("\r\n", 1))
|
2015-03-23 01:03:50 +08:00
|
|
|
assert_equal('x' * 47 + "\r", io.gets("\r\n", 48))
|
2014-01-21 05:31:06 +08:00
|
|
|
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
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(TEST_LINES[0], @io.gets(100))
|
|
|
|
assert_equal('this', @io.gets(4))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_line
|
2020-02-19 18:52:49 +08:00
|
|
|
line_num = 0
|
2015-03-21 16:10:37 +08:00
|
|
|
@io.each_line do |line|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert_equal(TEST_LINES[line_num], line)
|
|
|
|
line_num += 1
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
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'
|
2014-01-21 05:31:06 +08:00
|
|
|
rescue EOFError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|