Skip to content

Instantly share code, notes, and snippets.

@thatrubylove thatrubylove/cards.md
Last active Aug 29, 2015

Embed
What would you like to do?
TDD Cards in 55 Using MiniTest

Step by Step - Intro to TDD using a deck of playing cards

  1. Create and edit a file called cards.rb

  2. πŸ‘‡

require 'minitest/autorun'
require 'minitest/pride'
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
end
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
end
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  it "has a to_a method" do
    Deck.new.respond_to?(:to_a).must_equal true
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  def to_a
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  describe "#to_a" do
    it "has 52 items" do
      Deck.new.to_a.count.must_equal 52
    end
    ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  def to_a
    52.times.map { 0 }
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  describe "#to_a" do
    it "contains Cards" do
      Deck.new.to_a[0].class.must_equal Card
    end
    ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Card
end
  1. πŸ‘‡
class Deck
  def to_a
    52.times.map { Card.new }
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Card do
  it "has a rank" do
    Card.new.respond_to?(:rank).must_equal true
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Card < Struct.new(:rank)
end
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Card do
  it "has a suit" do
    Card.new.respond_to?(:suit).must_equal true
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Card < Struct.new(:rank, :suit)
end
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  it "has 13 ranks" do
    Deck::RANKS.count.must_equal 13
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  RANKS = %i[ace two three four five sex seven eight nine ten jack queen king]
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  it "has 4 suits" do
    Deck::SUITS.count.must_equal 4
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  SUITS = %i[clubs diamonds spades hearts]
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  it "must have at least 4 queens" do
    Deck.new.to_a.select {|card| card.rank == :queen }.count.must_be :>= 4
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  ...
  def to_a
    RANKS.map {|rank| Card.new(rank) }
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  ...
  def to_a
    RANKS.map {|rank| SUITS.map {|suit| Card.new(rank) } }
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  def to_a
    RANKS.flat_map {|rank| SUITS.map {|suit| Card.new(rank) } }
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Card do
  it "#to_s is the rank and suit in enligsh" do
    Card.new(:ace, :hearts).to_s.must_equal "ace of hearts"
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Card
  def to_s
    "#{rank} of #{suit}"
  end
  ...
  1. ruby -I"." cards.rb

  2. πŸ‘‡

describe Deck do
  it "initializes with a shoe size" do
    Deck.new(2).shoe_size.must_equal 2
  end
  1. ruby -I"." cards.rb

  2. πŸ‘‡

class Deck
  ...
  def initialize(shoe_size=1)
    @shoe_size = shoe_size
  end

  attr_reader :shoe_size
  ...
    ~~~

52) ruby -I"." cards.rb

53) :point_down:
~~~ruby
describe Deck do
  describe "#to_a" do
    it "has 104 items with a shoe_size of 2" do
      Deck.new(2).to_a.count.must_equal 52
    end
    ...
  1. ruby -I"." cards.rb

  2. DONE!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.