Last active
January 3, 2016 14:39
-
-
Save mikesimons/8477282 to your computer and use it in GitHub Desktop.
String calculator kata
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 StringCalculator | |
def calculate string = "" | |
string.split(/\D+/).map do |number| | |
number.to_i | |
end.reduce(&:+).to_i | |
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_relative 'calc' | |
describe StringCalculator do | |
before do | |
@calc = StringCalculator.new | |
end | |
it "should return zero with no arguments" do | |
@calc.calculate.should be 0 | |
end | |
it "should return zero with empty string" do | |
@calc.calculate("").should be 0 | |
end | |
it "should return zero with 0 as argument" do | |
@calc.calculate("0").should be 0 | |
end | |
it "should return a bare number" do | |
@calc.calculate("1").should be 1 | |
end | |
it "should return the sum of space separated numbers" do | |
@calc.calculate("2 3").should be 5 | |
end | |
it "should return the sum of any whitespace separated numbers" do | |
@calc.calculate("2 4").should be 6 | |
@calc.calculate("2\t4").should be 6 | |
end | |
it "should accept a custom separator" do | |
@calc.calculate("//+1+2+3+4").should be 10 | |
@calc.calculate("//a1a2a3a4").should be 10 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment