Last active
January 2, 2016 17:29
-
-
Save ssaunier/8336988 to your computer and use it in GitHub Desktop.
My solution to the problem described in http://sebastien.saunier.me/blog/2014/01/09/quickly-setup-a-ruby-tdd-throw-away-code.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍
Being picky but any reason in the spec file for line
subject { IntegerFormatter.new }
? It would work without this line.