Created
January 4, 2011 19:26
-
-
Save alberto/765257 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
require "test_notifier/runner/rspec" | |
TestNotifier.default_notifier = :notify_send | |
RSpec::Matchers.define :add_to do |expected| | |
match do |string| | |
string.add == expected | |
end | |
end | |
module AddableString | |
def add | |
numbers.inject(0) { |sum, number| sum + number } | |
end | |
private | |
def numbers | |
split(any_delimiter).map(&:to_i) | |
end | |
def any_delimiter | |
Regexp.union delimiters | |
end | |
def delimiters | |
return default_delimiters + singlechar_delimiter + bracketed_delimiters | |
end | |
def default_delimiters | |
[",", "\n"] | |
end | |
def singlechar_delimiter | |
has_custom_delimiters?? [self[2]] : [] | |
end | |
def bracketed_delimiters | |
scan(/\[(.+?)\]/).map{ |m| m[0] } | |
end | |
def has_custom_delimiters? | |
start_with?("//") | |
end | |
end | |
class String | |
include AddableString | |
end | |
describe "String Calculator" do | |
it "should return 0 for ''" do | |
"".should add_to 0 | |
end | |
context "can add one number" do | |
it "should return 1 for 1" do | |
"1".should add_to 1 | |
end | |
it "should return 2 for 2" do | |
"2".should add_to 2 | |
end | |
it "should return 42 for 42" do | |
"42".should add_to 42 | |
end | |
end | |
context "can add two numbers" do | |
it "should return 2 for 1,1" do | |
"1,1".should add_to 2 | |
end | |
it "should return 3 for 1,2" do | |
"1,2".should add_to 3 | |
end | |
it "should return 12 for 1,11" do | |
"1,11".should add_to 12 | |
end | |
end | |
context "can add any amount of numbers" do | |
it "should return 6 for 1,2,3" do | |
"1,2,3".should add_to 6 | |
end | |
it "should return 200 for one-hundred twos" do | |
Array.new(2,100).join(',').should add_to 200 | |
end | |
end | |
context "delimiters" do | |
it "supports \n as delimiter" do | |
"1\n2".should add_to 3 | |
end | |
it "supports ; as delimiter" do | |
"//;\n1;2".should add_to 3 | |
end | |
it "supports % as delimiter" do | |
"//%\n1%2".should add_to 3 | |
end | |
context "delimiters of any length" do | |
it "supports ;: as delimiter" do | |
"//[;:]\n1;:2".should add_to 3 | |
end | |
it "supports ::: as delimiter" do | |
"//[:::]\n1:::2".should add_to 3 | |
end | |
end | |
context "multiple delimiters" do | |
it "supports %, & as delimiters" do | |
"//[%][&]\n1%2&3".should add_to 6 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@xaviuzz el add que proponías devuelve nil en vez de 0 cuando summands == []