small medium large xlarge

Generic-user-small
14 Sep 2017, 19:50
Aaron K. (4 posts)

Bonus Points

…But for extra credit, feel free to make the specs pass. Post your solutions in the formus, and we’ll send you a GIF of a gold star.

Can has GIF (and/or review feedback), plz?

phone_number_extractor_spec.rb

#---
# Excerpted from "Effective Testing with RSpec 3",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rspec3 for more book information.
#---
class PhoneNumberExtractor
  def self.extract_from(text, &block)
    # Look for patterns like (###) ###-####
    text.scan(/\(?\d{3}\)?[\s|-]\d{3}-\d{4}/, &block)
  end
end

RSpec.describe PhoneNumberExtractor do
  let(:text) do
    <<-EOS
      Melinda: (202) 555-0168
      Bob: 202-555-0199
      Sabina: (202) 555-0176
    EOS
  end

  it 'yields phone numbers as it finds them' do
    expect { |block|
      PhoneNumberExtractor.extract_from(text, &block)
    }.to yield_successive_args(
      '(202) 555-0168',
      '202-555-0199',
      '(202) 555-0176'
    )
  end
end

public_company_spec.rb

#---
# Excerpted from "Effective Testing with RSpec 3",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rspec3 for more book information.
#---
PublicCompany = Struct.new(:name, :value_per_share, :share_count) do
  def got_better_than_expected_revenues
    self.value_per_share *= rand(1.05..1.10)
  end

  def market_cap
    @market_cap ||= value_per_share * share_count
  end
end

RSpec.describe PublicCompany do
  let(:company) { PublicCompany.new('Nile', 10, 100_000) }

  it 'increases its market cap when it gets better than expected revenues' do
    expect { company.got_better_than_expected_revenues }.
      to change { company.value_per_share }.
      to(a_value > company.value_per_share)
  end

  it 'provides attributes' do
    expect(company).to have_attributes(
      name: "Nile",
      value_per_share: 10,
      share_count: 100_000,
      market_cap: 1_000_000
    )
  end

end

tokenizer_spec.rb

#---
# Excerpted from "Effective Testing with RSpec 3",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rspec3 for more book information.
#---
class Tokenizer
  def self.tokenize(string)
    string.split(/ +/)
  end
end

RSpec.describe Tokenizer do
  let(:text) do
    <<-EOS
      I am Sam.
      Sam I am.
      Do you like green eggs and ham?
    EOS
  end

  it 'tokenizes multiple lines of text' do
    tokenized = Tokenizer.tokenize(text)
    expect(tokenized).to include('I', 'am', "Sam.\n", 'Sam', 'I', "am.\n")
  end
end

water_spec.rb

#---
# Excerpted from "Effective Testing with RSpec 3",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rspec3 for more book information.
#---
class Water
  def self.elements
    [:hydrogen, :oxygen, :hydrogen]
  end
end

RSpec.describe Water do
  it 'is H2O' do
    expect(Water.elements).to contain_exactly :hydrogen, :hydrogen, :oxygen
  end
end

calendar_spec.rb

#---
# Excerpted from "Effective Testing with RSpec 3",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rspec3 for more book information.
#---
require 'date'

Calendar = Struct.new(:date_string) do
  def on_weekend?
    date = Date.parse(date_string)
    date.saturday? || date.sunday?
  end
end

RSpec.describe Calendar do
  let(:sunday_date) { Calendar.new('Sun, 11 Jun 2017') }

  it 'considers sundays to be on the weekend' do
    expect(sunday_date).to be_on_weekend
  end
end
Mr_sketchy_pragsmall
15 Sep 2017, 16:37
Ian Dees (212 posts)

Hi, Aaron.

Thanks for posting these! Nice work making the matchers more readable as well. And I can confirm that your specs pass on my machine.

As promised, here’s a gold star:

gold star

Sincerely,

Ian

P.S. As an extra bonus question, can you add another example to your specs to check that the value of company.market_cap changes (and find the bug in the class definition that prevents this change from happening)?

Generic-user-small
26 Sep 2017, 17:13
Aaron K. (4 posts)

Ah, yes, Ian! I now see the error in my first solution to this exercise. Thank you for the feedback!

#---
# Excerpted from "Effective Testing with RSpec 3",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rspec3 for more book information.
#---
PublicCompany = Struct.new(:name, :value_per_share, :share_count) do
  def got_better_than_expected_revenues
    self.value_per_share *= rand(1.05..1.10)
  end

  def market_cap
    @market_cap = value_per_share * share_count
  end
end

RSpec.describe PublicCompany do
  let(:company) { PublicCompany.new('Nile', 10, 100_000) }

  it 'increases its market cap when it gets better than expected revenues' do
    expect { company.got_better_than_expected_revenues }.
      to change { company.market_cap }.
      to(a_value > company.market_cap)
  end

  it 'provides attributes' do
    expect(company).to have_attributes(
      name: "Nile",
      value_per_share: 10,
      share_count: 100_000,
      market_cap: 1_000_000
    )
  end

end
You must be logged in to comment