I've completed the string calculator kata in Ruby (spec from here - http://osherove.com/tdd-kata-1/). All tests currently pass.
Could it be refactored further to improve readability? And could I make my testing better?
string_calculator.rb
class StringCalculator
def initialize
end
def int_add(string_of_numbers)
raise 'only accepts a string' unless string_of_numbers.is_a?(String)
string_array = string_of_numbers.split(/[^0-9-]+/)
integer_array = string_array.map(&:to_i)
raise "cannot accept negatives - #{check_for_negatives(integer_array)}" if
check_for_negatives(integer_array)
integer_array.inject(0){|sum,x| x <= 1000? sum + x : sum }
end
def check_for_negatives(integer_array)
negatives_array = integer_array.select{ |i| i<0 }
if negatives_array.length > 0
return negatives_string = negatives_array.join(",")
else
return false
end
end
end
string_calculator_spec.rb
require 'string_calculator'
describe StringCalculator do
subject(:calculator) { described_class.new }
it 'should accept a string' do
expect{ calculator.int_add('1,2,3') }.not_to raise_error
end
it 'should not accept other data types' do
expect{ calculator.int_add(123) }.to raise_error('only accepts a string')
expect{ calculator.int_add(['123']) }.to raise_error('only accepts a
string')
end
it 'should return 0 for an empty string' do
expect(calculator.int_add('')).to eq(0)
end
it 'should return a number if the passed string contains no delimiters' do
expect(calculator.int_add('123')).to eq (123)
end
it 'should return the sum of the numbers in the passed string, if the passed
string contains comma delimiters' do
expect(calculator.int_add('12,34')).to eq(46)
end
it 'should return the sum of the numbers in the passed string, if the passed
string contains new line delimiters' do
expect(calculator.int_add("12\n34\n56")).to eq(102)
end
it 'should handle multiple random delimiters' do
expect(calculator.int_add("//;\n1;2")).to eq(3)
end
it 'should not accept negative numbers' do
expect{ calculator.int_add("123,-2") }.to raise_error("cannot accept
negatives - -2")
end
it 'should ignore numbers larger than 1000' do
expect(calculator.int_add("//;\n1;2:1001")).to eq(3)
end
end