test/io_streams_test.rb in iostreams-0.14.0 vs test/io_streams_test.rb in iostreams-0.15.0
- old
+ new
@@ -1,84 +1,160 @@
require_relative 'test_helper'
-# Unit Test for IOStreams::File
-module Streams
- class IOStreamsTest < Minitest::Test
- describe IOStreams do
- before do
- @source_file_name = File.join(File.dirname(__FILE__), 'files', 'text.txt')
- @data = File.read(@source_file_name)
+class IOStreamsTest < Minitest::Test
+ describe IOStreams do
+ let :source_file_name do
+ File.join(__dir__, 'files', 'text.txt')
+ end
- @temp_file = Tempfile.new('iostreams')
- @target_file_name = @temp_file.to_path
- end
+ let :data do
+ File.read(source_file_name)
+ end
- after do
- @temp_file.delete if @temp_file
- end
+ let :temp_file do
+ Tempfile.new('iostreams')
+ end
- describe '.copy' do
- it 'file' do
- size = IOStreams.reader(@source_file_name) do |source_stream|
- IOStreams.writer(@target_file_name) do |target_stream|
- IOStreams.copy(source_stream, target_stream)
- end
- end
- actual = File.read(@target_file_name)
+ let :target_file_name do
+ temp_file.path
+ end
- assert_equal actual, @data
- assert_equal actual.size, size
- end
+ after do
+ temp_file.delete
+ end
- it 'stream' do
- size = File.open(@source_file_name) do |source_stream|
- IOStreams.writer(@target_file_name) do |target_stream|
- IOStreams.copy(source_stream, target_stream)
- end
+ describe '.copy' do
+ it 'streams' do
+ size = IOStreams.reader(source_file_name) do |source_stream|
+ IOStreams.writer(target_file_name) do |target_stream|
+ IOStreams.copy(source_stream, target_stream)
end
- actual = File.read(@target_file_name)
+ end
+ actual = File.read(target_file_name)
- assert_equal actual, @data
- assert_equal actual.size, size
+ assert_equal actual, data
+ assert_equal actual.size, size
+ end
+
+ it 'IO stream' do
+ size = File.open(source_file_name) do |source_stream|
+ IOStreams.writer(target_file_name) do |target_stream|
+ IOStreams.copy(source_stream, target_stream)
+ end
end
+ actual = File.read(target_file_name)
+
+ assert_equal actual, data
+ assert_equal actual.size, size
end
- describe '.copy_file' do
- it 'copies' do
- size = IOStreams.copy_file(@source_file_name, @target_file_name)
- actual = File.read(@target_file_name)
+ it 'files' do
+ size = IOStreams.copy(source_file_name, target_file_name)
+ actual = File.read(target_file_name)
- assert_equal actual, @data
- assert_equal actual.size, size
- end
+ assert_equal actual, data
+ assert_equal actual.size, size
end
+ end
- describe '.streams_for_file_name' do
- it 'file only' do
- streams = IOStreams.streams_for_file_name('a.xyz')
- assert_equal [:file], streams
- end
+ describe '.streams_for_file_name' do
+ it 'file only' do
+ streams = IOStreams.streams_for_file_name('a.xyz')
+ assert_equal [], streams
+ end
- it 'single stream' do
- streams = IOStreams.streams_for_file_name('a.gz')
- assert_equal [:gz], streams
- end
+ it 'single stream' do
+ streams = IOStreams.streams_for_file_name('a.gz')
+ assert_equal [:gz], streams
+ end
- it 'multiple streams' do
- streams = IOStreams.streams_for_file_name('a.xlsx.gz')
- assert_equal [:xlsx, :gz], streams
- end
+ it 'multiple streams' do
+ streams = IOStreams.streams_for_file_name('a.xlsx.gz')
+ assert_equal [:xlsx, :gz], streams
+ end
- it 'is case-insensitive' do
- streams = IOStreams.streams_for_file_name('a.GzIp')
- assert_equal [:gzip], streams
- end
+ it 'is case-insensitive' do
+ streams = IOStreams.streams_for_file_name('a.GzIp')
+ assert_equal [:gzip], streams
+ end
- it 'multiple streams are case-insensitive' do
- streams = IOStreams.streams_for_file_name('a.XlsX.Gz')
- assert_equal [:xlsx, :gz], streams
- end
+ it 'multiple streams are case-insensitive' do
+ streams = IOStreams.streams_for_file_name('a.XlsX.Gz')
+ assert_equal [:xlsx, :gz], streams
end
+ end
+ describe '.each_line' do
+ it 'returns a line at a time' do
+ lines = []
+ IOStreams.each_line(source_file_name) { |line| lines << line }
+ assert_equal data.lines.map(&:strip), lines
+ end
end
+
+ describe '.reader' do
+ # IOStreams.reader('abc.csv') do |io|
+ # p data while (data = io.read(128))
+ # end
+ end
+
+ describe '.each_line' do
+ # IOStreams.each_line('abc.csv') do |line|
+ # puts line
+ # end
+ end
+
+ describe '.each_row' do
+ # IOStreams.each_row('abc.csv') do |array|
+ # p array
+ # end
+ end
+
+ describe '.each_record' do
+ # IOStreams.each_record('abc.csv') do |hash|
+ # p hash
+ # end
+
+ # array = [
+ # 'name, address, zip_code',
+ # 'Jack, Down Under, 12345'
+ # ]
+ # IOStreams.each_record(array) do |hash|
+ # p hash
+ # end
+ end
+
+ describe '.writer' do
+ # IOStreams.writer('abc.csv') do |io|
+ # io.write('This')
+ # io.write(' is ')
+ # io.write(" one line\n")
+ # end
+ end
+
+ describe '.line_writer' do
+ # IOStreams.line_writer('abc.csv') do |file|
+ # file << 'these'
+ # file << 'are'
+ # file << 'all'
+ # file << 'separate'
+ # file << 'lines'
+ # end
+ end
+
+ describe '.row_writer' do
+ # IOStreams.row_writer('abc.csv') do |io|
+ # io << %w[name address zip_code]
+ # io << %w[Jack There 1234]
+ # io << ['Joe', 'Over There somewhere', 1234]
+ # end
+ end
+
+ describe '.record_writer' do
+ # IOStreams.record_writer('abc.csv') do |stream|
+ # stream << {name: 'Jack', address: 'There', zip_code: 1234}
+ # stream << {name: 'Joe', address: 'Over There somewhere', zip_code: 1234}
+ # end
+ end
+
end
end