Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from NaulaN/JUnit
Browse files Browse the repository at this point in the history
[Test] Je souhaite faire un jeux de test au Joueur afin de garantir le bon fonctionnement du jeu.
  • Loading branch information
naulan-chrzaszcz authored Jun 12, 2022
2 parents 07dd7d7 + 11788df commit c5314b2
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 26 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ javafx {
}

dependencies {
implementation 'junit:junit:4.13.1'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
implementation 'junit:junit:4.13.2'

testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")

implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ public Meat(final Environment environment)
{
Player player = environment.getPlayer();

if (player.getPv() < player.getPvMax()) {
Inventory inventory = player.getInventory();

if (player.getPv() < player.getPvMax())
player.setPv(player.getPv() + 1);
inventory.get().get(inventory.getPosCursor()).remove();
}
}
}
13 changes: 9 additions & 4 deletions src/main/java/fr/sae/terraria/modele/entities/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,21 @@ public Player(final Environment environment)
}
}

@Override public void hit() { }
@Override public void hit()
{
this.setPv(this.getPv() - 1);
}

@Override public void spawn(int x, int y)
{
this.setX(x);
this.setY(y);

Image image = View.loadAnImage("sprites/player/player_idle.png", environment.scaleMultiplicatorWidth, environment.scaleMultiplicatorHeight);
this.setRect((int) image.getWidth(), (int) image.getHeight());
image.cancel();
Image img = View.loadAnImage("sprites/player/player_idle.png", environment.scaleMultiplicatorWidth, environment.scaleMultiplicatorHeight);
if (!Objects.isNull(img)) {
this.setRect((int) img.getWidth(), (int) img.getHeight());
img.cancel();
} else this.setRect(10, 10);

this.getGravity().setXInit(x);
this.getGravity().setYInit(y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ public void put(StowableObjectType item)
if (nbStacksInventory == 0) {
Stack stack = new Stack();
stack.nbItemsProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.intValue() < oldValue.intValue()) {
Inventory inventory = player.getInventory();
inventory.get().get(inventory.getPosCursor()).remove();
}

if (newValue.intValue() <= 0) {
this.value.remove(stack);
this.player.setStackSelected(null);
}
});
stack.setItem(item);
stack.add();
this.value.add(stack);
this.player.setStackSelected(stack);
} else {
Expand All @@ -93,7 +97,6 @@ public void put(StowableObjectType item)
}
});
stack.setItem(item);
stack.add();
this.value.add(stack);
this.player.setStackSelected(stack);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Stack()
{
super();

this.nbItems = new SimpleIntegerProperty(0);
this.nbItems = new SimpleIntegerProperty(1);
}

public boolean isSameItem(StowableObjectType object)
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/fr/sae/terraria/vue/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ private static Image foundImage(final String path)
public static Image loadAnImage(final String path, double scaleMultiplicatorWidth, double scaleMultiplicatorHeight)
{
Image img = View.foundImage(path);
double width = img.getWidth();
double height = img.getHeight();
img.cancel();

double widthScaled = width*scaleMultiplicatorWidth;
double heightScaled = height*scaleMultiplicatorHeight;
return new Image(img.getUrl(), widthScaled, heightScaled, false, false, false);
if (!Objects.isNull(img)) {
double width = img.getWidth();
double height = img.getHeight();
img.cancel();

double widthScaled = width*scaleMultiplicatorWidth;
double heightScaled = height*scaleMultiplicatorHeight;
return new Image(img.getUrl(), widthScaled, heightScaled, false, false, false);
}
return null;
}

public static ImageView createImageView(final Entity entity, final Image img)
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
requires com.google.gson;
requires java.desktop;


opens fr.sae.terraria to javafx.fxml;
exports fr.sae.terraria;
exports fr.sae.terraria.controller;
opens fr.sae.terraria.controller to javafx.fxml;
exports fr.sae.terraria.modele;
opens fr.sae.terraria.modele to javafx.fxml;
Expand Down
212 changes: 212 additions & 0 deletions src/test/java/fr/sae/terraria/modele/entities/player/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package fr.sae.terraria.modele.entities.player;

import fr.sae.terraria.modele.Environment;
import fr.sae.terraria.modele.TileMaps;
import fr.sae.terraria.modele.entities.Rabbit;
import fr.sae.terraria.modele.entities.blocks.Dirt;
import fr.sae.terraria.modele.entities.items.Coal;
import fr.sae.terraria.modele.entities.items.Meat;
import fr.sae.terraria.modele.entities.items.Vodka;
import javafx.geometry.Rectangle2D;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;

import java.awt.*;

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


