Skip to content

Instantly share code, notes, and snippets.

@joebew42
Created April 30, 2013 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebew42/5489629 to your computer and use it in GitHub Desktop.
Save joebew42/5489629 to your computer and use it in GitHub Desktop.
require File.join(File.dirname(__FILE__), 'string_calculator.rb')
describe StringCalculator do
context "sum with an empty string" do
it "returns zero" do
subject.add("").should == 0
end
end
context "sum with a string that contains one number" do
io_expectations = {
"1" => 1,
"2" => 2,
"3" => 3,
"4" => 4,
"5" => 5,
}
io_expectations.each do |input, output|
it %{returns #{output} itself if input is #{input}} do
subject.add(input).should == output
end
end
end
context "sum with a string that contains two numbers" do
it "returns the sum of the two numbers" do
subject.add("1,2").should == 3
end
end
context "sum with a string that contains an arbitrary number of numbers" do
it "returns the sum of the numbers" do
subject.add("1,2,3").should == 6
end
end
context "sum with a string that contains an arbitrary number of numbers separated with \\n or ," do
it "returns the sum of the numbers" do
subject.add("1,2,3\n4").should == 10
end
end
context "support of a user defined delimiter for numbers" do
it "returns the sum of the numbers" do
subject.add("//;\n1;2;3\n4").should == 10
end
end
context "called with negative numbers" do
it "raise an exception \"negative not allowed [-2]\"" do
expect { subject.add("//;\n1;2;3\n4;-2;5") }.to raise_error("negative not allowed [-2]")
end
it "raise an exception \"negative not allowed [-2, -10, -4, -7]\"" do
expect { subject.add("//;\n1;2;3\n4;-2;5;-10;-4;3;-7") }.to raise_error("negative not allowed [-2, -10, -4, -7]")
end
end
context "ignoring number greater than 1000" do
it "returns 15 if sum [1,2,3,4,1000,1001,5]" do
subject.add("//;\n1;2;3\n4;1000;1001;5").should == 15
end
end
context "length of the delimiter has an arbitrary length" do
it "returns the sum of the numbers" do
subject.add("//[del]\n1del2del3\n4del1000del1001del5").should == 15
end
end
context "number of defined delimiters is arbitrary" do
it "returns the sum of the numbers" do
subject.add("//[kill][them][all]\n1kill2them3\n4all1000kill1001them5").should == 15
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment