Skip to content

Commit

Permalink
Function Call Test and Pump fix
Browse files Browse the repository at this point in the history
Waterflow function test by invocation count for different elements of the map.
Fix for Pump step, now it correctly calls the step of the neighbour pipes.
  • Loading branch information
sziki12 committed May 21, 2024
1 parent 7c2b205 commit 9dfcecb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/Pump.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public void step(){
containingWater=true;
}
}
//Release water form leaking pipe neighbors
for(Element neighbor : neighbors)
{
if(outputPipe!=neighbor)
neighbor.step();
}
for(Element neighbor : inputPipe.getNeighbors())
{
if(this!=neighbor)
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/WaterflowFunctionCallTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class WaterflowFunctionCallTest extends MapTestBase{

@Override
@BeforeEach
public void MapInit() {
c = Mockito.spy(new Cistern());
c.setName("Spy c");

w = Mockito.spy(new WaterSource());
w.setName("Spy w");

p1 = Mockito.spy(new Pump());
p1.setName("Spy p1");

p2 = Mockito.spy(new Pump());
p2.setName("Spy p2");

pi1 = Mockito.spy(new Pipe());
pi1.setName("Spy pi1");
pi2 = Mockito.spy(new Pipe());
pi2.setName("Spy pi2");
pi3 = Mockito.spy(new Pipe());
pi3.setName("Spy pi3");
pi4 = Mockito.spy(new Pipe());
pi4.setName("Spy pi4");

addNeighbor(w, pi1);
addNeighbor(pi1, p1);
addNeighbor(p1, pi2);
p1.adjust(0,1);
addNeighbor(pi2, c);

addNeighbor(w, pi3);
addNeighbor(pi3, p2);
addNeighbor(p2, pi4);
p2.adjust(0,1);
addNeighbor(pi4, c);

pi4.damage();

elements = new ArrayList<>(List.of(w, c, p1, p2, pi1, pi2, pi3, pi4));
saboteurPointSources= new ArrayList<>(List.of( p1, p2, pi1, pi2, pi3, pi4));
pumps = new ArrayList<>(List.of( p1, p2));

game.load(elements,saboteurPointSources,new ArrayList<>(List.of(c)),new ArrayList<>(),new ArrayList<>(),0,0,1,2,2,pumps);
}

@Test
@DisplayName("Water Flow Function Call Test")
public void WaterflowInvocationTest()
{
SimulateWaterFlowMock();
//The Cistern is the beginning of water simulation
verify(c,times(1)).step();

//WaterSource called 2 times, because 2 path of water can reach it
verify(w,times(2)).step();

verify(p1,times(1)).step();
verify(p2,times(1)).step();

verify(pi1,times(1)).step();
verify(pi2,times(1)).step();
verify(pi3,times(1)).step();
//Damaging the pipe also calls step, beside the water flow simulation
verify(pi4,times(2)).step();

}
}

0 comments on commit 9dfcecb

Please sign in to comment.