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 test #25

Merged
merged 3 commits into from
May 22, 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
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ sourceSets {
srcDirs = ['src']
}
}
test {
java {
srcDirs = ['test']
}
}
}

mainClassName = "macaroni.Main"

dependencies {
testImplementation 'junit:junit:4.13.1'
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'org.junit.jupiter:junit-jupiter-api:5.5.1'
}
53 changes: 53 additions & 0 deletions src/macaroni/test/PlumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package macaroni.test;

import macaroni.model.character.Plumber;
import macaroni.model.element.Cistern;
import macaroni.model.element.Pipe;
import macaroni.model.misc.WaterCollector;
import macaroni.utils.ModelObjectFactory;
import org.junit.jupiter.api.Test;

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

class PlumberTest {
@Test
void repair(){
Pipe p1 = new Pipe(new WaterCollector());
p1.pierce();
assertTrue(p1.isPierced());
Plumber plu = new Plumber(p1);
plu.repair(p1);
assertFalse(p1.isPierced());
}

@Test
void pickUpPump(){
ModelObjectFactory.setCisternCreatePumpName("Pump");
Cistern c1 = new Cistern(new WaterCollector());
Plumber plu = new Plumber(c1);
plu.pickUpPump(c1);
assertEquals(plu.getHeldPumpCount(), 1);
for(int i=0; i<3; i++){
ModelObjectFactory.setCisternCreatePumpName("Pump"+i);
plu.pickUpPump(c1);
}
assertEquals(plu.getHeldPumpCount(), 4);
}

@Test
void placePump(){
ModelObjectFactory.setCisternCreatePumpName("Pump");
Cistern c1 = new Cistern(new WaterCollector());
Plumber plu = new Plumber(c1);
plu.pickUpPump(c1);
assertEquals(plu.getHeldPumpCount(), 1);
Pipe p1 = new Pipe(new WaterCollector());
Cistern c2 = new Cistern(new WaterCollector());
p1.addEndpoint(c1);
p1.addEndpoint(c2);
plu.moveTo(p1);
ModelObjectFactory.setPipeCreatePipeName("Pipe");
plu.placePump(p1);
assertEquals(plu.getHeldPumpCount(), 0);
}
}
50 changes: 50 additions & 0 deletions src/macaroni/test/SaboteurTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package macaroni.test;

import macaroni.model.character.Saboteur;
import macaroni.model.effects.BananaEffect;
import macaroni.model.effects.TechnokolEffect;
import macaroni.model.element.Pipe;
import macaroni.model.element.Pump;
import macaroni.model.misc.WaterCollector;
import org.junit.jupiter.api.Test;

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

class SaboteurTest {
@Test
void dropBanana(){
Pipe p1 = new Pipe(new WaterCollector());
Saboteur s1 = new Saboteur(p1);
s1.dropBanana(p1);
assertEquals(p1.getEffect().getClass(), BananaEffect.class);
}

@Test
void applyTechnokol(){
Pipe p1 = new Pipe(new WaterCollector());
Saboteur s1 = new Saboteur(p1);
s1.applyTechnokol(p1);
assertEquals(p1.getEffect().getClass(), TechnokolEffect.class);
}

@Test
void getLocation(){
Pipe p1 = new Pipe(new WaterCollector());
Saboteur s1 = new Saboteur(p1);
assertEquals(s1.getLocation(), p1);
}

@Test
void setPipeIO(){
Pump pump = new Pump();
Saboteur s1 = new Saboteur(pump);
Pipe p1 = new Pipe(new WaterCollector());
pump.addPipe(p1);
s1.setInputPipe(pump, p1);
assertEquals(pump.getInputPipe(), p1);
Pipe p2 = new Pipe(new WaterCollector());
pump.addPipe(p2);
s1.setOutputPipe(pump, p2);
assertEquals(pump.getOutputPipe(), p2);
}
}
30 changes: 30 additions & 0 deletions src/macaroni/test/SpringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package macaroni.test;

import macaroni.model.element.Pipe;
import macaroni.model.element.Pump;
import macaroni.model.element.Spring;
import macaroni.model.misc.WaterCollector;
import org.junit.jupiter.api.Test;

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

class SpringTest {
@Test
void tick(){
Spring s1 = new Spring();
Pump secondEndPoint = new Pump();
Pipe p1 = new Pipe(new WaterCollector(), 10);
s1.addPipe(p1);
secondEndPoint.addPipe(p1);
s1.tick();
assertEquals(p1.getStoredWater(), 4);

Spring s2 = new Spring(2);
Pipe p2 = new Pipe(new WaterCollector(), 10);
s2.addPipe(p2);
secondEndPoint.addPipe(p2);
s2.tick();
assertEquals(p2.getStoredWater(), 2);
}

}
60 changes: 60 additions & 0 deletions src/macaroni/test/Vector2DTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package macaroni.test;

import macaroni.math.Vector2D;
import macaroni.views.Position;
import org.junit.jupiter.api.*;

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

class Vector2DTest {

@Test
void add(){
Vector2D v1 = new Vector2D(1.0, 2.0);
Vector2D v2 = new Vector2D(1.0, 2.0);
v1 = v1.add(v2);
assertEquals(v1.getX(), 2.0);
assertEquals(v1.getY(), 4.0);
}

@Test
void dot(){
Vector2D v1 = new Vector2D(1.0, 2.0);
Vector2D v2 = new Vector2D(1.0, 2.0);
double dotProduct = v1.dot(v2);
assertEquals(dotProduct, 5.0);
}

@Test
void length() {
Vector2D v1 = new Vector2D(1.0, 2.0);
Vector2D v2 = new Vector2D(1.0, 2.0);
assertEquals(v2.length(), v1.length());
Vector2D v3 = new Vector2D(3.0, 4.0);
assertEquals(v3.length(), 5.0);
}

@Test
void scale(){
Vector2D v1 = new Vector2D(1.0, 2.0);
Vector2D v2 = v1.scale(3.0);
assertEquals(v2.getX(), 3.0);
assertEquals(v2.getY(), 6.0);
}

@Test
void normalize(){
Vector2D v3 = new Vector2D(3.0, 4.0);
v3 = v3.normalize();
assertEquals(v3.getX(), 3.0/5.0, 0.0001);
assertEquals(v3.getY(), 4.0/5.0, 0.0001);
}

@Test
void toPosition(){
Vector2D v3 = new Vector2D(3.6, 4.2);
Position p1 = v3.toPosition();
assertEquals(p1.x(), 3);
assertEquals(p1.y(), 4);
}
}
Loading