From c8999910fe6a9bc5bea6460bf948186e2fd5311e Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Fri, 14 Jun 2024 01:07:59 -0700 Subject: [PATCH] WIP to fix tests: pass Factions fixture during testing --- src/game-lib-tests/FactionFixtures.cs | 13 +++++++++++++ src/game-lib-tests/GameSessionTests.cs | 22 ++++++++++++++++++---- src/game-lib/State/GameSession.cs | 7 +++---- src/game-lib/State/GameState.cs | 4 ++-- 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 src/game-lib-tests/FactionFixtures.cs diff --git a/src/game-lib-tests/FactionFixtures.cs b/src/game-lib-tests/FactionFixtures.cs new file mode 100644 index 00000000..818f27f8 --- /dev/null +++ b/src/game-lib-tests/FactionFixtures.cs @@ -0,0 +1,13 @@ +using UfoGameLib.Lib; +using UfoGameLib.Model; + +namespace UfoGameLib.Tests; + +public static class FactionFixtures +{ + public static Factions SingleFaction(RandomGen randomGen) => new( + [ + // Note: need to ensure here that IDs are consecutive, and from zero. + Faction.Init(randomGen, id: 0, "Black Lotus cult", power: 200, powerIncrease: 5), + ]); +} \ No newline at end of file diff --git a/src/game-lib-tests/GameSessionTests.cs b/src/game-lib-tests/GameSessionTests.cs index 98ec6261..77d58394 100644 --- a/src/game-lib-tests/GameSessionTests.cs +++ b/src/game-lib-tests/GameSessionTests.cs @@ -9,15 +9,19 @@ namespace UfoGameLib.Tests; public class GameSessionTests { + // Using null! as these fields will be initialized in Setup() method. private Configuration _config = null!; private ILog _log = null!; - private readonly RandomGen _randomGen = new RandomGen(new Random()); + private RandomGen _randomGen = null!; + private Factions _factions = null!; [SetUp] public void Setup() { _config = new Configuration(new FileSystem()); _log = new Log(_config); + _randomGen = new RandomGen(new Random()); + _factions = FactionFixtures.SingleFaction(_randomGen); } // kja3 overall work plan: @@ -56,7 +60,7 @@ public void Setup() [Test] public void BasicHappyPathGameSessionWorks() { - var session = new GameSession(_randomGen); + var session = new GameSession(_randomGen, factions: _factions); var controller = new GameSessionController(_config, _log, session); var turnController = controller.CurrentTurnController; @@ -75,6 +79,16 @@ public void BasicHappyPathGameSessionWorks() controller.AdvanceTime(); controller.AdvanceTime(); // kja BUG ROOT CAUSE: getting no elems here because now site generation is randomized. Need to override it for testing. + // To solve this: + // 1. introduce IRandomGen and make it implement more abstract methods so they can be overriden. Like this one: + // + // // class Faction: + // private static int RandomizeMissionSiteCountdown(RandomGen randomGen) + // => randomGen.Roll(Ruleset.FactionMissionSiteCountdown); + // + // This way for testing I can say "the countdown is always 3" + // + // 2. Allow to pass factions when initing game session MissionSite site = controller.CurrentGameStatePlayerView.MissionSites.First(); turnController.LaunchMission(site, agentCount: 3); controller.AdvanceTime(); @@ -158,7 +172,7 @@ public void LoadingPreviousGameStateOverridesCurrentState() [Test] public void RoundTrippingSavingAndLoadingGameStateBehavesCorrectly() { - var session = new GameSession(_randomGen); + var session = new GameSession(_randomGen, factions: _factions); var controller = new GameSessionController(_config, _log, session); var turnController = controller.CurrentTurnController; @@ -221,7 +235,7 @@ public void RoundTrippingSavingAndLoadingGameStateBehavesCorrectly() [Test] public void RoundTrippingSavingAndLoadingGameStateWithActiveMissionBehavesCorrectly() { - var session = new GameSession(_randomGen); + var session = new GameSession(_randomGen, factions: _factions); var controller = new GameSessionController(_config, _log, session); var turnController = controller.CurrentTurnController; GameStatePlayerView state = controller.CurrentGameStatePlayerView; diff --git a/src/game-lib/State/GameSession.cs b/src/game-lib/State/GameSession.cs index a1eb7769..9230efe1 100644 --- a/src/game-lib/State/GameSession.cs +++ b/src/game-lib/State/GameSession.cs @@ -34,18 +34,17 @@ public class GameSession public readonly MissionSiteIdGen MissionSiteIdGen; - public GameSession(RandomGen randomGen, List? turns = null) + public GameSession(RandomGen randomGen, List? turns = null, Factions? factions = null) { RandomGen = randomGen; - Turns = turns ?? [new GameSessionTurn(startState: GameState.NewInitialGameState(randomGen))]; + Turns = turns ?? [new GameSessionTurn(startState: GameState.NewInitialGameState(randomGen, factions))]; Contract.Assert(Turns.Any()); Turns.ForEach(turn => turn.AssertInvariants()); - + EventIdGen = new EventIdGen(Turns); AgentIdGen = new AgentIdGen(Turns); MissionSiteIdGen = new MissionSiteIdGen(Turns); MissionIdGen = new MissionIdGen(Turns); - } public IReadOnlyList GameStates diff --git a/src/game-lib/State/GameState.cs b/src/game-lib/State/GameState.cs index 159d7cd3..4335c626 100644 --- a/src/game-lib/State/GameState.cs +++ b/src/game-lib/State/GameState.cs @@ -40,7 +40,7 @@ public GameState( Factions = factions; } - public static GameState NewInitialGameState(RandomGen? randomGen = null) + public static GameState NewInitialGameState(RandomGen? randomGen = null, Factions? factions = null) { randomGen ??= new RandomGen(new Random()); return new GameState( @@ -56,7 +56,7 @@ public static GameState NewInitialGameState(RandomGen? randomGen = null) new MissionSites(), new Missions(), terminatedAgents: new Agents(terminated: true), - factions: Ruleset.InitialFactions(randomGen)); + factions: factions ?? Ruleset.InitialFactions(randomGen)); } public static GameState FromJsonFile(File file)