From 59e7dca4a4158b405209a14a9f1466b23f8eebf6 Mon Sep 17 00:00:00 2001 From: Manuel Andruccioli Date: Mon, 9 Oct 2023 22:08:53 +0200 Subject: [PATCH] refactor(road-build): add possibility to skip check in road build to avoid heavy construction during tests --- .../model/game/BaseScatanStateTest.scala | 13 +++++ .../scatan/model/game/ops/AwardOpsTest.scala | 52 +++++++++---------- .../model/game/ops/BuildingOpsTest.scala | 6 +-- .../scatan/model/game/ops/ScoreOpsTest.scala | 12 ++--- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/test/scala/scatan/model/game/BaseScatanStateTest.scala b/src/test/scala/scatan/model/game/BaseScatanStateTest.scala index adfb156d..abc0d30e 100644 --- a/src/test/scala/scatan/model/game/BaseScatanStateTest.scala +++ b/src/test/scala/scatan/model/game/BaseScatanStateTest.scala @@ -4,12 +4,25 @@ import scatan.BaseTest import scatan.model.game.config.ScatanPlayer import scatan.model.game.ops.EmptySpotsOps.emptySpots import scatan.model.map.Spot +import scatan.model.map.RoadSpot +import scatan.model.game.ops.EmptySpotsOps.emptyRoadSpot +import scatan.model.game.ops.BuildingOps.assignBuilding +import scatan.model.components.BuildingType abstract class BaseScatanStateTest extends BaseTest: protected def players(n: Int): Seq[ScatanPlayer] = (1 to n).map(i => ScatanPlayer(s"Player $i")) + protected def noConstrainToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = _ => (_, _) => true + protected def spotShouldBeEmptyToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = s => + (r, _) => s.emptyRoadSpot.contains(r) + + // Avoid heavy check on spot type + extension (state: ScatanState) + def assignRoadWithoutRule(spot: RoadSpot, player: ScatanPlayer): Option[ScatanState] = + state.assignBuilding(spot, BuildingType.Road, player, noConstrainToBuildRoad) + protected def emptySpot(state: ScatanState): Spot = state.emptySpots.head val threePlayers = players(3) diff --git a/src/test/scala/scatan/model/game/ops/AwardOpsTest.scala b/src/test/scala/scatan/model/game/ops/AwardOpsTest.scala index f37bd2ae..9a0f5ea3 100644 --- a/src/test/scala/scatan/model/game/ops/AwardOpsTest.scala +++ b/src/test/scala/scatan/model/game/ops/AwardOpsTest.scala @@ -21,11 +21,11 @@ class AwardOpsTest extends BaseScatanStateTest: val player1 = threePlayers.head val it = state.emptyRoadSpot.iterator val stateWithAward = for - stateWithOneRoad <- state.assignBuilding(it.next, BuildingType.Road, player1) - stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player1) + stateWithOneRoad <- state.assignRoadWithoutRule(it.next, player1) + stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player1) + stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player1) + stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player1) + stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player1) yield stateWithFiveRoad stateWithAward match case Some(state) => state.awards(Award(AwardType.LongestRoad)) should be(Some((player1, 5))) @@ -52,18 +52,18 @@ class AwardOpsTest extends BaseScatanStateTest: val player2 = threePlayers.tail.head val it = state.emptyRoadSpot.iterator val firstStateWithAward = for - stateWithOneRoad <- state.assignBuilding(it.next, BuildingType.Road, player1) - stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player1) + stateWithOneRoad <- state.assignRoadWithoutRule(it.next, player1) + stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player1) + stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player1) + stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player1) + stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player1) yield stateWithFiveRoad val secondStateWithAward = for - stateWithOneRoad <- firstStateWithAward.get.assignBuilding(it.next, BuildingType.Road, player2) - stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player2) + stateWithOneRoad <- firstStateWithAward.get.assignRoadWithoutRule(it.next, player2) + stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player2) + stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player2) + stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player2) + stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player2) yield stateWithFiveRoad firstStateWithAward match case Some(state) => state.awards(Award(AwardType.LongestRoad)) should be(Some((player1, 5))) @@ -80,19 +80,19 @@ class AwardOpsTest extends BaseScatanStateTest: val player2 = threePlayers.tail.head val it = state.emptyRoadSpot.iterator val firstStateWithAward = for - stateWithOneRoad <- state.assignBuilding(it.next, BuildingType.Road, player1) - stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player1) - stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player1) + stateWithOneRoad <- state.assignRoadWithoutRule(it.next, player1) + stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player1) + stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player1) + stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player1) + stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player1) yield stateWithFiveRoad val secondStateWithAward = for - stateWithOneRoad <- firstStateWithAward.get.assignBuilding(it.next, BuildingType.Road, player2) - stateWithTwoRoad <- stateWithOneRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithThreeRoad <- stateWithTwoRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithFourRoad <- stateWithThreeRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithFiveRoad <- stateWithFourRoad.assignBuilding(it.next, BuildingType.Road, player2) - stateWithSixRoad <- stateWithFiveRoad.assignBuilding(it.next, BuildingType.Road, player2) + stateWithOneRoad <- firstStateWithAward.get.assignRoadWithoutRule(it.next, player2) + stateWithTwoRoad <- stateWithOneRoad.assignRoadWithoutRule(it.next, player2) + stateWithThreeRoad <- stateWithTwoRoad.assignRoadWithoutRule(it.next, player2) + stateWithFourRoad <- stateWithThreeRoad.assignRoadWithoutRule(it.next, player2) + stateWithFiveRoad <- stateWithFourRoad.assignRoadWithoutRule(it.next, player2) + stateWithSixRoad <- stateWithFiveRoad.assignRoadWithoutRule(it.next, player2) yield stateWithSixRoad firstStateWithAward match case Some(state) => state.awards(Award(AwardType.LongestRoad)) should be(Some((player1, 5))) diff --git a/src/test/scala/scatan/model/game/ops/BuildingOpsTest.scala b/src/test/scala/scatan/model/game/ops/BuildingOpsTest.scala index 166b315e..52b566da 100644 --- a/src/test/scala/scatan/model/game/ops/BuildingOpsTest.scala +++ b/src/test/scala/scatan/model/game/ops/BuildingOpsTest.scala @@ -21,10 +21,6 @@ class BuildingOpsTest extends BaseScatanStateTest: private def roadNearSpot(state: ScatanState, spot: StructureSpot): RoadSpot = state.emptyRoadSpot.filter(_.contains(spot)).head - private def noConstrainToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = _ => (_, _) => true - private def spotShouldBeEmptyToBuildRoad: ScatanState => ((RoadSpot, ScatanPlayer) => Boolean) = s => - (r, _) => s.emptyRoadSpot.contains(r) - "A State with buildings Ops" should "have empty buildings when state start" in { val state = ScatanState(threePlayers) state.assignedBuildings.keySet should have size 0 @@ -34,7 +30,7 @@ class BuildingOpsTest extends BaseScatanStateTest: val state = ScatanState(threePlayers) given ScatanState = state state - .assignBuilding(spotToBuildRoad(state), BuildingType.Road, threePlayers.head, noConstrainToBuildRoad) match + .assignRoadWithoutRule(spotToBuildRoad(state), threePlayers.head) match case Some(state) => state.assignedBuildings should have size 1 case None => fail("state should be defined") } diff --git a/src/test/scala/scatan/model/game/ops/ScoreOpsTest.scala b/src/test/scala/scatan/model/game/ops/ScoreOpsTest.scala index b2fed425..54a3447b 100644 --- a/src/test/scala/scatan/model/game/ops/ScoreOpsTest.scala +++ b/src/test/scala/scatan/model/game/ops/ScoreOpsTest.scala @@ -46,7 +46,7 @@ class ScoreOpsTest extends BaseScatanStateTest: val state = ScatanState(threePlayers) val player1 = threePlayers.head val it = state.emptyRoadSpot.iterator - val stateWithRoad = state.assignBuilding(it.next(), BuildingType.Road, player1) + val stateWithRoad = state.assignRoadWithoutRule(it.next(), player1) stateWithRoad match case Some(state) => state.scores(player1) should be(0) @@ -60,11 +60,11 @@ class ScoreOpsTest extends BaseScatanStateTest: val stateWithSettlementAndAward = for stateWithSettlement <- state.assignBuilding(state.emptyStructureSpot.head, BuildingType.Settlement, player1) - oneRoadState <- stateWithSettlement.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1) - twoRoadState <- oneRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1) - threeRoadState <- twoRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1) - fourRoadState <- threeRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1) - fiveRoadState <- fourRoadState.assignBuilding(roadSpotIterator.next, BuildingType.Road, player1) + oneRoadState <- stateWithSettlement.assignRoadWithoutRule(roadSpotIterator.next, player1) + twoRoadState <- oneRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1) + threeRoadState <- twoRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1) + fourRoadState <- threeRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1) + fiveRoadState <- fourRoadState.assignRoadWithoutRule(roadSpotIterator.next, player1) yield fiveRoadState stateWithSettlementAndAward match case Some(state) =>