Created
January 16, 2019 20:44
-
-
Save dflima/6a13b7e12f16b13179de875596c82b34 to your computer and use it in GitHub Desktop.
[XP Training] Pair programming calculator problem
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 Calculator | |
def calculate(input) | |
splited = split_string(input) | |
mapped = splited.map(&:to_i) | |
if (input.include?('+')) | |
return mapped.reduce(:+) | |
elsif (input.include?('-')) | |
return mapped.reduce(:-) | |
end | |
input.to_i | |
end | |
def split_string(input) | |
input_arr = input.split(/[+-]/) | |
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("Calculator") | |
context 'calculator' do | |
before :all do | |
@calculator = Calculator.new | |
end | |
it 'should return the input number' do | |
expect(@calculator.calculate('5')).to eq 5 | |
end | |
it 'should return the input number' do | |
expect(@calculator.calculate('0')).to eq 0 | |
end | |
it 'should add two numbers separated by the plus char' do | |
expect(@calculator.calculate('5+5')).to eq 10 | |
end | |
it 'should sum two or more numbers' do | |
expect(@calculator.calculate('5+5+5+1')).to eq 16 | |
end | |
it 'should sum two or more numbers separated by the plus char' do | |
expect(@calculator.calculate('99+4+1+109')).to eq 213 | |
end | |
it 'should subtract two numbers separated by the minus char' do | |
expect(@calculator.calculate('5-1')).to eq 4 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment