Skip to content

Commit

Permalink
Merge pull request #68 from SERG-Delft/cucumber
Browse files Browse the repository at this point in the history
Added a simple Cucumber test.
  • Loading branch information
avandeursen committed Apr 18, 2016
2 parents 763fe6f + 4ec7f0a commit e888dd6
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 8 deletions.
14 changes: 7 additions & 7 deletions doc/scenarios.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ When I press an arrow key towards that square;
Then my Pacman can move to that square
and my points remain the same.
Scenario S2.3: The player dies
Scenario S2.3: The move fails
Given the game has started,
and my Pacman is next to a cell containing a wall;
When I press an arrow key towards that cell;
Then the move is not conducted.
Scenario S2.4: The player dies
Given the game has started,
and my Pacman is next to a cell containing a ghost;
When I press an arrow key towards that square;
Then my Pacman dies,
and the game is over.
Scenario S2.4: The move fails
Given the game has started,
and my Pacman is next to a cell containing a wall;
When I press an arrow key towards that cell;
Then the move is not conducted.
Scenario S2.5: Player wins, extends S2.2
When I have eaten the last pellet;
Then I win the game.
Expand Down
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<junit.version>4.12</junit.version>
<mockito.version>2.0.47-beta</mockito.version>
<hamcrest.version>1.3</hamcrest.version>
<cucumber.version>1.2.2</cucumber.version>


<surefire.plugin.version>2.19.1</surefire.plugin.version>
<javadoc.plugin.version>2.10.3</javadoc.plugin.version>
Expand Down Expand Up @@ -84,6 +86,20 @@
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/nl/tudelft/jpacman/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
public class Launcher {

private static final PacManSprites SPRITE_STORE = new PacManSprites();

public static final String DEFAULT_MAP = "/board.txt";
private String levelMap = DEFAULT_MAP;

private PacManUI pacManUI;
private Game game;
Expand All @@ -39,6 +42,24 @@ public class Launcher {
public Game getGame() {
return game;
}

/**
* The map file used to populate the level.
* @return The name of the map file.
*/
protected String getLevelMap() {
return levelMap;
}

/**
* Set the name of the file containing this level's map.
* @param fileName Map to be used.
* @return Level corresponding to the given map.
*/
public Launcher withMapFile(String fileName) {
levelMap = fileName;
return this;
}

/**
* Creates a new game using the level from {@link #makeLevel()}.
Expand All @@ -60,12 +81,13 @@ public Game makeGame() {
public Level makeLevel() {
MapParser parser = getMapParser();
try (InputStream boardStream = Launcher.class
.getResourceAsStream("/board.txt")) {
.getResourceAsStream(getLevelMap())) {
return parser.parseMap(boardStream);
} catch (IOException e) {
throw new PacmanConfigurationException("Unable to create level.", e);
}
}


/**
* @return A new map parser object using the factories from
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/nl/tudelft/jpacman/cucumber/CucumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.tudelft.jpacman.cucumber;

import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

/**
* Entry point for running the the Cucumber tests in JUnit.
*
* @author Jan-Willem Gmelig Meyling
*/
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty"},
snippets = SnippetType.CAMELCASE,
features = "classpath:features")
public class CucumberTest {

/**
* This class should be empty, step definitions should be in separate classes.
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package nl.tudelft.jpacman.cucumber;

import static org.junit.Assert.assertTrue;

import cucumber.api.java.After;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import nl.tudelft.jpacman.Launcher;
import nl.tudelft.jpacman.game.Game;

/**
* Step definitions for the Cucumber tests.
*
* The steps also support setting up a {@link Game} object
* which other tests can use for further testing the game.
*
* @author Jan-Willem Gmelig Meyling, Arie van Deursen
*/
public class StateNavigationSteps {

private static Game theGame;

private Launcher launcher;

/**
* The Game created by the tests.
*
* @return Game created when starting up the game. Null if game has not been launched.
*/
public static Game getGame() {
return theGame;
}

private static void setGame(Game game) {
theGame = game;
}

/**
* Launch the game. This makes the game available via
* the {@link getGame} method.
*/
@Given("^the user has launched the JPacman GUI$")
public void theUserHasLaunchedTheJPacmanGUI() {
launcher = new Launcher();
launcher.launch();

setGame(launcher.getGame());
}

/**
* Start playing the game.
*/
@When("^the user presses the \"Start\" button$")
public void theUserPressesStart() {
getGame().start();
}

/**
* Verify that the play is actually running.
*/
@Then("^the game is running$")
public void theGameShouldStart() {
assertTrue(getGame().isInProgress());
}

/**
* Close the UI after all tests are finished.
*/
@After
public void tearDownUI() {
launcher.dispose();
}
}
9 changes: 9 additions & 0 deletions src/test/resources/features/startup.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: 1. Startup
As a player
I want to start the game
so that I can actually play

Scenario: S1.1 Startup
Given the user has launched the JPacman GUI
When the user presses the "Start" button
Then the game is running

0 comments on commit e888dd6

Please sign in to comment.