Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Last active August 29, 2015 14:23
Show Gist options
  • Save MaxPleaner/9d00e0edded890f3662c to your computer and use it in GitHub Desktop.
Save MaxPleaner/9d00e0edded890f3662c to your computer and use it in GitHub Desktop.
RSpec for Crazy 8s
RSpec.describe 'Player can view their own hand and the decks visible card' do
let(:game) { Game.new(2) }
subject(:player) { game.players[0] }
let(:deck) { game.deck }
it "can see visible card" do
expect(player.see_visible).to be_a(Card)
end
end
RSpec.describe "player can try and make move" do
let(:game) { Game.new(2) }
subject(:player) { game.players[0] }
let(:deck) { game.deck }
it "accepts a play if card matches the suit or value of the visible card" do
deck.visible = Card.new(:hearts, :five)
player.hand.cards = [Card.new(:hearts, :six)]
selection = player.hand.cards[0]
expect(player.play(selection)).to be true
end
it "rejects invalid plays" do
deck.visible = Card.new(:hearts, :five)
player.hand.cards = [Card.new(:diamonds, :four)]
selection = player.hand.cards[0]
expect(player.play(selection)).to be false
end
end
RSpec.describe "#play! if #play validates move" do
let(:game) { Game.new(2) }
subject(:player) { game.players[0] }
let(:deck) { game.deck }
before(:each) do
deck.visible = Card.new(:hearts, :five)
player.hand.cards = [Card.new(:diamonds, :five)]
@selection = player.hand.cards[0]
@initial_deck_length = deck.cards.length
@new_card = deck.cards[0]
player.play!(@selection)
end
it "puts a card on the top of the deck" do
expect(deck.visible).to eq(@selection)
end
it "removes played card from player's hand" do
expect(player.hand.cards).to_not include(@selection)
end
end
RSpec.describe "#has_moves? looks for valid moves" do
let(:game) { Game.new(2) }
subject(:player) { game.players[0] }
let(:deck) { game.deck }
before(:each) do
deck.visible = Card.new(:hearts, :five)
end
it "returns true if user has a valid move" do
player.hand.cards = [Card.new(:hearts, :four)]
expect(player.has_moves?).to be true
end
it "returns false if user doesn't have valid move" do
player.hand.cards = [Card.new(:diamonds, :four)]
expect(player.has_moves?).to be false
end
end
RSpec.describe "#make_move_exist" do
let(:game) { Game.new(2) }
subject(:player) { game.players[0] }
let(:deck) { game.deck }
before(:each) do
deck.visible = Card.new(:hearts, :five)
player.hand.cards = [Card.new(:diamonds, :four)]
@draw_card_1 = Card.new(:spades, :three)
@draw_card_2 = Card.new(:hearts, :king)
@draw_card_3 = Card.new(:hearts, :queen)
deck.cards = [@draw_card_1, @draw_card_2, @draw_card_3]
end
it "gives player new cards if none are available" do
player.make_move_exist
expect(player.hand.cards).to include(@draw_card_1, @draw_card_2)
expect(player.hand.cards).to_not include(@draw_card_3)
end
it "doesn't draw cards for player if a play is available" do
player.hand.cards = [Card.new(:hearts, :four)]
player.make_move_exist
expect(player.hand.cards).to_not include(@draw_card_1)
end
end
RSpec.describe "Player plays 8" do
let(:game) { Game.new(2) }
subject(:player) { game.players[0] }
let(:deck) { game.deck }
before(:each) do
deck.visible = Card.new(:hearts, :five)
deck.cards[0] = Card.new(:spades, :four)
@eight_card = Card.new(:diamonds, :eight)
player.hand.cards = [@eight_card]
end
it "allows player to play 8 despite mismatched suit" do
player.play(player.hand.cards[0])
expect(player.hand.cards).to_not include(@eight_card)
expect(deck.visible).to eq(@eight_card)
end
it "allows player to play 8 on another 8" do
deck.cards[0] = Card.new(:spades, :eight)
player.play(player.hand.cards[0])
expect(player.hand.cards).to_not include(@eight_card)
expect(deck.visible).to eq(@eight_card)
end
it "records next required suit form user input" do
player.play(player.hand.cards[0])
expect([:hearts, :diamonds, :spades, :clubs]).to include(deck.suit_after_8)
end
it "limits valid plays to suit_after_8 if visible card is 8" do
player.hand.cards = [@eight_card, Card.new(:diamonds, :ten)]
player.play(player.hand.cards[0])
expect(player.play(player.hand.cards[0])).to be false
end
it "does not filter by suit_after_8 if visible card is not 8" do
player.hand.cards = [@eight_card, Card.new(:hearts, :ten)]
expect(player.play(player.hand.cards[0])).to be true
end
end
require 'rspec'
require 'game'
RSpec.describe "Initialization" do
subject(:game) { Game.new(2) }
let(:game_players) { game.players }
it "Makes each player start with 8 cards" do
expect(game_players.all? { |p| p.hand.cards.length == 8 } ).to be true
end
let(:game_deck) { game.deck }
it "makes top card turned face up to start the game" do
expect(game_deck.visible).to be_a(Card)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment