test/line_reader_test.rb in iostreams-0.20.3 vs test/line_reader_test.rb in iostreams-1.0.0.beta

- old
+ new

@@ -34,31 +34,31 @@ describe 'embedded_within_quotes' do describe 'csv file' do it 'fails to keep embedded lines if flag is not set' do lines = [] - IOStreams::Line::Reader.open(csv_file) do |io| + IOStreams::Line::Reader.file(csv_file) do |io| io.each do |line| lines << line end end assert_equal 7, lines.count end it 'keeps embedded lines if flag is set' do lines = [] - IOStreams::Line::Reader.open(csv_file, embedded_within: '"') do |io| + IOStreams::Line::Reader.file(csv_file, embedded_within: '"') do |io| io.each do |line| lines << line end end assert_equal 4, lines.count end it 'raises error for unclosed quote' do assert_raises(RuntimeError) do - IOStreams::Line::Reader.open(unclosed_quote_file, embedded_within: '"') do |io| + IOStreams::Line::Reader.file(unclosed_quote_file, embedded_within: '"') do |io| io.each do |line| end end end end @@ -66,21 +66,21 @@ end describe '#each' do it 'each_line file' do lines = [] - count = IOStreams::Line::Reader.open(file_name) do |io| + count = IOStreams::Line::Reader.file(file_name) do |io| io.each { |line| lines << line } end assert_equal data, lines assert_equal data.size, count end it 'each_line stream' do lines = [] count = File.open(file_name) do |file| - IOStreams::Line::Reader.open(file) do |io| + IOStreams::Line::Reader.stream(file) do |io| io.each { |line| lines << line } end end assert_equal data, lines assert_equal data.size, count @@ -88,21 +88,21 @@ ["\r\n", "\n", "\r"].each do |delimiter| it "autodetect delimiter: #{delimiter.inspect}" do lines = [] stream = StringIO.new(data.join(delimiter)) - count = IOStreams::Line::Reader.open(stream, buffer_size: 15) do |io| + count = IOStreams::Line::Reader.stream(stream, buffer_size: 15) do |io| io.each { |line| lines << line } end assert_equal data, lines assert_equal data.size, count end it "single read autodetect delimiter: #{delimiter.inspect}" do lines = [] stream = StringIO.new(data.join(delimiter)) - count = IOStreams::Line::Reader.open(stream) do |io| + count = IOStreams::Line::Reader.stream(stream) do |io| io.each { |line| lines << line } end assert_equal data, lines assert_equal data.size, count end @@ -110,11 +110,11 @@ ['@', 'BLAH'].each do |delimiter| it "reads delimited #{delimiter.inspect}" do lines = [] stream = StringIO.new(data.join(delimiter)) - count = IOStreams::Line::Reader.open(stream, buffer_size: 15, delimiter: delimiter) do |io| + count = IOStreams::Line::Reader.stream(stream, buffer_size: 15, delimiter: delimiter) do |io| io.each { |line| lines << line } end assert_equal data, lines assert_equal data.size, count end @@ -122,11 +122,11 @@ it 'reads binary delimited' do delimiter = "\x01" lines = [] stream = StringIO.new(data.join(delimiter).encode('ASCII-8BIT')) - count = IOStreams::Line::Reader.open(stream, buffer_size: 15, delimiter: delimiter) do |io| + count = IOStreams::Line::Reader.stream(stream, buffer_size: 15, delimiter: delimiter) do |io| io.each { |line| lines << line } end assert_equal data, lines assert_equal data.size, count end @@ -139,11 +139,11 @@ it 'reads delimiter in first block, no delimiter at end' do data = [short_line, longer_line].join(delimiter) buffer_size = short_line.length + delimiter.size + (longer_line.size / 2) stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal short_line, io.readline assert_equal longer_line, io.readline @@ -156,11 +156,11 @@ it 'reads delimiter in second block, no delimiter at end' do data = [longer_line, short_line, short_line].join(delimiter) buffer_size = (longer_line.length + delimiter.size + 5) / 2 stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal longer_line, io.readline assert_equal short_line, io.readline assert_equal short_line, io.readline @@ -172,11 +172,11 @@ it 'reads delimiter split across first and second blocks' do data = [longer_line, short_line, short_line].join(delimiter) buffer_size = longer_line.length + 1 stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal longer_line, io.readline assert_equal short_line, io.readline assert_equal short_line, io.readline @@ -189,11 +189,11 @@ delimiter = '@' data = [longer_line, short_line, longer_line].join(delimiter) + delimiter buffer_size = longer_line.length + 1 stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal "\n", io.delimiter, -> { io.delimiter.ai } assert_equal data, io.readline assert io.eof? assert_nil io.readline @@ -203,11 +203,11 @@ it 'reads small file with no matching delimiter' do data = short_line buffer_size = short_line.length + 100 stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal "\n", io.delimiter, -> { io.delimiter.ai } assert_equal short_line, io.readline assert io.eof? assert_nil io.readline @@ -218,11 +218,11 @@ delimiter = '@' data = [longer_line, short_line, longer_line].join(delimiter) + delimiter buffer_size = longer_line.length + 1 stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size, delimiter: delimiter) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size, delimiter: delimiter) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal longer_line, io.readline assert_equal short_line, io.readline assert_equal longer_line, io.readline @@ -234,11 +234,11 @@ it 'reads last line with the multi-byte delimiter as the last bytes' do data = [longer_line, short_line, longer_line].join(delimiter) + delimiter buffer_size = longer_line.length + 1 stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal longer_line, io.readline assert_equal short_line, io.readline assert_equal longer_line, io.readline @@ -252,11 +252,11 @@ it 'delimiter at the end' do data = [longer_line, short_line, longer_line].join(delimiter) + delimiter stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal longer_line, io.readline assert_equal short_line, io.readline assert_equal longer_line, io.readline @@ -267,11 +267,11 @@ it 'no delimiter at the end' do data = [longer_line, short_line, longer_line].join(delimiter) stream = StringIO.new(data) - IOStreams::Line::Reader.open(stream, buffer_size: buffer_size) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: buffer_size) do |io| refute io.eof? assert_equal delimiter, io.delimiter, -> { io.delimiter.ai } assert_equal longer_line, io.readline assert_equal short_line, io.readline assert_equal longer_line, io.readline @@ -281,19 +281,19 @@ end end it 'reads empty file' do stream = StringIO.new - IOStreams::Line::Reader.open(stream) do |io| + IOStreams::Line::Reader.stream(stream) do |io| assert io.eof? end end it 'prevents denial of service' do data = 'a' * IOStreams::Line::Reader::MAX_BLOCKS_MULTIPLIER + 'a' stream = StringIO.new(data) assert_raises IOStreams::Errors::DelimiterNotFound do - IOStreams::Line::Reader.open(stream, buffer_size: 1) do |io| + IOStreams::Line::Reader.stream(stream, buffer_size: 1) do |io| end end end end