public class PlayerTest
{
private static Environment environment;
private static Player player;


public PlayerTest() { super(); }

@BeforeAll public static void init()
{
environment = new Environment(1., 1.);
player = environment.getPlayer();
}

@Test public final void moveTest()
{
double previousPosX;

previousPosX = player.getX();
player.moveRight();
player.move();

assertEquals(previousPosX + player.getVelocity(), player.getX(),
"Test: déplacement vers la droite.");
player.idleOnX();


previousPosX = player.getX();
player.moveLeft();
player.move();

assertEquals(previousPosX - player.getVelocity(), player.getX(),
"Test: déplacement vers la gauche");
}


@Test public final void collideTest()
{
/*
// Le TOP et le BOTTOM est un pet complex à tester dans un JUnit
Dirt dirtLeft = new Dirt(environment, 1, 1);
dirtLeft.setRect(1, 1);
environment.getEntities().add(dirtLeft);
Dirt dirtRight = new Dirt(environment, 5, 1);
dirtRight.setRect(1, 1);
environment.getEntities().add(dirtRight);
player.setRect(1, 1);
player.setVelocity(0);
player.setY(1);
player.setX(3);
player.moveLeft();
player.collide();
assertTrue(player.isMovingLeft());
player.setX(1);
player.moveLeft();
player.collide();
assertTrue(player.isIDLEonX());
player.setX(5);
player.moveRight();
player.collide();
assertTrue(player.isIDLEonX());
*/
}

@Test public final void hitTest()
{
double previousPv = player.getPv();
player.hit();

assertEquals(previousPv - 1, player.getPv(),
"Vérifie s'il y a bien subit un dégât");
}

@Test public final void spawnTest()
{
int spawnLocX = 100;
int spawnLocY = 100;

player.spawn(spawnLocX, spawnLocY);

assertEquals(player.getX(), spawnLocX,
"Regarde si la localisation en X est correcte");
assertEquals(player.getY(), spawnLocY,
"Regarde si la localisation en X est correcte");
assertEquals(player.getGravity().xInit, spawnLocX,
"Regarde si la localisation au niveau de la gestion de la gravité, en X est correcte");
assertEquals(player.getGravity().yInit, spawnLocY,
"Regarde si la localisation au niveau de la gestion de la gravité, en X est correcte");
}

@Test public final void worldLimitTest()
{
player.setX(2*TileMaps.TILE_DEFAULT_SIZE);
player.moveLeft();
player.worldLimit();

assertTrue(player.isMovingLeft());

player.setX(-TileMaps.TILE_DEFAULT_SIZE);
player.moveLeft();
player.worldLimit();

assertTrue(player.isIDLEonX());

player.setX(environment.getTileMaps().getWidth()*TileMaps.TILE_DEFAULT_SIZE + TileMaps.TILE_DEFAULT_SIZE);
player.moveRight();
player.worldLimit();

assertTrue(player.isIDLEonX());
}

@Test public final void interactWithBlockTest() // TODO: Le nom est probablement mal choisie dans player
{
Rectangle2D blockSelected;
double beforePv;

Dirt dirt = new Dirt(environment, 1, 1);
dirt.setRect(1, 1);
environment.getEntities().add(dirt);
blockSelected = new Rectangle2D(1, 1, 1, 1);

beforePv = dirt.getPv();
player.interactWithBlock(blockSelected);
assertEquals(beforePv - 1, dirt.getPv());
environment.getEntities().remove(dirt);


Rabbit rabbit = new Rabbit(environment, 10, 10);
rabbit.setRect(1, 1);
environment.getEntities().add(rabbit);
blockSelected = new Rectangle2D(10, 10, 1, 1);

beforePv = rabbit.getPv();
player.interactWithBlock(blockSelected);
assertEquals(beforePv - 1, rabbit.getPv());
}

@Test public final void placeBlockTest()
{
TileMaps tileMaps = environment.getTileMaps();
player.pickup(new Dirt(environment, 1, 1));

assertEquals(tileMaps.getTile(0, 0), TileMaps.SKY);
player.placeBlock(0, 0);
assertEquals(tileMaps.getTile(0, 0), TileMaps.DIRT);

assertNull(player.getStackSelected());
}

@Test public final void drunkTest()
{
player.pickup(new Vodka(environment));

assertFalse(player.drunkProperty().get(),
"Vérifie si il n'est pas bourré");
((Vodka) player.getStackSelected().getItem()).consumes();
assertTrue(player.drunkProperty().get(),
"Verife si il est bourré");
}

@Test public final void pickupTest()
{
player.getInventory().posCursorProperty().set(0);


// Test avec de la viande
player.pickup(new Meat(environment));
assertTrue(player.getStackSelected().getItem() instanceof Meat,
"Verifie si le stack est bien de la viande");
assertEquals(player.getStackSelected().getNbItems(), 1,
"Verifie s'il y a bien 1 objet dans le stack");
((Meat) player.getStackSelected().getItem()).consumes();
player.getStackSelected().remove();

assertNull(player.getStackSelected());


// Test avec du charbon
player.pickup(new Coal());
assertTrue(player.getStackSelected().getItem() instanceof Coal,
"Verifie si le stack est bien du charbon");
assertEquals(player.getStackSelected().getNbItems(), 1,
"Verifie s'il y a bien 1 objet dans le stack");
player.getStackSelected().remove();

assertNull(player.getStackSelected());
}
}

0 comments on commit c5314b2

Please sign in to comment.