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 Winner winner1;
    static Winner winner2;
    static Winner winner3;
    static Winner winner4;
    static Winner winner5;
    static Winner winner6;
    static Winner winner7;
    static Winner winner8;
    static Winner winner9;

    Because of = () =>
    {
      RockPaperScissors rps = new RockPaperScissors();
      winner1 = rps.Play( Move.Rock, Move.Scissors );
      winner2 = rps.Play( Move.Scissors, Move.Rock );

      winner3 = rps.Play( Move.Scissors, Move.Paper );
      winner4 = rps.Play( Move.Paper, Move.Scissors );

      winner5 = rps.Play( Move.Paper, Move.Rock );
      winner6 = rps.Play( Move.Rock, Move.Paper );

      winner7 = rps.Play( Move.Rock, Move.Rock );
      winner8 = rps.Play( Move.Scissors, Move.Scissors );
      winner9 = rps.Play( Move.Paper, Move.Paper );
    };

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

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

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

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

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

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

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

      shouldPlayAgainIfNoGamesPlayed = srs.ShouldPlayAgain( gameResults );

      gameResults.Add( Winner.First );

      shouldPlayAgainIfOneGameWon = srs.ShouldPlayAgain( gameResults );

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

      shouldPlayAgainIfDraws = srs.ShouldPlayAgain( gameResults );

      gameResults.Add( Winner.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( Winner.First );
  }

  [Subject( "Input" )]
  public class when_converting_char_to_move
  {
    static Move R;
    static Move Rlower;
    static Move P;
    static Move Plower;
    static Move S;
    static Move Slower;
    static Exception caught;

    Because of = () =>
    {
      R = InputConverter.Convert( 'R' );
      Rlower = InputConverter.Convert( 'r' );

      P = InputConverter.Convert( 'P' );
      Plower = InputConverter.Convert( 'p' );

      S = InputConverter.Convert( 'S' );
      Slower = InputConverter.Convert( 's' );

      caught = Catch.Exception( () => InputConverter.Convert( 'A' ) );
    };

    It should_convert_lower_or_upper_R_to_Rock = () =>
    {
      R.ShouldEqual( Move.Rock );
      Rlower.ShouldEqual( Move.Rock );
    };

    It should_convert_lower_or_upper_P_to_Paper = () =>
    {
      P.ShouldEqual( Move.Paper );
      Plower.ShouldEqual( Move.Paper );
    };

    It should_convert_lower_or_upper_S_to_Scissors = () =>
    {
      S.ShouldEqual( Move.Scissors );
      Slower.ShouldEqual( Move.Scissors );
    };

    It should_throw_exception_otherwise = () => caught.ShouldBeOfType( typeof( ArgumentOutOfRangeException ) );
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.