Skip to content

Commit

Permalink
step definitions + running tests + documentation #3
Browse files Browse the repository at this point in the history
  • Loading branch information
bendi1119 committed May 18, 2024
1 parent 65e820d commit 3ee7db0
Show file tree
Hide file tree
Showing 22 changed files with 525 additions and 28 deletions.
16 changes: 15 additions & 1 deletion docs/BDD_DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,23 @@ Eljött az idő a lépések definiáláshoz.

**Lépésdefiníciók megírása**

Az IntelliJ segítségével szerencsére egyszerűen legenerálható a `StepDefinitions.java` file, amely már tartalmazza az implementálandó lépések vázát (illetve új lépések is könnyen hozzáadhatók). Ezekben a generált függvényekben megírom a szükséges lépések implementációját. A függvényekbe kerülhetnek a CmdInterpreter osztály megfelelő parancsvégrehajtásai, értékadás segédváltozóknak (pl. aktív játékos eltárolása), illetve a *Then* típusú függvényekbe a a jUnit-ból már ismert *assertEquals* / *assertTrue*, amelyek segítségével ellenőrzni tudom, hogy a lépések eredményeként az elvárt kimenetet kapjuk-e. Extra lépésként kicsit módosítok a feature fájlokban definiált prekondíciók közül is néhányon (vagy a sorrendjükön), hogy csökkentsem a redundanciát. Pl.: a specifikációban különböző eseteknél az "I fix the pipe" és az "I try to fix the pipe" lépés is megtalálható, de mivel mindkettő esetén ugyanaz a parancs hajtódik végre a háttérben, ezért elég hogyha csak az egyik szerepel a feature fájlban.

**Tesztek futtatása**

**Eredmények analizálása**
Miután egy funkció feature file-ja és a mögöttes lépésdefiníciók is rendelkezésre állnak, futtathatjuk az adott tesztet az IntelliJ segítségével. Ehhez egyszerűen jobb egérgombbal rákattintok a teszthez tartozó `.feature` file-ra, majd a megjelenő menüben a `"Run: Feature (...)"` opciót választom:

![Run BDD test](images/runbddtest.png)

Ekkor (hogyha mindent jól csináltam) megjelenik egy `Test Result` terminál ablak, ahol láthatom a teszt futásának eredményét:

![BDD test results](images/bddtestresults.png)

Ha a teszt futása sikertelen (pl. elírtam a CmdInterpreter valamelyik parancsát), akkor a terminálnak valami ilyesmit kell mutatnia:

![Failed BDD test](images/failedbddtest.png)

**Eredmények**

**Tanulság**

Expand Down
Binary file added docs/images/bddtestresults.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/failedbddtest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/runbddtest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
306 changes: 306 additions & 0 deletions src/test/java/StepDefinitions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
import io.cucumber.java.Before;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StepDefinitions {

CmdInterpreter cmd = new CmdInterpreter();
String activePlayer = "";
String activeComponent = "";
String testResult = "";
String neighbourComponent = "";

@Before
public void setup() {
cmd.NewCommand("Load:testmap.txt");
activePlayer = "";
activeComponent = "";
testResult = "";
neighbourComponent = "";
}

@Given("that a pipe has been punctured")
public void thatAPipeHasBeenPunctured() {
activeComponent = "pi1";
cmd.NewCommand("ChangeBreakState:"+activeComponent+",broken");
}

@And("I am a Mechanic")
public void iAmAMechanic() {
activePlayer = "me1";
}

@And("I am standing on the pipe")
public void iAmStandingOnThePipe() {
if(Objects.equals(activePlayer, ""))
{
activePlayer = "me1";
}
activeComponent = "pi1";
cmd.NewCommand("SetPlayerPos:"+activePlayer+","+activeComponent);
}

@And("I have enough AP")
public void iHaveEnoughAP() {
cmd.NewCommand("SetAP:"+activePlayer+",10");
}

@When("I fix the pipe")
public void iFixThePipe() {
testResult = cmd.NewCommand("PlayerSimpleAction:"+activePlayer+",fix");
}

@Then("it should be functional again")
public void itShouldBeFunctionalAgain() {
assertEquals("[SIKERES] A(z) "+activePlayer+" jatekos megszerelte a komponenst, amin all", testResult);
}

@And("I don't have enough AP")
public void iDonTHaveEnoughAP() {
cmd.NewCommand("SetAP:"+activePlayer+",0");
}

@Then("the pipe shouldn't be fixed")
public void thePipeShouldnTBeFixed() {
assertEquals("[HIBA] A jatekosnak nincs eleg AP-je a muvelet elvegzesehez!", testResult);
}

@Given("that I am the active player")
public void thatIAmTheActivePlayer() {
activePlayer = "me1";
}

@When("I pass my turn")
public void iPassMyTurn() {
testResult = cmd.NewCommand("PlayerSimpleAction:"+activePlayer+",pass");
}

@Then("my turn should be over")
public void myTurnShouldBeOver() {
assertEquals("[SIKERES] A(z) "+activePlayer+" jatekos passzolt", testResult);
}

@Given("that I am a Cistern")
public void thatIAmACistern() {
activeComponent = "ci1";
}

@And("I have no generated pumps")
public void iHaveNoGeneratedPumps() {
cmd.NewCommand("ToggleRandom:false"); //véletlenszerű eventek letiltása, a ciszternán így nem generálódhat pumpa
}

@When("it is time to generate a pump")
public void itIsTimeToGenerateAPump() {
testResult = cmd.NewCommand("GeneratePump:"+activeComponent);
}

@Then("the pump should get generated")
public void thePumpShouldGetGenerated() {
assertEquals("[SIKERES]: Pumpa sikeresen legeneralva!", testResult);
}

@And("I already have a generated pump")
public void iAlreadyHaveAGeneratedPump() {
cmd.NewCommand("GeneratePump:"+activeComponent);
}

@Then("the pump shouldn't get generated")
public void thePumpShouldnTGetGenerated() {
assertEquals("[HIBA]: nem lehet letrehozni a pumpat a "+activeComponent+" komponensen, mert mar van rajta egy masik", testResult);
}

@Given("that a cistern has a generated pump")
public void thatACisternHasAGeneratedPump() {
activeComponent = "ci1";
cmd.NewCommand("GeneratePump:"+activeComponent);
}

@And("I am standing on the cistern")
public void iAmStandingOnTheCistern() {
activePlayer = "me1";
cmd.NewCommand("SetPlayerPos:"+activePlayer+","+activeComponent);
}

@And("my inventory is empty")
public void myInventoryIsEmpty() {
cmd.NewCommand("SetPlayerPos:"+activePlayer+",pi3"); //Player gets moved to a Pipe
cmd.NewCommand("PlayerSimpleAction:"+activePlayer+",place"); //Player attempts to place down pump (if he has a pump, it will be placed down, so now his inventory is empty)
cmd.NewCommand("SetPlayerPos:"+activePlayer+","+activeComponent); //Player gets moved back to the Cistern
}

@When("I attempt to collect the pump")
public void iAttemptToCollectThePump() {
testResult = cmd.NewCommand("PlayerSimpleAction:"+activePlayer+",collect");
}

@Then("the pump should be collected")
public void thePumpShouldBeCollected() {
assertEquals("[SIKERES] A(z) "+activePlayer+" jatekos osszeszedett egy pumpat", testResult);
}

@Then("the pump shouldn't be collected")
public void thePumpShouldnTBeCollected() {
assertTrue(testResult.contains("[HIBA]"));
}

@And("my inventory is not empty")
public void myInventoryIsNotEmpty() {
cmd.NewCommand("SetItem:"+activePlayer);
}

@Given("that a cistern has no generated pumps")
public void thatACisternHasNoGeneratedPumps() {
cmd.NewCommand("ToggleRandom:false"); //véletlenszerű eventek letiltása, a ciszternán így nem generálódhat pumpa
activeComponent = "ci1";
}

@Then("there is no pump to collect")
public void thereIsNoPumpToCollect() {
assertEquals("[HIBA] A jatekos jelenlegi poziciojan nincs generalt pumpa!", testResult);
}

@Given("that a pipe is not slippery")
public void thatAPipeIsNotSlippery() {
activeComponent = "pi1";
cmd.NewCommand("ChangePipeState:"+activeComponent+",Normal");
}

@And("I am a Saboteur")
public void iAmASaboteur() {
activePlayer = "sa1";
}

@When("I attempt to make the pipe slippery")
public void iAttemptToMakeThePipeSlippery() {
testResult = cmd.NewCommand("PlayerSimpleAction:"+activePlayer+",slippery");
}

@Then("the pipe should get slippery")
public void thePipeShouldGetSlippery() {
assertEquals("[SIKERES] A(z) "+activePlayer+" jatekos csuszossa tette a komponenst, amin all", testResult);
}

@Given("that a pipe is slippery")
public void thatAPipeIsSlippery() {
activeComponent = "pi1";
cmd.NewCommand("ChangePipeState:"+activeComponent+",Slippery");
}

@Then("nothing should happen, as the pipe is already slippery")
public void nothingShouldHappenAsThePipeIsAlreadySlippery() {
assertEquals("[HIBA] Ez a tipusu komponens nem teheto csuszossa! (Elvart: Pipe)", testResult); //Egy picit csalóka, de ez a helyes hibaüzenet
}

@Then("the pipe shouldn't get slippery")
public void thePipeShouldnTGetSlippery() {
assertEquals("[HIBA] A jatekosnak nincs eleg AP-je a muvelet elvegzesehez!", testResult);
}

@Given("that I am a pump")
public void thatIAmAPump() {
activeComponent = "pu1";
}

@And("I am functional")
public void iAmFunctional() {
cmd.NewCommand("ChangeBreakState:"+activeComponent+",breakable");
}

@When("I experience a breakdown")
public void iExperienceABreakdown() {
testResult = cmd.NewCommand("BreakPump:"+activeComponent);
}

@Then("I should become non-functional")
public void iShouldBecomeNonFunctional() {
assertEquals("[SIKERES] "+activeComponent+" komponens elrontva", testResult);
}

@And("I am non-functional")
public void iAmNonFunctional() {
cmd.NewCommand("ChangeBreakState:"+activeComponent+",broken");
}

@Then("nothing happens, as I am already broken")
public void nothingHappensAsIAmAlreadyBroken() {
assertEquals("[HIBA] "+activeComponent+" nem rontható el, mivel már elromlott", testResult);
}

@Given("that I intend to move to an existing neighbour")
public void thatIIntendToMoveToAnExistingNeighbour() {
activePlayer = "me1";
neighbourComponent = "pu1";
}

@And("I am not stuck")
public void iAmNotStuck() {
cmd.NewCommand("UnstickPlayer:"+activePlayer);
}

@When("I attempt to move to the selected component")
public void iAttemptToMoveToTheSelectedComponent() {
testResult = cmd.NewCommand("MovePlayer:"+activePlayer+","+neighbourComponent);
}

@Then("the movement should be successful")
public void theMovementShouldBeSuccessful() {
assertEquals("[SIKERES]: "+activePlayer+" sikeresen atlepett a "+neighbourComponent+" komponensre!", testResult);
}

@And("I am stuck")
public void iAmStuck() {
cmd.NewCommand("MovePlayer:"+activePlayer+","+neighbourComponent); //Temporarily move player to neighbouring component
cmd.NewCommand("ChangePipeState:"+activeComponent+",Sticky"); //Turn the pipe sticky
cmd.NewCommand("MovePlayer:"+activePlayer+","+activeComponent); //Move player back to pipe, so he gets stuck
}

@Then("the movement should be unsuccessful")
public void theMovementShouldBeUnsuccessful() {
assertTrue(testResult.contains("[HIBA]"));
}

@And("that I intend to move to a non-existing neighbour")
public void thatIIntendToMoveToANonExistingNeighbour() {
neighbourComponent = "pu100";
}

@Given("that a pipe is working")
public void thatAPipeIsWorking() {
activeComponent = "pi1";
cmd.NewCommand("ChangeBreakState:"+activeComponent+",breakable");
}

@When("I puncture the pipe")
public void iPunctureThePipe() {
testResult = cmd.NewCommand("PlayerSimpleAction:"+activePlayer+",puncture");
}

@Then("it should be punctured")
public void itShouldBePunctured() {
assertEquals("[SIKERES] A(z) " + activePlayer + " jatekos kiszurta a komponenst, amin all", testResult);
}

@Then("the pipe shouldn't get punctured")
public void thePipeShouldnTGetPunctured() {
assertEquals("[HIBA] A jatekosnak nincs eleg AP-je a muvelet elvegzesehez!", testResult);
}

@Given("that a pipe is punctured")
public void thatAPipeIsPunctured() {
activeComponent = "pi1";
cmd.NewCommand("ChangeBreakState:"+activeComponent+",broken");
}

@Then("the pipe shouldn't be punctured again")
public void thePipeShouldnTBePuncturedAgain() {
assertEquals("[HIBA] A komponenst nem lehet kiszurni (vagy mar ki van szurva)!", testResult);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/mechanic_fixes_pipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Feature: Mechanic standing on a punctured pipe attempts to fix it.
And I am a Mechanic
And I am standing on the pipe
And I don't have enough AP
When I try to fix the pipe
When I fix the pipe
Then the pipe shouldn't be fixed
16 changes: 8 additions & 8 deletions src/test/resources/player_moves_from_pipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ Feature: Player attempts to move from a pipe to one of its neighbouring componen
As a Player, I want to be able to move from pipes to one of their neighbouring components.

Scenario: A non-stuck Player standing on a pipe with enough AP moves to a valid neighbouring component.
Given that I am standing on a pipe
Given that I intend to move to an existing neighbour
And I am standing on the pipe
And I am not stuck
And I have enough AP
And I intend to move to an existing neighbour
When I attempt to move to the selected component
Then the movement should be successful

Scenario: A stuck Player standing on a pipe attempts to move to a valid neighbouring component.
Given that I am standing on a pipe
Given that I intend to move to an existing neighbour
And I am standing on the pipe
And I am stuck
And I intend to move to an existing neighbour
When I attempt to move to the selected component
Then the movement should be unsuccessful

Scenario: A non-stuck Player standing on a pipe without enough AP attempts to move to a valid neighbouring component.
Given that I am standing on a pipe
Given that I intend to move to an existing neighbour
And I am standing on the pipe
And I am not stuck
And I don't have enough AP
And I intend to move to an existing neighbour
When I attempt to move to the selected component
Then the movement should be unsuccessful

Scenario: A Player standing on a pipe attempts to move to a non-existent neighbouring component.
Given that I am standing on a pipe
And I intend to move to a non-existing neighbour
Given that I intend to move to a non-existing neighbour
And I am standing on the pipe
When I attempt to move to the selected component
Then the movement should be unsuccessful
4 changes: 2 additions & 2 deletions src/test/resources/player_punctures_pipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Feature: Player standing on a pipe attempts to puncture it.
Given that a pipe is working
And I am standing on the pipe
And I don't have enough AP
When I attempt to puncture the pipe
When I puncture the pipe
Then the pipe shouldn't get punctured

Scenario: A Player without enough AP attempts to puncture a broken pipe while standing on it.
Given that a pipe is punctured
And I am standing on the pipe
And I have enough AP
When I attempt to puncture the pipe
When I puncture the pipe
Then the pipe shouldn't be punctured again
Loading

0 comments on commit 3ee7db0

Please sign in to comment.