Last active
August 29, 2015 14:02
-
-
Save lgrains/81b30c98722380121163 to your computer and use it in GitHub Desktop.
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
rspec string_calculator.rb | |
.....F | |
Failures: | |
1) add negative number(s) | |
Failure/Error: raise "negatives not allowed - " + v.to_s if v | |
RuntimeError: | |
negatives not allowed - -3 | |
# ./string_calculator.rb:6:in `add' | |
# ./string_calculator.rb:53:in `block (3 levels) in <top (required)>' | |
Finished in 0.00203 seconds | |
6 examples, 1 failure | |
Failed examples: | |
rspec ./string_calculator.rb:53 # add negative number(s) |
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 'rspec/autorun' | |
class StringCalculator | |
def self.add(numbers="") | |
v = contains_negatives(numbers) | |
raise "negatives not allowed - " + v.to_s if v | |
delimiters = /,|\n/ | |
return add_inject(numbers, delimiters) unless numbers.start_with?('//') | |
delim,nums = partition_input(numbers) | |
delimiters = delim[-1] | |
add_inject(nums,delimiters) | |
# implement using split, map, reduce | |
end | |
def self.partition_input(str) | |
str.split("\n") | |
end | |
def self.add_inject(numbers, delimiters) | |
numbers.split(delimiters).map{|n| n.to_i}.reduce(0,:+) | |
end | |
def self.contains_negatives(numbers) | |
/-\d/.match(numbers) | |
end | |
end | |
describe "add" do | |
subject {StringCalculator} | |
context "empty string" do | |
it {subject.add.should == 0} | |
end | |
context "one number" do | |
it {subject.add("3").should == 3 } | |
end | |
context "unknown amount of numbers" do | |
it {subject.add("4,6,8,10").should == 28} | |
end | |
context "a newline in the numbers" do | |
it {subject.add("1\n2,3").should == 6 } | |
end | |
context "given a delimiter at the beginning of string" do | |
it {subject.add("//;\n1;2").should == 3} | |
end | |
context "negative number(s)" do | |
it { subject.add("-3,3").to raise_error } | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One other suggestion I would make is to have your subject capture the method you're testing and pass the param as it's defined with a let under each context. You can dry things up considerably this way: