Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tesztek készítése #14

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class Controller {
* Getter for test
* */
public static boolean isTest() {return test;}

public static void setTest(boolean value){ test = value;}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


/**
* Name of the used file
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public void reset(){
* @return mechanic - Points of the mechanics.
*/
public int getMechanic() { return mechanic; }

public ArrayList<Cistern> getCisterns() { return cisterns; }

public ArrayList<Pipe> getPipes() { return pipes; }

public boolean getEnd() { return end; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


/**
*Adds new Cistern to cisterns.
* @param c Cistern that is to be added to cisterns.
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package Controll;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import Fields.Pipe;
import Fields.ActiveFields.Cistern;

class WaterCounterTest {
private WaterCounter waterCounter;
private Cistern cistern;
private Pipe pipe;

@BeforeEach
void setUp() throws Exception {
waterCounter = new WaterCounter();
cistern = new Cistern();
pipe = new Pipe(10);

waterCounter.addPipe(pipe);
waterCounter.addCistern(cistern);
}

@Test
void testReset() {
waterCounter.reset();

assertEquals(0, waterCounter.getSaboteur());
assertEquals(0, waterCounter.getMechanic());
assertFalse(waterCounter.getEnd());
assertTrue(waterCounter.getCisterns().isEmpty());
assertTrue(waterCounter.getPipes().isEmpty());
}

@Test
void testAddCistern() {
assertTrue(waterCounter.getCisterns().contains(cistern));
}

@Test
void testAddPipe() {
assertTrue(waterCounter.getPipes().contains(pipe));
}

@Test
void testSetEnd() {
waterCounter.setEnd();

assertTrue(waterCounter.getEnd());
}

@Test
void testCount_End() {
pipe.fillInWater(-6);
cistern.setWater(10);
waterCounter.setEnd();

waterCounter.count();

assertEquals(0, waterCounter.getSaboteur());
assertEquals(10, waterCounter.getMechanic());
}

@Test
void testCount_No_End() {
pipe.fillInWater(6);
cistern.setWater(10);

waterCounter.count();

assertEquals(6, waterCounter.getSaboteur());
assertEquals(0, waterCounter.getMechanic());

waterCounter.setEnd();

waterCounter.count();
assertEquals(10, waterCounter.getMechanic());
}

@Test
void testToString() {
waterCounter.count();

String exp = "saboteur: 0\nmechanic: 0\n";
assertEquals(exp, waterCounter.toString());
}

@Test
void testWinner_Draw() {
waterCounter.setEnd();
waterCounter.count();

assertEquals("Döntetlen", waterCounter.winner());
}

@Test
void testWinner_Mechanic() {
pipe.fillInWater(6);
cistern.setWater(10);

waterCounter.setEnd();
waterCounter.count();

assertEquals("Mechanic", waterCounter.winner());
}

@Test
void testWinner_Mechanic_Saboteur() {
pipe.fillInWater(10);
cistern.setWater(6);

waterCounter.setEnd();
waterCounter.count();

assertEquals("Saboteur", waterCounter.winner());
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package Fields.ActiveFields;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import Controll.Controller;
import Fields.Pipe;

class CisternTest {
private Cistern cistern;
private Pipe pipe;
private Pump pump;

@BeforeEach
void setUp() throws Exception {
cistern = new Cistern();
pipe = new Pipe(10);
pump = new Pump(20);
}

@Test
void testCreateNewPump_Success() {
pump = cistern.createNewPump(true);

assertNotNull(pump);
}

@Test
void testCreateNewPump_No_Success() {
pump = cistern.createNewPump(false);

assertNull(pump);
}

@Test
void testGetWater() {
cistern.setWater(10);
int water = cistern.getWater();

assertEquals(10, water);
}

@Test
void testPickUpPipe() {
cistern.step();
pipe = cistern.pickUpPipe();

assertNotNull(pipe);
assertEquals(1, cistern.getPipes().size());
assertTrue(cistern.getPipes().contains(pipe));
}

@Test
void testStep() {
cistern.addPipe(pipe);
cistern.step();

assertEquals(0, cistern.getWater());
assertNotNull(cistern.pickUpPipe());
assertNotNull(cistern.getPipes());
}

@Test
void testToString() {
cistern.setBroken(true);
cistern.setWater(13);
cistern.addPipe(pipe);
String cisternString = cistern.toString();

assertTrue(cisternString.contains("occupied: false"));
assertTrue(cisternString.contains("water: 13"));
assertTrue(cisternString.contains("broken: true"));
assertTrue(cisternString.contains("players: null"));
assertTrue(cisternString.contains("pipes: null"));
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Fields.ActiveFields;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import Fields.Pipe;

class PumpTest {
private Pump pump;
private ArrayList<Pipe> pipes;

@BeforeEach
void setUp() throws Exception {
pump = new Pump(100);
pipes = new ArrayList<>();
pipes.add(new Pipe(50));
pipes.add(new Pipe(50));
pump.setPipes(pipes);
}

@Test
void testSet() {
Pipe inputPipe = pipes.get(0);
Pipe outputPipe = pipes.get(1);

boolean result = pump.set(inputPipe, outputPipe);
assertTrue(result);
assertEquals(0, pump.getWaterFrom());
assertEquals(1, pump.getWaterTo());
}

@Test
void testRepair() {
boolean repairResult = pump.repair();

assertFalse(pump.isBroken());
assertTrue(repairResult);
}

@Test
void testStep() {
pump.setWaterFrom(0);
pump.setWaterTo(1);
pump.step();

assertEquals(0,pump.getWater());
assertEquals(0,pipes.get(pump.getWaterTo()).getWater());
}

@Test
void testToString() {
pump.setOccupied(true);
pump.setWater(10);
pump.setBroken(true);
pump.setWaterFrom(0);
pump.setWaterTo(1);
String pumpString = pump.toString();

assertTrue(pumpString.contains("occupied: true"));
assertTrue(pumpString.contains("water: 10"));
assertTrue(pumpString.contains("broken: true"));
assertTrue(pumpString.contains("players: null"));
assertTrue(pumpString.contains("pipes:"));
assertTrue(pumpString.contains("tank: 100"));
assertTrue(pumpString.contains("waterFrom: null"));
assertTrue(pumpString.contains("waterTo: null"));
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Fields.ActiveFields;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import Fields.Pipe;
import Players.Mechanic;

class SpringTest {
private Spring spring;
private ArrayList<Pipe> pipes;
private Mechanic mechanic;

@BeforeEach
void setUp() throws Exception {
spring = new Spring(10);
mechanic = new Mechanic();
pipes = new ArrayList<>();
pipes.add(new Pipe(30));
pipes.add(new Pipe(30));
spring.setPipes(pipes);
}

@Test
void testStep() {
pipes.get(0).setWater(5);
pipes.get(1).setWater(10);

spring.step();

assertEquals(0, spring.getWaterOut());
assertEquals(10, spring.getMaxOutWater());
assertEquals(-10, pipes.get(0).getWater());
}

@Test
void testToString() {
pipes.get(0).setWater(20);
pipes.get(1).setWater(10);

String springString = spring.toString();

assertTrue(springString.contains("occupied: false"));
assertTrue(springString.contains("water: 0"));
assertTrue(springString.contains("broken: false"));
assertTrue(springString.contains("players: null"));
assertTrue(springString.contains("pipes: null"));
assertTrue(springString.contains("waterOut: 10"));
assertTrue(springString.contains("maxOutWater: 10"));
}

@Test
void testGetNeighborFields() {
assertEquals(pipes,spring.getNeighborFields());
}

@Test
void testRemovePlayer() {
mechanic.setStandingField(spring);
spring.setOccupied(true);
spring.setPlayers(mechanic);
spring.accept(mechanic);

boolean removed = spring.removePlayer(mechanic);

assertTrue(removed);
assertFalse(spring.isOccupied());
}
}
Loading
Loading