<commit_before>package test.java.org.jpacman.test.framework.model;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import main.java.org.jpacman.framework.factory.UndoGameFactory;
import main.java.org.jpacman.framework.model.UndoableGame;

import org.jpacman.framework.factory.FactoryException;
import org.jpacman.framework.factory.IGameFactory;
import org.jpacman.framework.factory.MapParser;
import org.jpacman.framework.model.Direction;
import org.jpacman.framework.model.Food;
import org.jpacman.framework.model.Player;
import org.jpacman.framework.model.Tile;
import org.jpacman.test.framework.model.GameTest;
import org.junit.Test;

public class UndoableGameTest extends GameTest {

	@Override
	public IGameFactory makeFactory() {
		return new UndoGameFactory();
	}

	/**
	 * Simply create a single row game. The resulting game is returned, and can also be obtained via
	 * getGame().
	 * 
	 * @param singleRow
	 *            String representation of one row
	 * @throws FactoryException
	 *             If singleRow contains illegal chars.
	 * @return The game created while parsing the row.
	 */
	@Override
	protected UndoableGame makePlay(String singleRow) throws FactoryException {
		MapParser p = new MapParser(makeFactory());
		UndoableGame theGame = (UndoableGame) p.parseMap(new String[] { singleRow });
		return theGame;
	}

	@Test
	public void testC1a_PlayerMovesToEmptyAndUndo() throws FactoryException {
		UndoableGame g = makePlay("P #");
		g.movePlayer(Direction.RIGHT);

		assertEquals("Player moved", tileAt(g, 1, 0), g.getPlayer().getTile());
		assertEquals("No food eaten.", 0, g.getPlayer().getPoints());
		assertEquals(Direction.RIGHT, g.getPlayer().getDirection());

		g.undo();
		assertEquals("Undo Player moved", tileAt(g, 0, 0), g.getPlayer().getTile());
		assertEquals(Direction.LEFT, g.getPlayer().getDirection());

	}

	@Test
	public void testC2a_PlayerMovesToWallAndUndo() throws FactoryException {
		UndoableGame g = makePlay("#P.");
		g.movePlayer(Direction.LEFT);
		assertThat("Still at the start", tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));
		g.undo();
		String msg =
		        "Still at the start after undo (original,current) " + tileAt(g, 1, 0) + " "
		                + g.getPlayer().getTile();
		assertThat(msg, tileAt(g, 1, 0), equalTo(g.getPlayer()
		        .getTile()));

		g.movePlayer(Direction.LEFT);
		assertThat("A2.Still at the start", tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));
		g.undo();
		String msg2 =
		        "A2.Still at the start after undo (original,current) " + tileAt(g, 1, 0) + " "
		                + g.getPlayer().getTile();
		assertThat(msg2, tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));
	}

	@Test
	public void testC3_PlayerMovesToGhostAndUndo() throws FactoryException {
		UndoableGame g = makePlay("PG#");
		Player p = g.getPlayer();

		g.movePlayer(Direction.RIGHT);

		assertFalse("Move kills player", p.isAlive());
		assertThat(tileAt(g, 1, 0), equalTo(p.getTile()));

		g.undo();
		assertFalse("Player remains dead", p.isAlive());

		assertThat("Player remains unmoved", tileAt(g, 1, 0), equalTo(p.getTile()));
	}

	/**
	 * Test that a player indeed consumes food if he enters food.
	 * 
	 * @throws FactoryException
	 *             Never.
	 */
	@Test
	public void testC4_PlayerMovesToFoodAndUndo() throws FactoryException {
		UndoableGame game = makePlay("P.#");
		Food food = (Food) game.getBoard().spriteAt(1, 0);
		Player player = game.getPlayer();

		int pts = game.getPlayer().getPoints();

		game.movePlayer(Direction.RIGHT);

		Tile newTile = tileAt(game, 1, 0);
		assertEquals("Food added", food.getPoints(), player.getPoints());
		assertEquals("Player moved", newTile.topSprite(), player);
		assertFalse("Food gone", newTile.containsSprite(food));

		game.undo();

		player = game.getPlayer();
		Tile oldTile = tileAt(game, 0, 0);

		System.out.println("PlayerMovesToFoodUndo");
		assertEquals("Player moved undo", oldTile.topSprite(), player);
		assertEquals("Player points removed", pts, game.getPlayer().getPoints());
		assertTrue("Food replaced", newTile.containsSprite(food));
	}

}<commit_msg>Adding several test cases for Player movements. Player movements tests are passing. Added test cases for player movements interleaved with Food consumption.<commit_after>package test.java.org.jpacman.test.framework.model;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import main.java.org.jpacman.framework.factory.UndoGameFactory;
import main.java.org.jpacman.framework.model.UndoableGame;

import org.jpacman.framework.factory.FactoryException;
import org.jpacman.framework.factory.IGameFactory;
import org.jpacman.framework.factory.MapParser;
import org.jpacman.framework.model.Direction;
import org.jpacman.framework.model.Food;
import org.jpacman.framework.model.Player;
import org.jpacman.framework.model.Tile;
import org.jpacman.test.framework.model.GameTest;
import org.junit.Test;

public class UndoableGameTest extends GameTest {

	@Override
	public IGameFactory makeFactory() {
		return new UndoGameFactory();
	}

	/**
	 * Simply create a single row game. The resulting game is returned, and can also be obtained via
	 * getGame().
	 * 
	 * @param singleRow
	 *            String representation of one row
	 * @throws FactoryException
	 *             If singleRow contains illegal chars.
	 * @return The game created while parsing the row.
	 */
	@Override
	protected UndoableGame makePlay(String singleRow) throws FactoryException {
		MapParser p = new MapParser(makeFactory());
		UndoableGame theGame = (UndoableGame) p.parseMap(new String[] { singleRow });
		return theGame;
	}

	//
	// Player Moves Test Cases
	//
	/**
	 * Player Moves to an Empty Cell & Undo
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP1_PlayerMovesToEmptyAndUndo() throws FactoryException {
		UndoableGame g = makePlay("P #");
		g.movePlayer(Direction.RIGHT);

		assertEquals("Player moved", tileAt(g, 1, 0), g.getPlayer().getTile());
		assertEquals("No food eaten.", 0, g.getPlayer().getPoints());
		assertEquals(Direction.RIGHT, g.getPlayer().getDirection());

		g.undo();
		assertEquals("Undo Player moved", tileAt(g, 0, 0), g.getPlayer().getTile());
		assertEquals(Direction.LEFT, g.getPlayer().getDirection());

	}

	/**
	 * Player Moves into a Wall & Undo
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP2_PlayerMovesToWallAndUndo() throws FactoryException {
		UndoableGame g = makePlay("#P.");
		g.movePlayer(Direction.LEFT);
		assertThat("Still at the start", tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));
		g.undo();
		String msg =
		        "Still at the start after undo (original,current) " + tileAt(g, 1, 0) + " "
		                + g.getPlayer().getTile();
		assertThat(msg, tileAt(g, 1, 0), equalTo(g.getPlayer()
		        .getTile()));

		g.movePlayer(Direction.LEFT);
		assertThat("A2.Still at the start", tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));
		g.undo();
		String msg2 =
		        "A2.Still at the start after undo (original,current) " + tileAt(g, 1, 0) + " "
		                + g.getPlayer().getTile();
		assertThat(msg2, tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));
	}


	/**
	 * Player Moves to a Food cell & Undo
	 * 
	 * @throws FactoryException
	 *             Never.
	 */
	@Test
	public void testP3_PlayerMovesToFoodAndUndo() throws FactoryException {
		UndoableGame game = makePlay("P.#");
		Food food = (Food) game.getBoard().spriteAt(1, 0);
		Player player = game.getPlayer();

		int pts = game.getPlayer().getPoints();

		game.movePlayer(Direction.RIGHT);

		Tile newTile = tileAt(game, 1, 0);
		assertEquals("Food added", food.getPoints(), player.getPoints());
		assertEquals("Player moved", newTile.topSprite(), player);
		assertFalse("Food gone", newTile.containsSprite(food));

		game.undo();

		player = game.getPlayer();
		Tile oldTile = tileAt(game, 0, 0);

		System.out.println("PlayerMovesToFoodUndo");
		assertEquals("Player moved undo", oldTile.topSprite(), player);
		assertEquals("Player points removed", pts, game.getPlayer().getPoints());
		assertTrue("Food replaced", newTile.containsSprite(food));
	}

	/**
	 * Player Moves to Ghost & Undo
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP4_PlayerMovesToGhostAndUndo() throws FactoryException {
		UndoableGame g = makePlay("PG#");
		Player p = g.getPlayer();

		g.movePlayer(Direction.RIGHT);

		assertFalse("Move kills player", p.isAlive());
		assertThat(tileAt(g, 1, 0), equalTo(p.getTile()));

		g.undo();
		assertFalse("Player remains dead", p.isAlive());

		assertThat("Player remains unmoved", tileAt(g, 1, 0), equalTo(p.getTile()));
	}

	/**
	 * Undo Player at the start of the game.
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP5_PlayerMovesToEmptyAndUndo() throws FactoryException {
		UndoableGame g = makePlay("P #");

		g.undo();
		assertTrue("Player remains alive", g.getPlayer().isAlive());
		assertEquals("Undo Player moved", tileAt(g, 0, 0), g.getPlayer().getTile());

	}

	//
	// Player Composite Moves Test Cases
	//
	/**
	 * Player moves into Empty cell, original position, then Wall & 4xUndo
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP_C1_PlayerCompositeMoveEmptyWallAndUndo() throws FactoryException {
		UndoableGame g = makePlay("#P ");
		g.movePlayer(Direction.RIGHT);
		g.movePlayer(Direction.LEFT);
		g.movePlayer(Direction.LEFT);
		assertThat("Still at the start", tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));

		g.undo();
		g.undo();
		g.undo();
		g.undo();

		String msg =
		        "Still at the start after undo (original,current) " + tileAt(g, 1, 0) + " "
		                + g.getPlayer().getTile();
		assertThat(msg, tileAt(g, 1, 0), equalTo(g.getPlayer().getTile()));

	}

	/**
	 * Player moves across two Empty Cells and 3xUndo
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP_C2_PlayerCompositeMoveMultipleEmptyAndUndo() throws FactoryException {
		UndoableGame g = makePlay("P  #");
		g.movePlayer(Direction.RIGHT);
		g.movePlayer(Direction.RIGHT);
		g.movePlayer(Direction.RIGHT);

		g.undo();
		g.undo();
		g.undo();

		String msg =
		        "Still at the start after undo (original,current) " + tileAt(g, 0, 0) + " "
		                + g.getPlayer().getTile();
		assertThat(msg, tileAt(g, 0, 0), equalTo(g.getPlayer().getTile()));

	}

	/**
	 * Move across Food, Empty Cells, Wall and 3xUndo
	 * 
	 * @throws FactoryException
	 */
	@Test
	public void testP_C3_PlayerCompositeMoveFoodWallAndUndo() throws FactoryException {
		UndoableGame g = makePlay("P. #");
		Food food = (Food) g.getBoard().spriteAt(1, 0);
		Tile foodTile = tileAt(g, 1, 0);

		int pts = g.getPlayer().getPoints();

		g.movePlayer(Direction.RIGHT);
		g.movePlayer(Direction.LEFT);
		g.movePlayer(Direction.LEFT);
		assertThat("Still at the start", tileAt(g, 0, 0), equalTo(g.getPlayer().getTile()));

		g.undo();
		g.undo();
		g.undo();

		Tile oldTile = tileAt(g, 0, 0);

		assertEquals("Player moved undo", oldTile.topSprite(), g.getPlayer());
		assertEquals("Player points removed", pts, g.getPlayer().getPoints());
		assertTrue("Food replaced", foodTile.containsSprite(food));
	}

	//
	// Ghost Move Test Cases
	//

	// TODO: Test cases missing
}
{'fraction_non_alphanumeric': 0.14295610009524634, 'fraction_numerical': 0.00839899558403325, 'mean_word_length': 4.140186915887851, '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': '1919195', 'n_tokens_mistral': 4075, 'n_tokens_neox': 3642, 'n_words': 880}