Skip to content

Instantly share code, notes, and snippets.

@pdbradley
Created October 16, 2017 19:31
Show Gist options
  • Save pdbradley/512ef9b46267b207007ee8cf4570fbab to your computer and use it in GitHub Desktop.
Save pdbradley/512ef9b46267b207007ee8cf4570fbab to your computer and use it in GitHub Desktop.
ali king spec
require 'rails_helper'
RSpec.describe King, type: :class do
describe '.valid_move?' do
it "should check for valid move for a King" do
user = FactoryGirl.create(:user)
piece = FactoryGirl.create(:king, user_id: user.id)
piece.x = 4; piece.y = 4; piece.color = "white"
expect(piece.valid_move?(piece.x+1, piece.y+0)).to eq(true)
expect(piece.valid_move?(piece.x-1, piece.y+0)).to eq(true)
expect(piece.valid_move?(piece.x+0, piece.y+1)).to eq(true)
expect(piece.valid_move?(piece.x+0, piece.y-1)).to eq(true)
end
it "should check for invalid move for a King" do
user = FactoryGirl.create(:user)
piece = FactoryGirl.create(:king, user_id: user.id)
piece.x = 4; piece.y = 4; piece.color = "white"
expect(piece.valid_move?(piece.x+0, piece.y+0)).to eq(false)
expect(piece.valid_move?(piece.x+2, piece.y+0)).to eq(false)
expect(piece.valid_move?(piece.x-3, piece.y+0)).to eq(false)
expect(piece.valid_move?(piece.x+0, piece.y+2)).to eq(false)
expect(piece.valid_move?(piece.x+0, piece.y-3)).to eq(false)
expect(piece.valid_move?(piece.x+2, piece.y-3)).to eq(false)
end
end
describe '.moved?' do
it "should check if the piece has moved" do
user = FactoryGirl.create(:user)
king = FactoryGirl.create(:king, user_id: user.id)
expect(king.moved?).to eq(false)
end
end
describe '.is_castle_move?' do
# on the bottom row, (2,0) and (6,0) are valid castling destinations
it "should return false if the target is not one of the possibles" do
game = blank_game
king = FactoryGirl.create(:king, user_id: game.white_player_id, game: game)
expect(king.is_castle_move?(2,1)).to be false
end
it "should return false if the target is occupied" do
game = blank_game
king = FactoryGirl.create(:king, user_id: game.white_player_id, game: game)
FactoryGirl.create(:bishop, user_id: game.white_player_id, game: game, x: 0, y:2)
expect(king.is_castle_move?(0,2)).to be false
end
it "should return false if the appropriate rook is not there" do
game = blank_game
king = FactoryGirl.create(:king, user_id: game.white_player_id, game: game)
FactoryGirl.create(:rook, user_id: game.white_player_id, game: game, x: 0, y:0)
expect(king.is_castle_move?(0,2)).to be false
end
it "should return true if move is a castle move" do
game = blank_game
king = FactoryGirl.create(:king, user_id: user.id, game: game)
expect(king.is_castle_move?(king.x-2, king.y)).to eq(true)
end
end
describe '.valid_castle_move?' do
it "should check for a valid castle move" do
user = FactoryGirl.create(:user)
king = FactoryGirl.create(:king, user_id: user.id)
expect(king.valid_castle_move?).to eq(true)
end
end
def blank_game
user = FactoryGirl.create(:user)
game = FactoryGirl.create(:game, white_player_id: user.id)
game.chess_pieces.destroy_all
game
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment