require 'test/unit'

class WrongNumberOfPlayersError < StandardError ; end 
class NoSuchStrategyError < StandardError ; end 
 
STRATS = ['R','P','S']
PLAYER2_WINS = [['R','P'], ['P','S'],['S','R']]

def rps_game_winner(game) 
    raise WrongNumberOfPlayersError unless game.length == 2 

    # strategy error checking
    raise NoSuchStrategyError unless game[0].length == 2 && game[1].length == 2
    strat1 = game[0][1].upcase
    strat2 = game[1][1].upcase
    raise NoSuchStrategyError unless STRATS.include?(strat1) && STRATS.include?(strat2)

    matchup = [strat1, strat2]
    return PLAYER2_WINS.include?(matchup) ? game[1] : game[0] # player1 wins if 2 doesn't
end 


class RpsWinner < Test::Unit::TestCase

    def test_examples
        expected = ['John', 'P']
        actual = rps_game_winner([['John', 'P'], ['Rick', 'R']])
        assert_equal(expected, actual)

        expected = ['John', 'P']
        actual = rps_game_winner([['John', 'P'], ['Rick', 'R']])
        assert_equal(expected, actual)
    end

    def test_num_of_players_errors
        assert_raise WrongNumberOfPlayersError do
            rps_game_winner([['John', 'P'], ['Rick', 'R'], ['Rick', 'R']])
        end
        assert_raise WrongNumberOfPlayersError do
            rps_game_winner([['John', 'P']])
        end
        assert_raise WrongNumberOfPlayersError do
            rps_game_winner("Hello")
        end
        assert_raise WrongNumberOfPlayersError do
            rps_game_winner([])
        end
        assert_raise WrongNumberOfPlayersError do
            rps_game_winner([[]])
        end
    end

    def test_num_of_players_errors
        assert_raise NoSuchStrategyError do
            rps_game_winner([['John', 'Q'], ['Rick', 'R']])
        end
        assert_raise NoSuchStrategyError do
            rps_game_winner([['John', 'R'], ['Rick', 'X']])
        end
        assert_nothing_raised NoSuchStrategyError do
            rps_game_winner([['John', 'p'], ['Rick', 'r']])
        end
    end
end
                                  