Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mcalaguap/StringCalculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class StringCalculator
def Add(string)
if (string == "")
0
else
negative = []
delim = "\n"
sum = 0
if string.start_with? "//"
delim = string[2].chr
string = string.split("\n")[1]
elsif string.include? ","
delim = ","
end
string.split(delim).each { |n|
if n.to_i < 0
negative << n
end
sum+=n.to_i
}
if negative.empty?
sum
else
raise "negative numbers are not allowed #{negative.join(",")}"
end
end
end
end
68 changes: 68 additions & 0 deletions mcalaguap/StringCalculator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

require './StringCalculator'

describe "StringCalculator" do

context "Cadena Vacia" do
it "Retorna 0 si parametro es vacio" do
sc = StringCalculator.new
sc.Add("").should == 0
end
end

context "Para un Numero" do
it "Retorna el numero cero" do
sc = StringCalculator.new
sc.Add("0").should == 0
end
it "Retorna el numero uno" do
sc = StringCalculator.new
sc.Add("1").should == 1
end
it "Retorna el numero 123456" do
sc = StringCalculator.new
sc.Add("123456").should == 123456
end
end

context "Dos o mas numeros" do
it "Retorna 1+2=3" do
sc = StringCalculator.new
sc.Add("1,2").should == 3
end
it "Trabaja con 10 numeros" do
sc = StringCalculator.new
sc.Add("1,2,3,4,5,6,7,8,9,10")
end
end

context "Salto de linea en lugar de , para separar los numeros" do
it "Retorna 20+20=40" do
sc = StringCalculator.new
sc.Add("20\n20").should == 40
end
end

context "Cualquier delimitador" do
it "Trabaja con ; y retrona 40" do
sc = StringCalculator.new
sc.Add("//;\n20;20").should == 40
end
it "Trabaja con culquiera como el caracter H" do
sc = StringCalculator.new
sc.Add("//H\n20H20").should == 40
end
end

context "Numero negativos no permitidos" do
it "Saltar excepcion con un numero negativo" do
sc = StringCalculator.new
expect{ sc.Add("//;\n20;-20") }.to raise_error "negative numbers are not allowed -20"
end
it "Saltar excepcion con N numeros negativo" do
sc = StringCalculator.new
expect{ sc.Add("//;\n1;-2;3;-4;5;-6;7;-8;9;-10") }.to raise_error "negative numbers are not allowed -2,-4,-6,-8,-10"
end
end

end