test/stream_test.rb in iostreams-1.0.0.beta vs test/stream_test.rb in iostreams-1.0.0.beta2

- old
+ new

@@ -95,22 +95,316 @@ end describe '.each_record' do end - describe '.writer' do - end + describe '#writer' do + describe "#write" do + it 'one block' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream.write("Hello World") + end + assert_equal "Hello World", io.string + end - describe '.writer' do + it 'multiple blocks' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream.write("He") + stream.write("l") + stream.write("lo ") + stream.write("World") + end + assert_equal "Hello World", io.string + end + + it 'empty blocks' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream.write("") + stream.write("He") + stream.write("") + stream.write("l") + stream.write("") + stream.write("lo ") + stream.write("World") + stream.write("") + end + assert_equal "Hello World", io.string + end + + it 'nil blocks' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream.write(nil) + stream.write("He") + stream.write(nil) + stream.write("l") + stream.write(nil) + stream.write("lo ") + stream.write("World") + stream.write(nil) + end + assert_equal "Hello World", io.string + end + end + + describe "#<<" do + it 'one block' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream << "Hello World" + end + assert_equal "Hello World", io.string + end + + it 'multiple blocks' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream << "He" + stream << "l" << "lo " << "World" + end + assert_equal "Hello World", io.string + end + + it 'empty blocks' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream << "" + stream << "He" << "" << "l" << "" + stream << "lo " << "World" + stream << "" + end + assert_equal "Hello World", io.string + end + + it 'nil blocks' do + io = StringIO.new + IOStreams::Stream.new(io).writer do |stream| + stream << nil + stream << "He" << nil << "l" << nil + stream << "lo " << "World" + stream << nil + end + assert_equal "Hello World", io.string + end + end end - describe '.line_writer' do + describe '#line_writer' do + describe "#write" do + it 'one block' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream.write("Hello World") + end + assert_equal "Hello World\n", io.string + end + + it 'multiple blocks' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream.write("He") + stream.write("l") + stream.write("lo ") + stream.write("World") + end + assert_equal "He\nl\nlo \nWorld\n", io.string + end + + it 'empty blocks' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream.write("") + stream.write("He") + stream.write("") + stream.write("l") + stream.write("") + stream.write("lo ") + stream.write("World") + stream.write("") + end + assert_equal "\nHe\n\nl\n\nlo \nWorld\n\n", io.string, io.string.inspect + end + + it 'nil blocks' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream.write(nil) + stream.write("He") + stream.write(nil) + stream.write("l") + stream.write(nil) + stream.write("lo ") + stream.write("World") + stream.write(nil) + end + assert_equal "\nHe\n\nl\n\nlo \nWorld\n\n", io.string, io.string.inspect + end + end + + describe "#<<" do + it 'one block' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream << "Hello World" + end + assert_equal "Hello World\n", io.string + end + + it 'multiple blocks' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream << "He" + stream << "l" << "lo " << "World" + end + assert_equal "He\nl\nlo \nWorld\n", io.string + end + + it 'empty blocks' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream << "" + stream << "He" << "" << "l" << "" + stream << "lo " << "World" + stream << "" + end + assert_equal "\nHe\n\nl\n\nlo \nWorld\n\n", io.string + end + + it 'nil blocks' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream << nil + stream << "He" << nil << "l" << nil + stream << "lo " << "World" + stream << nil + end + assert_equal "\nHe\n\nl\n\nlo \nWorld\n\n", io.string + end + end + + describe "line writers within line writers" do + it 'uses existing line writer' do + io = StringIO.new + IOStreams::Stream.new(io).line_writer do |stream| + stream.write("Before") + IOStreams::Stream.new(stream).line_writer do |inner| + stream.write("Inner") + assert_equal inner.object_id, stream.object_id + end + stream.write("After") + end + assert_equal "Before\nInner\nAfter\n", io.string, io.string.inspect + end + end end - describe '.row_writer' do + describe '#row_writer' do + describe "#write" do + it 'one block' do + io = StringIO.new + IOStreams::Stream.new(io).row_writer do |stream| + stream << %w[Hello World] + end + assert_equal "Hello,World\n", io.string + end + + it 'multiple blocks' do + io = StringIO.new + IOStreams::Stream.new(io).row_writer do |stream| + stream << %w[He] + stream << %w[l lo\ World] + stream << ["He", "", "l", ""] + stream << ["lo ", "World"] + end + assert_equal "He\nl,lo ,World\nHe,\"\",l,\"\"\nlo ,World\n", io.string, io.string.inspect + end + + it 'empty blocks' do + # skip "TODO" + io = StringIO.new + IOStreams::Stream.new(io).row_writer do |stream| + stream << %w[He] + stream << [] + stream << %w[l lo\ World] + stream << ["He", "", "l", ""] + stream << ["lo ", "World"] + stream << [] + end + assert_equal "He\n\nl,lo ,World\nHe,\"\",l,\"\"\nlo ,World\n\n", io.string, io.string.inspect + end + + it 'nil values' do + io = StringIO.new + IOStreams::Stream.new(io).row_writer do |stream| + stream << %w[He] + stream << %w[l lo\ World] + stream << ["He", nil, "l", nil] + stream << ["lo ", "World"] + end + assert_equal "He\nl,lo ,World\nHe,,l,\nlo ,World\n", io.string, io.string.inspect + end + + it 'empty leading array' do + skip "TODO" + io = StringIO.new + IOStreams::Stream.new(io).row_writer do |stream| + stream << [] + stream << %w[He] + stream << %w[l lo\ World] + stream << ["He", "", "l", ""] + stream << ["lo ", "World"] + stream << [] + end + assert_equal "\nHe\n\nl\n\nlo \nWorld\n\n", io.string, io.string.inspect + end + end end - describe '.record_writer' do + describe '#record_writer' do + describe "#write" do + it 'one block' do + io = StringIO.new + IOStreams::Stream.new(io).record_writer do |stream| + stream << {first_name: "Jack", last_name: "Johnson"} + end + assert_equal "first_name,last_name\nJack,Johnson\n", io.string, io.string.inspect + end + + it 'multiple blocks' do + io = StringIO.new + IOStreams::Stream.new(io).record_writer do |stream| + stream << {first_name: "Jack", last_name: "Johnson"} + stream << {first_name: "Able", last_name: "Smith"} + end + assert_equal "first_name,last_name\nJack,Johnson\nAble,Smith\n", io.string, io.string.inspect + end + + it 'empty hashes' do + io = StringIO.new + IOStreams::Stream.new(io).record_writer do |stream| + stream << {first_name: "Jack", last_name: "Johnson"} + stream << {} << {first_name: "Able", last_name: "Smith"} + stream << {} + end + assert_equal "first_name,last_name\nJack,Johnson\n\n{:first_name=>\"Able\", :last_name=>\"Smith\"}\n\n", io.string, io.string.inspect + end + + it 'nil values' do + skip "TODO" + io = StringIO.new + IOStreams::Stream.new(io).record_writer do |stream| + stream << {first_name: "Jack", last_name: "Johnson"} + stream << {} << {first_name: "Able", last_name: "Smith"} + stream << {first_name: "Able", last_name: nil} + stream << {} + end + assert_equal "first_name,last_name\nJack,Johnson\n\n{:first_name=>\"Able\", :last_name=>\"Smith\"}\n\n", io.string, io.string.inspect + end + + end end end end