player move tests

This commit is contained in:
Sidharth Kulkarni 2026-05-09 09:10:16 -07:00
parent 9301f2211c
commit 4b34e48a59
Signed by: skulk
SSH key fingerprint: SHA256:Jby+S9d1WmwqnXIrngHgccYNHz+cYquxN1zm3ym3Kbg

View file

@ -4,30 +4,64 @@
module Spec where module Spec where
import Data.Array (array, elems) import Data.Array (array, elems)
import Data.Sequence qualified as S
import Display (displayText) import Display (displayText)
import Optics
import Test.Hspec import Test.Hspec
import Types.BoardAction
import Types.GameBoard import Types.GameBoard
import Types.GameMode (SumTo (MkSumTo), Tile (IntTile)) import Types.GameMode (SumTo (MkSumTo), Tile (IntTile))
import Types.GameState
import Types.Player
spec :: Spec spec :: Spec
spec = describe "gameboard" do spec = do
let tiles = describe "gameboard" do
array let tiles =
((0, 0), (1, 1)) array
[ ((0, 0), Just $ IntTile 1) ((0, 0), (1, 1))
, ((0, 1), Just $ IntTile 2) [ ((0, 0), Just $ IntTile 1)
, ((1, 0), Just $ IntTile 3) , ((0, 1), Just $ IntTile 2)
, ((1, 1), Just $ IntTile 4) , ((1, 0), Just $ IntTile 3)
] , ((1, 1), Just $ IntTile 4)
gameBoard = MkGameBoard{width = 2, height = 2, gameMode = MkSumTo 10, cells = tiles} ]
gameBoard = MkGameBoard{width = 2, height = 2, gameMode = MkSumTo 10, cells = tiles}
it "displays correctly" do it "displays correctly" do
displayText gameBoard `shouldBe` "1 2 \n3 4 \n" displayText gameBoard `shouldBe` "1 2 \n3 4 \n"
it "validates a move" do it "validates a move" do
selectCheck (0, 0) (1, 1) gameBoard `shouldBe` True selectCheck (0, 0) (1, 1) gameBoard `shouldBe` True
selectCheck (0, 0) (0, 1) gameBoard `shouldBe` False selectCheck (0, 0) (0, 1) gameBoard `shouldBe` False
it "clears properly" do it "clears properly" do
let cleared = clear (0, 0) (0, 1) gameBoard let cleared = clear (0, 0) (0, 1) gameBoard
elems (cells cleared) `shouldBe` [Nothing, Nothing, Just $ IntTile 3, Just $ IntTile 4] elems (cells cleared) `shouldBe` [Nothing, Nothing, Just $ IntTile 3, Just $ IntTile 4]
describe "player moves" do
let tiles =
array
((0, 0), (1, 1))
[ ((0, 0), Just $ IntTile 5)
, ((0, 1), Just $ IntTile 5)
, ((1, 0), Just $ IntTile 1)
, ((1, 1), Just $ IntTile 2)
]
gameBoard = MkGameBoard{width = 2, height = 2, gameMode = MkSumTo 10, cells = tiles}
initialState = MkGameState{board = gameBoard, players = S.fromList [MkPlayerState (MkPlayerName "P1") 0]}
it "increases score if sum equals target" do
let action = SelectSquare (0, 0) (0, 1)
newState = makeMove (MkPlayerIndex 0) action initialState
preview (#players % ix 0 % #score) newState `shouldBe` Just 1
it "does not increase score if sum does not equal target" do
let invalidAction = SelectSquare (0, 0) (1, 0)
newState = makeMove (MkPlayerIndex 0) invalidAction initialState
preview (#players % ix 0 % #score) newState `shouldBe` Just 0
it "does not change anything if invalid player index is provided" do
let action = SelectSquare (0, 0) (0, 1)
newState = makeMove (MkPlayerIndex 99) action initialState
preview (#players % ix 99 % #score) newState `shouldBe` Nothing