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

Commit 09e0728

Browse files
joshuapinterpirj
authored andcommitted
Allow pluralize to handle words that end with s.
Use Case: We have a custom formatter that we use with `parallel_tests` to get a cleaner output from the various test processes. We make use of `RSpec::Core::Formatters::Helpers.pluralize` in there to display the number of remaining processes left. However, instead of getting "processes", we get "processs". Looking into the `pluralize` method definition, it simple adds an "s" to the end of the provided String, unless the count is equal to 1. Without accounting for all the different words that are possible, which something like Rails would do, we just extended this to add "es" if the provided String ends in "s" already. We also added tests for words that end in "s" and words that do not end in "s".
1 parent bfd2b31 commit 09e0728

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/rspec/core/formatters/helpers.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ def self.strip_trailing_zeroes(string)
8686
# @param string [String] word to be pluralized
8787
# @return [String] pluralized word
8888
def self.pluralize(count, string)
89-
"#{count} #{string}#{'s' unless count.to_f == 1}"
89+
pluralized_string = if count.to_f == 1
90+
string
91+
elsif string.end_with?('s') # e.g. "process"
92+
"#{string}es" # e.g. "processes"
93+
else
94+
"#{string}s"
95+
end
96+
97+
"#{count} #{pluralized_string}"
9098
end
9199

92100
# @api private

spec/rspec/core/formatters/helpers_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,38 @@
117117
end
118118
end
119119

120+
describe "pluralize" do
121+
context "when word does not end in s" do
122+
let(:word){ "second" }
123+
124+
it "pluralizes with 0" do
125+
expect(helper.pluralize(0, "second")).to eq("0 seconds")
126+
end
127+
128+
it "does not pluralizes with 1" do
129+
expect(helper.pluralize(1, "second")).to eq("1 second")
130+
end
131+
132+
it "pluralizes with 2" do
133+
expect(helper.pluralize(2, "second")).to eq("2 seconds")
134+
end
135+
end
136+
137+
context "when word ends in s" do
138+
let(:word){ "process" }
139+
140+
it "pluralizes with 0" do
141+
expect(helper.pluralize(0, "process")).to eq("0 processes")
142+
end
143+
144+
it "does not pluralizes with 1" do
145+
expect(helper.pluralize(1, "process")).to eq("1 process")
146+
end
147+
148+
it "pluralizes with 2" do
149+
expect(helper.pluralize(2, "process")).to eq("2 processes")
150+
end
151+
end
152+
end
120153

121154
end

0 commit comments

Comments
 (0)