Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class IntegerFormatter
def with_commas(integer)
quotient = integer / 1000
remainder = integer % 1000
if quotient == 0
remainder.to_s
else
prepend = with_commas(quotient)
"#{prepend},#{"%03d" % remainder}"
end
end
end
require "./integer_formatter"
describe IntegerFormatter do
subject { IntegerFormatter.new }
describe "#with_commas" do
it "should handle number until 999 without any commas" do
test_with_commas(1, "1")
test_with_commas(10, "10")
test_with_commas(100, "100")
end
it "should handle number between 1000 and 999999" do
test_with_commas(1000, "1,000")
test_with_commas(10000, "10,000")
test_with_commas(100000, "100,000")
end
it "should handle number above 10000000" do
test_with_commas(1000000, "1,000,000")
test_with_commas(35235235, "35,235,235")
end
end
def test_with_commas(integer, expected_string)
subject.with_commas(integer).should eq expected_string
end
end

simcap commented Oct 2, 2014

👍

Being picky but any reason in the spec file for line subject { IntegerFormatter.new }? It would work without this line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment