Skip to content

Instantly share code, notes, and snippets.

@spangenberg
Last active December 15, 2015 19:59
Show Gist options
  • Select an option

  • Save spangenberg/5315418 to your computer and use it in GitHub Desktop.

Select an option

Save spangenberg/5315418 to your computer and use it in GitHub Desktop.
Funny ruby kata with @stevenklar
rspec string_calculator_spec.rb
class StringCalculator
def self.add(command)
string_calculator = new(command)
string_calculator.calculate
end
def initialize(command)
@command = command
end
def calculate
numbers.inject(0) do |sum, number|
raise 'negatives not allowed' if number.to_i < 0
sum += number.to_i if number.to_i < 1000
sum
end
end
private
def delimiter
pattern = /,|\n/
if @command =~ /^\/\/(.+)/
pattern = $1
if $1[0] == '[' && $1[$1.size - 1] == ']'
pattern = $1[1..($1.size - 2)]
end
end
pattern
end
def numbers
(@command[/^\/\/(.+)\n(.*)$/, 2] || @command).split(delimiter)
end
end
require_relative 'string_calculator'
describe StringCalculator do
subject { StringCalculator }
describe '#add' do
it 'should return 0 when a empty string is given' do
subject.add('').should eq(0)
end
it 'should return 1 when string with 1 is given' do
subject.add('1').should eq(1)
end
it 'should return 3 when string with 1,2 is given' do
subject.add('1,2').should eq(3)
end
it 'should add up unlimited number of numbers (dafuq what a bad wording)' do
# Allow the Add method to handle an unknown amount of numbers
#
# Ich denke wir verstehen das hier anders... ich dachte wir testen eher
# sowas: 1,2,3,4,5,6,7,8,9 = 45
#
subject.add('1,2,3,4,5,6,7,8,9').should eq(45)
#
#klar geht auch, bin auch kein freund von "random" tests,
#aber hier hat man halt auch nicht unbedingt die gewissheit von unlimited
#
#ja korrekt, lass uns weiter machen :P
end
it 'Allow the Add method to handle new lines between numbers (instead of commas).' do
subject.add("1\n2,3").should eq(6)
end
#Support different delimiters
#to change a delimiter, the beginning of the string will
#contain a separate line that looks like this:
#“//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three
#where the default delimiter is ‘;’ .
it 'should support custom delimiter' do
subject.add("//;\n1;2").should eq(3)
subject.add("//bla:D\n1bla:D2").should eq(3)
end
it 'should throw an exception when a negative number is passed' do
expect { subject.add('-1') }.to raise_error('negatives not allowed')
end
# Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2
it 'should skip numbers bigger than 1000' do
subject.add('2,1001').should eq(2)
end
# Delimiters can be of any length with the following format:
# “//[delimiter]\n” for example: “//[***]\n1***2***3” should return 6
it 'should support custom delimiter v2' do
subject.add("//[***]\n1***2***3").should eq(6)
end
# Multiple Delimiters
# [***][&&&]
it 'should support multiple delimiters' do
subject.add("//[***][&&&]\n1***2&&&3").should eq(6)
end
end
end
@stevenklar
Copy link

If we remove the comments, even the tests are awesome small

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment