-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First good test written! It should fail because I'm TDDing
- Loading branch information
flober
committed
Aug 8, 2024
1 parent
5abf81a
commit 61009d4
Showing
9 changed files
with
109 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,81 @@ | ||
{-# LANGUAGE ConstraintKinds #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE OverloadedRecordDot #-} | ||
{-# LANGUAGE OverloadedRecordUpdate #-} | ||
{-# LANGUAGE RebindableSyntax #-} | ||
{-# OPTIONS_GHC -fplugin=Data.Record.Plugin #-} | ||
|
||
module CombatTest (module CombatTest) where | ||
|
||
import Card | ||
import Data.Maybe (fromJust) | ||
import Model | ||
import Combat | ||
import Control.Arrow (second) | ||
import Data.Record.Overloading hiding (loop) | ||
import Model hiding (turn) | ||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.HUnit (assertEqual, testCase) | ||
import Test.Tasty.HUnit (Assertion, assertEqual, testCase) | ||
|
||
combatTestGroup :: TestTree | ||
combatTestGroup = | ||
testGroup | ||
"Combat Tests" | ||
[ testHarmlessBonehead | ||
"test basic combats via histories" | ||
[ assert1v1HarmlessBonehead | ||
] | ||
|
||
testHarmlessBonehead :: TestTree | ||
testHarmlessBonehead = | ||
testCase "Expected combat state after player Harmless Bonehead attacks enemy Harmless Bonehead and dies." $ do | ||
assertEqual | ||
"Attack indices update correctly after bonehead death + summon, boneheads are the only minions." | ||
after | ||
(snipe 0 before) | ||
assertEqual | ||
"Attack indices update correctly after bonehead death + summon, when other minions exist before bonehead." | ||
after' | ||
(snipe 1 before') | ||
type HardCodedTurn = (CombatState -> (CombatState, CombatHistory)) -- See signature of `turn`. | ||
|
||
-- Frequently, we wish to test the expected history after a series of turns. | ||
-- There is a tradeoff with testing history versus testing combat states: | ||
-- Pro: Easier to write | ||
-- Con: Slightly less rigorous. Harder to localize error (at which turn did state go wrong?) | ||
makeMultiturnHistoryAssertionTest :: CombatHistory -> CombatState -> [HardCodedTurn] -> Assertion | ||
makeMultiturnHistoryAssertionTest expectedHistory cs turns = | ||
assertEqual | ||
"Expected same history" | ||
expectedHistory | ||
( snd -- discard CombatState since we just want history | ||
$ foldl | ||
(\(cs, histAcc) t -> second (histAcc ++) (t cs)) -- | ||
(cs, []) -- initial state and the history accumulator. | ||
turns | ||
) | ||
|
||
assert1v1HarmlessBonehead :: TestTree | ||
assert1v1HarmlessBonehead = | ||
testCase "Two boneheads full battle" $ makeMultiturnHistoryAssertionTest expectedHistory initialCS turns | ||
where | ||
before = | ||
CombatState | ||
{ attacker = FighterState {nextAttackIndex = 0, fighter = One, board = [CardInstance harmlessBonehead]}, | ||
defender = FighterState {nextAttackIndex = 0, fighter = Two, board = [CardInstance harmlessBonehead]} | ||
turns = [turn 0, turn 1, turn 0] | ||
onePS = | ||
defPlayerState | ||
{ phase = Combat, | ||
board = [CardInstance harmlessBonehead 0], | ||
idGen = IdGen 1 | ||
} | ||
after = | ||
CombatState | ||
{ attacker = FighterState {nextAttackIndex = 0, fighter = One, board = [CardInstance skeleton, CardInstance skeleton]}, | ||
defender = FighterState {nextAttackIndex = 0, fighter = Two, board = [CardInstance skeleton, CardInstance skeleton]} | ||
twoPS = | ||
defPlayerState | ||
{ phase = Combat, | ||
board = [CardInstance harmlessBonehead 0], | ||
idGen = IdGen 1 | ||
} | ||
before' = | ||
initialCS = | ||
CombatState | ||
{ attacker = FighterState {nextAttackIndex = 1, fighter = One, board = [CardInstance dummy, CardInstance harmlessBonehead]}, | ||
defender = FighterState {nextAttackIndex = 1, fighter = Two, board = [CardInstance dummy, CardInstance harmlessBonehead]} | ||
{ attacker = One, | ||
one = FighterState {nextAttackIndex = 0, playerState = onePS}, | ||
two = FighterState {nextAttackIndex = 0, playerState = twoPS}, | ||
config = Config {maxBoardSize = 7, maxHandSize = 10, maxCombatBoardSize = 7} | ||
} | ||
after' = | ||
CombatState | ||
{ attacker = FighterState {nextAttackIndex = 1, fighter = One, board = [CardInstance dummy, CardInstance skeleton, CardInstance skeleton]}, | ||
defender = FighterState {nextAttackIndex = 1, fighter = Two, board = [CardInstance dummy, CardInstance skeleton, CardInstance skeleton]} | ||
} | ||
expectedHistory = | ||
[ ([CardInstance harmlessBonehead 0], [CardInstance harmlessBonehead 0]), | ||
([CardInstance harmlessBonehead {health = 0} 0], [CardInstance harmlessBonehead {health = 0} 0]), | ||
([CardInstance skeleton 1], [CardInstance skeleton 1]), | ||
([CardInstance skeleton 1, CardInstance skeleton 2], [CardInstance skeleton 1, CardInstance skeleton 2]), | ||
([CardInstance skeleton 1, CardInstance skeleton {health = 0} 2], [CardInstance skeleton {health = 0} 1, CardInstance skeleton 2]), | ||
([CardInstance skeleton 1], [CardInstance skeleton 2]), | ||
([CardInstance skeleton {health = 0} 1], [CardInstance skeleton {health = 0} 2]), | ||
([], []) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Test.Tasty (defaultMain, testGroup) | ||
import CombatTest | ||
|
||
main :: IO () | ||
main = defaultMain $ testGroup "Unit Tests" [] | ||
main = defaultMain $ testGroup "Unit Tests" [combatTestGroup] |