├── .gitignore ├── .rspec ├── Gemfile ├── README.md ├── lib └── string_calculator.rb ├── .semaphore └── semaphore.yml ├── Gemfile.lock └── spec └── string_calculator_spec.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | .bundle 3 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "rspec" 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rspec-intro 2 | 3 | Source code used in Semaphore's [RSpec 4 | tutorial](https://semaphoreci.com/community/tutorials/getting-started-with-rspec). 5 | 6 | Here's how you can run it: 7 | 8 | ``` 9 | bundle install --path .bundle 10 | bundle exec rspec 11 | ``` 12 | -------------------------------------------------------------------------------- /lib/string_calculator.rb: -------------------------------------------------------------------------------- 1 | class StringCalculator 2 | 3 | def self.add(input) 4 | if input.size == 0 5 | 0 6 | else 7 | numbers = input.split(",").map { |num| num.to_i } 8 | numbers.inject(0) { |sum, number| sum + number.to_i } 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /.semaphore/semaphore.yml: -------------------------------------------------------------------------------- 1 | version: v1.0 2 | name: Ruby 3 | agent: 4 | machine: 5 | type: e1-standard-2 6 | os_image: ubuntu1804 7 | blocks: 8 | - name: RSpec 9 | task: 10 | jobs: 11 | - name: Run specs 12 | commands: 13 | - checkout 14 | - sem-version ruby 2.6.0 15 | - bundle install --path vendor/bundle 16 | - bundle exec rspec 17 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | diff-lcs (1.3) 5 | rspec (3.9.0) 6 | rspec-core (~> 3.9.0) 7 | rspec-expectations (~> 3.9.0) 8 | rspec-mocks (~> 3.9.0) 9 | rspec-core (3.9.1) 10 | rspec-support (~> 3.9.1) 11 | rspec-expectations (3.9.0) 12 | diff-lcs (>= 1.2.0, < 2.0) 13 | rspec-support (~> 3.9.0) 14 | rspec-mocks (3.9.1) 15 | diff-lcs (>= 1.2.0, < 2.0) 16 | rspec-support (~> 3.9.0) 17 | rspec-support (3.9.2) 18 | 19 | PLATFORMS 20 | ruby 21 | 22 | DEPENDENCIES 23 | rspec 24 | 25 | BUNDLED WITH 26 | 2.1.4 27 | -------------------------------------------------------------------------------- /spec/string_calculator_spec.rb: -------------------------------------------------------------------------------- 1 | require "string_calculator" 2 | 3 | describe StringCalculator do 4 | 5 | describe ".add" do 6 | context "given an empty string" do 7 | it "returns zero" do 8 | expect(StringCalculator.add("")).to eql(0) 9 | end 10 | end 11 | 12 | context "single numbers" do 13 | context "given '4'" do 14 | it "returns 4" do 15 | expect(StringCalculator.add("4")).to eql(4) 16 | end 17 | end 18 | 19 | context "given '10'" do 20 | it "returns 10" do 21 | expect(StringCalculator.add("10")).to eql(10) 22 | end 23 | end 24 | end 25 | 26 | context "two numbers" do 27 | context "given '2,4'" do 28 | it "returns 6" do 29 | expect(StringCalculator.add("2,4")).to eql(6) 30 | end 31 | end 32 | 33 | context "given '17,100'" do 34 | it "returns 117" do 35 | expect(StringCalculator.add("17,100")).to eql(117) 36 | end 37 | end 38 | end 39 | end 40 | end 41 | --------------------------------------------------------------------------------