<commit_before>package org.amc.game.chess;

import static org.junit.Assert.*;

import org.amc.game.chess.ChessBoard.Coordinate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;

public class TestChessGame {

    private Player whitePlayer;
    private Player blackPlayer;
    private ChessBoard board;
    private ChessGame game;
    
    @Before
    public void setUp() throws Exception {
        whitePlayer=new HumanPlayer("Teddy", Colour.WHITE);
        blackPlayer=new HumanPlayer("Robin", Colour.BLACK);
        board=new ChessBoard();
        game=new ChessGame(whitePlayer,blackPlayer);
        game.setBoard(board);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testPlayerHasTheirKing(){
        KingPiece kingWhite=new KingPiece(Colour.WHITE);
        board.putPieceOnBoardAt(kingWhite, new Location(Coordinate.D,4));
        assertTrue(game.doesThePlayerStillHaveTheirKing(whitePlayer));
    }
    
    @Test
    public void testPlayerDoesNotHaveTheirKing(){
        KingPiece kingWhite=new KingPiece(Colour.WHITE);
        board.putPieceOnBoardAt(kingWhite, new Location(Coordinate.D,4));
        assertTrue(game.doesThePlayerStillHaveTheirKing(whitePlayer));
        assertFalse(game.doesThePlayerStillHaveTheirKing(blackPlayer));
    }
    
    @Test
    public void isGameOverOnePlayerLosesTheirKing(){
        board.initialise();
        BishopPiece bishop=new BishopPiece(Colour.WHITE);
        
        assertFalse(game.isGameOver(whitePlayer, blackPlayer));
        board.putPieceOnBoardAt(bishop,new Location(Coordinate.E,8));
        assertTrue(game.isGameOver(whitePlayer, blackPlayer));
        
        board.initialise();
        bishop=new BishopPiece(Colour.BLACK);
        
        assertFalse(game.isGameOver(whitePlayer, blackPlayer));
        board.putPieceOnBoardAt(bishop,new Location(Coordinate.E,1));
        assertTrue(game.isGameOver(whitePlayer, blackPlayer));
       
    }
    
    @Test
    public void testStartPlayerOneWinner(){
        final String[] winningMoves={"F1B5","C8A6","B5E8"};
        board.initialise();
        game.setView(new ChessBoardView(board));
        ConsoleController controller=new ConsoleController(board, whitePlayer, blackPlayer);
        controller.setConsole(new MockUserInput(winningMoves));
        game.setController(controller);
        game.start();
        assertTrue(whitePlayer.isWinner());
        assertFalse(blackPlayer.isWinner());
    }
    
    @Test
    public void testStartPlayerTwoWinner(){
        final String[] winningMoves={"F1E2","F8B4","E2D3","B4E1"};
        board.initialise();
        game.setView(new ChessBoardView(board));
        ConsoleController controller=new ConsoleController(board, whitePlayer, blackPlayer);
        controller.setConsole(new MockUserInput(winningMoves));
        game.setController(controller);
        game.start();
        assertFalse(whitePlayer.isWinner());
        assertTrue(blackPlayer.isWinner());
    }
    
    /**
     * Todo Need to add an assertion, just calls a line in a catch block in ChessGame.start()
     */
    @Test
    public void testStartThrowsInvalidMoveException(){
        final String[] winningMoves={"F1E1","F1E2","F8B4","E2D3","B4E1"};
        board.initialise();
        game.setView(new ChessBoardView(board));
        ConsoleController controller=new ConsoleController(board, whitePlayer, blackPlayer);
        controller.setConsole(new MockUserInput(winningMoves));
        game.setController(controller);
        game.start();
        assertFalse(whitePlayer.isWinner());
        assertTrue(blackPlayer.isWinner());
    }
    
    
    public static class MockUserInput implements ConsoleController.UserConsole{

        private String[] output={};
        private int counter=0;
        
        public MockUserInput(String[] desiredOutput) {
            this.output=desiredOutput;
        }
        
        @Override
        public String readLine(String fmt, Object... args) {
            return getNextOutput();
        }

        @Override<commit_msg>Tests fail due to call to board.initialise no longer returns expected ChessBoard<commit_after>package org.amc.game.chess;

import static org.junit.Assert.*;

import org.amc.game.chess.ChessBoard.Coordinate;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;

public class TestChessGame {

    private Player whitePlayer;
    private Player blackPlayer;
    private ChessBoard board;
    private ChessGame game;
    
    @Before
    public void setUp() throws Exception {
        whitePlayer=new HumanPlayer("Teddy", Colour.WHITE);
        blackPlayer=new HumanPlayer("Robin", Colour.BLACK);
        board=new ChessBoard();
        game=new ChessGame(whitePlayer,blackPlayer);
        game.setBoard(board);
    }

    public void setupChessBoardInSimpleTestState(){
        board.putPieceOnBoardAt(new BishopPiece(Colour.WHITE), new Location(Coordinate.C,1));
        board.putPieceOnBoardAt(new BishopPiece(Colour.WHITE), new Location(Coordinate.F,1));
        board.putPieceOnBoardAt(new KingPiece(Colour.WHITE), new Location(Coordinate.E, 1));
           
        board.putPieceOnBoardAt(new BishopPiece(Colour.BLACK), new Location(Coordinate.C,8));
        board.putPieceOnBoardAt(new BishopPiece(Colour.BLACK), new Location(Coordinate.F,8));
        board.putPieceOnBoardAt(new KingPiece(Colour.BLACK), new Location(Coordinate.E, 8));
    
    }
    
    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testPlayerHasTheirKing(){
        KingPiece kingWhite=new KingPiece(Colour.WHITE);
        board.putPieceOnBoardAt(kingWhite, new Location(Coordinate.D,4));
        assertTrue(game.doesThePlayerStillHaveTheirKing(whitePlayer));
    }
    
    @Test
    public void testPlayerDoesNotHaveTheirKing(){
        KingPiece kingWhite=new KingPiece(Colour.WHITE);
        board.putPieceOnBoardAt(kingWhite, new Location(Coordinate.D,4));
        assertTrue(game.doesThePlayerStillHaveTheirKing(whitePlayer));
        assertFalse(game.doesThePlayerStillHaveTheirKing(blackPlayer));
    }
    
    @Test
    public void isGameOverOnePlayerLosesTheirKing(){
        board.initialise();
        BishopPiece bishop=new BishopPiece(Colour.WHITE);
        
        assertFalse(game.isGameOver(whitePlayer, blackPlayer));
        board.putPieceOnBoardAt(bishop,new Location(Coordinate.E,8));
        assertTrue(game.isGameOver(whitePlayer, blackPlayer));
        
        board.initialise();
        bishop=new BishopPiece(Colour.BLACK);
        
        assertFalse(game.isGameOver(whitePlayer, blackPlayer));
        board.putPieceOnBoardAt(bishop,new Location(Coordinate.E,1));
        assertTrue(game.isGameOver(whitePlayer, blackPlayer));
       
    }
    
    @Test
    public void testStartPlayerOneWinner(){
        final String[] winningMoves={"F1B5","C8A6","B5E8"};
        setupChessBoardInSimpleTestState();
        game.setView(new ChessBoardView(board));
        ConsoleController controller=new ConsoleController(board, whitePlayer, blackPlayer);
        controller.setConsole(new MockUserInput(winningMoves));
        game.setController(controller);
        game.start();
        assertTrue(whitePlayer.isWinner());
        assertFalse(blackPlayer.isWinner());
    }
    
    @Test
    public void testStartPlayerTwoWinner(){
        final String[] winningMoves={"F1E2","F8B4","E2D3","B4E1"};
        setupChessBoardInSimpleTestState();
        game.setView(new ChessBoardView(board));
        ConsoleController controller=new ConsoleController(board, whitePlayer, blackPlayer);
        controller.setConsole(new MockUserInput(winningMoves));
        game.setController(controller);
        game.start();
        assertFalse(whitePlayer.isWinner());
        assertTrue(blackPlayer.isWinner());
    }
    
    /**
     * Todo Need to add an assertion, just calls a line in a catch block in ChessGame.start()
     */
    @Test
    public void testStartThrowsInvalidMoveException(){
        final String[] winningMoves={"F1E1","F1E2","F8B4","E2D3","B4E1"};
        setupChessBoardInSimpleTestState();
        game.setView(new ChessBoardView(board));
        ConsoleController controller=new ConsoleController(board, whitePlayer, blackPlayer);
        controller.setConsole(new MockUserInput(winningMoves));
        game.setController(controller);
        game.start();
        assertFalse(whitePlayer.isWinner());
        assertTrue(blackPlayer.isWinner());
    }
    
    
    public static class MockUserInput implements ConsoleController.UserConsole{

        private String[] output={};
        private int counter=0;
        
        public MockUserInput(String[] desiredOutput) {
            this.output=desiredOutput;
        }
        
        @Override
        public String readLine(String fmt, Object... args) {
            return getNextOutput();
        }

        @Override
{'fraction_non_alphanumeric': 0.10464471788050106, 'fraction_numerical': 0.00709455714444075, 'mean_word_length': 3.1962790697674417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3744038', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2576, 'n_words': 449}