Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Allow pluralize to handle words that end with s. #2779

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/rspec/core/formatters/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ def self.strip_trailing_zeroes(string)
# @param string [String] word to be pluralized
# @return [String] pluralized word
def self.pluralize(count, string)
"#{count} #{string}#{'s' unless count.to_f == 1}"
pluralized_string = if count.to_f == 1
string
elsif string.end_with?('s') # e.g. "process"
"#{string}es" # e.g. "processes"
else
"#{string}s"
end

"#{count} #{pluralized_string}"
end

# @api private
Expand Down
33 changes: 33 additions & 0 deletions spec/rspec/core/formatters/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,38 @@
end
end

describe "pluralize" do
context "when word does not end in s" do
let(:word){ "second" }

it "pluralizes with 0" do
expect(helper.pluralize(0, "second")).to eq("0 seconds")
end

it "does not pluralizes with 1" do
expect(helper.pluralize(1, "second")).to eq("1 second")
end

it "pluralizes with 2" do
expect(helper.pluralize(2, "second")).to eq("2 seconds")
end
end

context "when word ends in s" do
let(:word){ "process" }

it "pluralizes with 0" do
expect(helper.pluralize(0, "process")).to eq("0 processes")
end

it "does not pluralizes with 1" do
expect(helper.pluralize(1, "process")).to eq("1 process")
end

it "pluralizes with 2" do
expect(helper.pluralize(2, "process")).to eq("2 processes")
end
end
end

end