Skip to content

Commit

Permalink
Fix IO#each_line not chomping non-default separators
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 25, 2024
1 parent 4d8c1aa commit 43d6ff7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/polyphony/extensions/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def binwrite(name, string, offset = nil)
alias_method :orig_foreach, :foreach

# @!visibility private
def foreach(name, sep = $/, limit = nil, getline_args = EMPTY_HASH, &block)
def foreach(name, sep = $/, limit = nil, **getline_args, &block)
if sep.is_a?(Integer)
sep = $/
limit = sep
end
File.open(name, 'r') do |f|
f.each_line(sep, limit, chomp: getline_args[:chomp], &block)
f.each_line(sep, limit, **getline_args, &block)
end
end

Expand Down Expand Up @@ -320,7 +320,7 @@ def each_line(sep = $/, limit = nil, chomp: false)
while true
while (idx = @read_buffer.index(sep))
line = @read_buffer.slice!(0, idx + sep_size)
line = line.chomp if chomp
line = line.chomp(sep) if chomp
yield line
end

Expand Down
76 changes: 69 additions & 7 deletions test/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,46 @@ def test_getbyte

def test_each_line_with_block
file = Tempfile.new
file << "foo\nbar\nbaz\n"
file << "foo:\nbar:\nbaz:\n"
file.close

strings = ["foo\n", "bar\n", "baz\n"]
strings = ["foo:\n", "bar:\n", "baz:\n"]

# No arguments
lines = []
File.open(file, 'r').each_line { |line| lines << line }
assert_equal strings, lines

i = 0
File.open(file, 'r').each_line do |line|
assert_equal line, strings[i]
lines = []
File.open(file, 'r').each_line(chomp: true) { |line| lines << line }
assert_equal strings.map(&:chomp), lines

i += 1
end
# Separator as argument
lines = []
File.open(file, 'r').each_line(":") { |line| lines << line }
assert_equal "foo:", lines[0]

lines = []
File.open(file, 'r').each_line(":", chomp: true) { |line| lines << line }
assert_equal "foo", lines[0]

# Limit as argument
lines = []
File.open(file, 'r').each_line(4) { |line| lines << line }
assert_equal "foo:\n", lines[0]

lines = []
File.open(file, 'r').each_line(4, chomp: true) { |line| lines << line }
assert_equal "foo:", lines[0]

# Separator and limit as arguments
lines = []
File.open(file, 'r').each_line(":", 3) { |line| lines << line }
assert_equal "foo:", lines[0]

lines = []
File.open(file, 'r').each_line(":", 3, chomp: true) { |line| lines << line }
assert_equal "foo", lines[0]
end

def test_each_line_iterator
Expand Down Expand Up @@ -625,10 +654,43 @@ def test_binwrite
end

def test_foreach
# No arguments
lines = []
IO.foreach(__FILE__) { |l| lines << l }
assert_equal "# frozen_string_literal: true\n", lines[0]
assert_equal "end\n", lines[-1]

lines = []
IO.foreach(__FILE__, chomp: true) { |l| lines << l }
assert_equal "# frozen_string_literal: true", lines[0]
assert_equal "end", lines[-1]

# Separator as argument
lines = []
IO.foreach(__FILE__, ":") { |l| lines << l }
assert_equal "# frozen_string_literal:", lines[0]

lines = []
IO.foreach(__FILE__, ":", chomp: true) { |l| lines << l }
assert_equal "# frozen_string_literal", lines[0]

# Limit as argument
lines = []
IO.foreach(__FILE__, 30) { |l| lines << l }
assert_equal "# frozen_string_literal: true\n", lines[0]

lines = []
IO.foreach(__FILE__, 30, chomp: true) { |l| lines << l }
assert_equal "# frozen_string_literal: true", lines[0]

# Separator and limit as argument
lines = []
IO.foreach(__FILE__, ":", 23) { |l| lines << l }
assert_equal "# frozen_string_literal:", lines[0]

lines = []
IO.foreach(__FILE__, ":", 23, chomp: true) { |l| lines << l }
assert_equal "# frozen_string_literal", lines[0]
end

def test_read_class_method
Expand Down

0 comments on commit 43d6ff7

Please sign in to comment.