Source

RockPaperScissors / RockPaperScissors.Specs / GameSpecs.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Machine.Specifications;

namespace RockPaperScissors.Specs
{
  [Subject( "Round" )]
  public class when_playing_a_round
  {
    static Player winner1;
    static Player winner2;
    static Player winner3;
    static Player winner4;
    static Player winner5;
    static Player winner6;
    static Player winner7;
    static Player winner8;
    static Player winner9;

    Because of = () =>
    {
      RockPaperScissors rps = new RockPaperScissors();
      winner1 = rps.Play( "R", "S" );
      winner2 = rps.Play( "S", "R" );

      winner3 = rps.Play( "S", "P" );
      winner4 = rps.Play( "P", "S" );

      winner5 = rps.Play( "P", "R" );
      winner6 = rps.Play( "R", "P" );

      winner7 = rps.Play( "R", "R" );
      winner8 = rps.Play( "S", "S" );
      winner9 = rps.Play( "P", "P" );
    };

    It should_pick_rock_over_scissors = () =>
    {
      winner1.ShouldEqual( Player.First );
      winner2.ShouldEqual( Player.Second );
    };

    It should_pick_scissors_over_paper = () =>
    {
      winner3.ShouldEqual( Player.First );
      winner4.ShouldEqual( Player.Second );
    };

    It should_pick_paper_over_rock = () =>
    {
      winner5.ShouldEqual( Player.First );
      winner6.ShouldEqual( Player.Second );
    };

    It should_draw_on_rock_rock = () => winner7.ShouldEqual( Player.Draw );

    It should_draw_on_scissors_scissors = () => winner8.ShouldEqual( Player.Draw );
    
    It should_draw_on_paper_paper = () => winner9.ShouldEqual( Player.Draw );
  }

  [Subject( "Single Round" )]
  public class when_playing_a_single_round_game
  {
    static bool shouldPlayAgainIfNoGamesPlayed;
    static bool shouldPlayAgainIfOneGameWon;
    static bool shouldPlayAgainIfDraws;
    static Player whoWon;

    Because of = () =>
    {
      List<Player> gameResults = new List<Player>();
      SingleRoundStrategy srs = new SingleRoundStrategy();

      shouldPlayAgainIfNoGamesPlayed = srs.ShouldPlayAgain( gameResults );

      gameResults.Add( Player.First );

      shouldPlayAgainIfOneGameWon = srs.ShouldPlayAgain( gameResults );

      gameResults = new List<Player>();
      gameResults.Add( Player.Draw );
      gameResults.Add( Player.Draw );

      shouldPlayAgainIfDraws = srs.ShouldPlayAgain( gameResults );

      gameResults.Add( Player.First );

      whoWon = srs.GetWhoWon( gameResults );
    };

    It should_play_again_if_no_games_played = () => shouldPlayAgainIfNoGamesPlayed.ShouldBeTrue();

    It should_not_play_again_if_one_game_won = () => shouldPlayAgainIfOneGameWon.ShouldBeFalse();

    It should_play_again_if_games_drawn_but_none_won = () => shouldPlayAgainIfDraws.ShouldBeTrue();

    It should_know_who_won = () =>
      whoWon.ShouldEqual( Player.First );
  }
}