Skip to content

Commit

Permalink
Resolves #1828, implement File#readlines (Node.js) (#1882)
Browse files Browse the repository at this point in the history
* Resolves #1828, implement File#readlines (Node.js)

* Pass the separator argument
  • Loading branch information
ggrossetie authored and iliabylich committed Sep 5, 2018
1 parent 296930e commit d77bd3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion stdlib/nodejs/file.rb
Expand Up @@ -186,6 +186,10 @@ def read
end
end

def readlines(separator = $/)
each_line(separator).to_a
end

def each_line(separator = $/, &block)
if @eof
return block_given? ? self : [].to_enum
Expand All @@ -212,7 +216,7 @@ def each_line(separator = $/, &block)
}
self
else
read.each_line
read.each_line separator
end
end

Expand Down
24 changes: 22 additions & 2 deletions test/nodejs/test_file.rb
Expand Up @@ -43,7 +43,7 @@ def test_write_read

def test_read_file
File.write('tmp/foo', 'bar')
assert_equal("bar", File.read('tmp/foo'))
assert_equal('bar', File.read('tmp/foo'))
end

def test_read_each_line
Expand All @@ -53,7 +53,27 @@ def test_read_each_line
file.each_line do |line|
lines << line
end
assert_equal(lines.length, 2)
assert_equal(2, lines.length)
assert_equal("one\n", lines[0])
assert_equal('two', lines[1])
end

def test_readlines
File.write('tmp/quz', "one\ntwo")
file = File.new('tmp/quz', 'r')
lines = file.readlines
assert_equal(2, lines.length)
assert_equal("one\n", lines[0])
assert_equal('two', lines[1])
end

def test_readlines_separator
File.write('tmp/qux', "one-two")
file = File.new('tmp/qux', 'r')
lines = file.readlines '-'
assert_equal(2, lines.length)
assert_equal('one-', lines[0])
assert_equal('two', lines[1])
end

def test_read_noexistent_should_raise_io_error
Expand Down

0 comments on commit d77bd3c

Please sign in to comment.