test: started writing GameLogic test, and added providers for several types

This commit is contained in:
Yannik Bretschneider 2021-05-28 17:25:49 +02:00
parent a25231fb3e
commit 42e57daa54

View File

@ -1,12 +1,17 @@
package uulm.teamname.marvelous.gamelibrary.gamelogic;
import net.jqwik.api.*;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
@ -39,4 +44,151 @@ class GameLogicTest {
@Test
void checkWinConditions() {
}
// @Provide("gamestate")
// Arbitrary<GameState> gamestate() {
// var states = Arbitraries.integers()
// .tuple2()
// .map(x -> new IntVector2(x.get1(), x.get2()))
// .map(GameState::new);
// }
// @Provide("entityManager")
// Arbitrary<EntityManager> entityManager() {
// var managers = Arbitraries.of(EntityManager.class);
//
// }
// @Provide("entity")
// Arbitrary<Entity> entity() {
// var entities = Arbitraries.of(Entity.class);
// }
// Characters are kind of hard, but most prequisites are here
// @Provide("character")
// Arbitrary<Character> character() {
// var characters = Combinators.combine(
// anyStat(),
// Arbitraries.integers()
// ).as(x -> new Character());
// }
@Provide("stat")
Arbitrary<Stat> anyStat() {
var stats = Combinators.combine(
Arbitraries.of(StatType.class),
Arbitraries.integers())
.as(Stat::new);
return Combinators.combine(stats, Arbitraries.integers().greaterOrEqual(0))
.as((stat, decrease) -> {
stat.decreaseValue(decrease % (stat.max + 1));
return stat;
});
}
@Provide("entityIDs")
Arbitrary<EntityID> entityIDs() {
return Combinators.combine(
Arbitraries.of(EntityType.class),
Arbitraries.integers()
).as((type, id) -> {
EntityID eid;
switch (type) {
case NPC -> eid = new EntityID(type, id % 3);
case P1, P2, InfinityStones -> eid = new EntityID(type, id % 6);
case Rocks -> eid = new EntityID(type, id);
default -> throw new IllegalStateException("Unexpected value: " + type);
}
return eid;
});
}
@Provide("positions")
Arbitrary<IntVector2> positions() {
return Arbitraries.integers()
.greaterOrEqual(0)
.tuple2()
.map(x -> new IntVector2(x.get1(), x.get2()));
}
@Provide("requests")
Arbitrary<Request> requests() {
return Combinators.combine(
Arbitraries.of(true, false),
gameRequests(),
characterRequests())
.as((which, gameRequest, characterRequest) -> which ? gameRequest : characterRequest);
}
@Provide("gameRequests")
Arbitrary<GameRequest> gameRequests() {
final var gameRequestTypes = List.of(
RequestType.PauseStopRequest,
RequestType.PauseStartRequest,
RequestType.EndRoundRequest,
RequestType.DisconnectRequest,
RequestType.Req);
return Arbitraries.of(RequestType.class)
.filter(gameRequestTypes::contains)
.map(x -> new RequestBuilder(x).buildGameRequest());
}
@Provide("characterRequests")
Arbitrary<CharacterRequest> characterRequests() {
final var characterRequestTypes = List.of(
RequestType.MeleeAttackRequest,
RequestType.RangedAttackRequest,
RequestType.MoveRequest,
RequestType.ExchangeInfinityStoneRequest,
RequestType.UseInfinityStoneRequest);
var positions = Arbitraries.integers()
.greaterOrEqual(0)
.tuple2()
.map(x -> new IntVector2(x.get1(), x.get2()));
var characterIDs = Combinators.combine(Arbitraries.of(true, false)
.map(x -> x ? EntityType.P1 : EntityType.P2),
Arbitraries.integers()
.between(0, 5))
.as(EntityID::new);
var rockIDs = Arbitraries.integers()
.greaterOrEqual(0)
.map(x -> new EntityID(EntityType.Rocks, x));
var targetIDs = Combinators.combine(
Arbitraries.of(true, false),
characterIDs,
rockIDs)
.as((which, characterID, rockID) -> which ? characterID : rockID);
var stoneTypes = Arbitraries.of(StoneType.class);
var values = Arbitraries.integers();
return Combinators.combine(
Arbitraries.of(RequestType.class).filter(characterRequestTypes::contains), // types
positions.tuple2(),
characterIDs,
targetIDs,
stoneTypes,
values
).as((type, pos, characterID, targetID, stoneType, val) -> {
return new RequestBuilder(type)
.withOriginField(pos.get1())
.withTargetField(pos.get2())
.withOriginEntity(characterID)
.withTargetEntity(targetID)
.withStoneType(stoneType)
.withValue(val)
.buildCharacterRequest();
});
}
}