diff --git a/lib/remote_input.rb b/lib/remote_input.rb index 3e480f6..4c21f97 100644 --- a/lib/remote_input.rb +++ b/lib/remote_input.rb @@ -47,6 +47,12 @@ def gets(...) local_file.gets(...) end + def each_line(...) + return to_enum(__method__, ...) unless block_given? + local_file.each_line(...) + self + end + def each_chunk(&block) return to_enum(__method__) unless block_given? download(&block) diff --git a/test/test-remote-input.rb b/test/test-remote-input.rb index d6a8378..6a21e2b 100644 --- a/test/test-remote-input.rb +++ b/test/test-remote-input.rb @@ -111,6 +111,42 @@ def test_gets_after_close end end + def test_each_line + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_equal(["1\n", "2\n"], + input.each_line.to_a) + end + end + + def test_each_line_with_nil_separator + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_equal(["1\n2\n"], + input.each_line(nil).to_a) + end + end + + def test_each_line_with_chomp + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_equal(["1", "2"], + input.each_line(chomp: true).to_a) + end + end + + def test_each_line_return_self + open_input do |input| + input.send(:cache_path).parent.mkpath + input.send(:cache_path).write("1\n2\n") + assert_same(input, + input.each_line {}) + end + end + def test_each_chunk open_input do |input| expected_chunks = ["1" * (1024 * 1024), "2"]