Fix word_wrap with empty string

Follow-up to #45948.

This fixes `word_wrap` to return an empty string instead of `nil` when
given an empty string.

Fixes #50067.
This commit is contained in:
Jonathan Hefner 2023-11-15 16:14:35 -06:00
parent d424cab951
commit 2191f20706
2 changed files with 7 additions and 0 deletions

@ -266,6 +266,8 @@ def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18
# word_wrap('Once upon a time', line_width: 1, break_sequence: "\r\n")
# # => Once\r\nupon\r\na\r\ntime
def word_wrap(text, line_width: 80, break_sequence: "\n")
return +"" if text.empty?
# Match up to `line_width` characters, followed by one of
# (1) non-newline whitespace plus an optional newline
# (2) the end of the string, ignoring any trailing newlines

@ -393,6 +393,11 @@ def test_excerpt_with_separator
assert_equal " 1-+1-+ 1-+1", word_wrap(input, line_width: 3, break_sequence: "-+")
end
test "word_wrap when no wrapping is necessary" do
assert_equal "1", word_wrap("1", line_width: 3)
assert_equal "", word_wrap("", line_width: 3)
end
def test_pluralization
assert_equal("1 count", pluralize(1, "count"))
assert_equal("2 counts", pluralize(2, "count"))