Skip to content

Commit

Permalink
WIP to fix tests: pass Factions fixture during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 14, 2024
1 parent b113e78 commit c899991
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/game-lib-tests/FactionFixtures.cs
Original file line number Diff line number Diff line change
@@ -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),
]);
}
22 changes: 18 additions & 4 deletions src/game-lib-tests/GameSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/game-lib/State/GameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ public class GameSession

public readonly MissionSiteIdGen MissionSiteIdGen;

public GameSession(RandomGen randomGen, List<GameSessionTurn>? turns = null)
public GameSession(RandomGen randomGen, List<GameSessionTurn>? 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<GameState> GameStates
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/State/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit c899991

Please sign in to comment.