Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to visualize test cases in-game #144

Merged
merged 2 commits into from
Nov 13, 2024
Merged

Conversation

SimonAlling
Copy link
Owner

@SimonAlling SimonAlling commented Nov 11, 2024

When writing test cases or trying to figure out why a test case is failing, it's usually very helpful to visualize the test case in the game.

This PR takes all test cases that don't involve multiple dynamically created sub-tests, and moves their initial states into their own modules in src/TestScenarios/, thereby enabling visualization with minimal changes in src/Main.elm:

import TestScenarios.CrashIntoWallExactTiming

init : () -> ( Model, Cmd Msg )
init _ =
    let
        ( gameState, cmd ) =
            { seedAfterSpawn = Random.initialSeed 0
            , spawnedKurves = TestScenarios.CrashIntoWallExactTiming.spawnedKurves
            }
                |> prepareReplayRound
                |> newRoundGameStateAndCmd Config.default
    in
    ( { pressedButtons = Set.empty
      , appState = InGame gameState
      , config = Config.default
      , players = initialPlayers
      }
    , cmd
    )

Moving tickNumber into src/ means that it cannot use Debug.todo anymore (because npm run build would then fail), so I went with Tick.genesis because it seemed like the most reasonable option. I guess tickNumber n would also make sense in principle, but it would cause npm test to hang indefinitely should a negative number be passed to tickNumber.

For the record, one can short-circuit update to speed up the visualization:

--- b/src/Main.elm
+++ a/src/Main.elm
@@ -98,6 +98,6 @@ stepSpawnState config { kurvesLeft, ticksLeft } =


-update : Msg -> Model -> ( Model, Cmd Msg )
-update msg ({ config, pressedButtons } as model) =
+update : Cmd Msg -> Msg -> Model -> ( Model, Cmd Msg )
+update cmdSoFar msg ({ config, pressedButtons } as model) =
     case msg of
         FocusLost ->
@@ -117,8 +117,21 @@ update msg ({ config, pressedButtons } as model) =
                 ( tickResult, cmd ) =
                     Game.reactToTick config tick midRoundState
+
+                newModel =
+                    { model | appState = InGame (tickResultToGameState tickResult) }
+
+                newCmd =
+                    Cmd.batch [ cmdSoFar, cmd ]
             in
-            ( { model | appState = InGame (tickResultToGameState tickResult) }
-            , cmd
-            )
+            case tickResult of
+                RoundKeepsGoing _ newMidRoundState ->
+                    if modBy 100 (Tick.toInt tick) == 0 then
+                        ( newModel, newCmd )
+
+                    else
+                        update newCmd (GameTick (Tick.succ tick) newMidRoundState) newModel
+
+                RoundEnds _ ->
+                    ( newModel, newCmd )

         ButtonUsed Down button ->

(Without the modBy 100 check, the StressTestRealisticTurtleSurvivalRound scenario would crash with InternalError: too much recursion.)

💡 git show --color-moved=plain --color-moved-ws=allow-indentation-change

@SimonAlling
Copy link
Owner Author

I have only moved some test case scenarios for starters, to give an idea of the change. All of them should be given the same treatment before merging.

Copy link
Collaborator

@lydell lydell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

It was fun watching what StressTestRealisticTurtleSurvivalRound really looks like.

@SimonAlling SimonAlling marked this pull request as ready for review November 13, 2024 22:20
@SimonAlling SimonAlling merged commit 1be5122 into main Nov 13, 2024
1 check passed
@SimonAlling SimonAlling deleted the test-scenarios branch November 13, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants