Skip to content

Commit 33a8ba8

Browse files
committed
Add tests
1 parent 8280a26 commit 33a8ba8

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

O21.Tests/GameEngineTests.fs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,41 @@ module Player =
142142
Velocity = Vector(1, 0)
143143
}
144144
let player' = player.Update(getEmptyPlayerEnvWithLevel level, 1)
145-
Assert.True(match player' with | PlayerEffect.Die -> true | _ -> false)
145+
Assert.True(match player' with | PlayerEffect.Die -> true | _ -> false)
146+
147+
[<Theory>]
148+
[<InlineData(EntityKind.Fish)>]
149+
[<InlineData(EntityKind.Bomb)>]
150+
let ``Colliding with enemy kills the player and enemy``(enemy: EntityKind): unit =
151+
let level = createEmptyLevel 1 2
152+
153+
let engine = newEngine.ChangeLevel(level)
154+
let engine = engine |> spawnEntity enemy (0, 1)
155+
156+
let enemyPos = engine |> getEntityPos enemy 0
157+
158+
let dist = 10
159+
160+
let engine =
161+
{ engine with
162+
GameEngine.Player.TopLeft = Point(enemyPos.X,
163+
enemyPos.Y - GameRules.PlayerSize.Y - dist)
164+
GameEngine.Player.Velocity = Vector(0, 1) }
165+
166+
let initialLives = engine.Player.Lives
167+
let engine = engine |> frameUpN timeZero (dist + 1)
168+
169+
let actualLives = engine.Player.Lives
170+
171+
Assert.Equal(initialLives - 1, actualLives)
172+
match enemy with
173+
| EntityKind.Bomb ->
174+
Assert.Equal(0, engine.Bombs.Length)
175+
| EntityKind.Fish ->
176+
// Assert.Equal(0, engine.Fishes.Length)
177+
// TODO: Uncomment when fishes are implemented
178+
()
179+
| _ -> failwith "Entity is not enemy"
146180

147181
module OxygenSystem =
148182

@@ -294,15 +328,7 @@ module ScoreSystem =
294328

295329
let engine = newEngine.ChangeLevel(level)
296330
let engine = engine |> spawnEntity entity (1, 0)
297-
let enemyPosX =
298-
match entity with
299-
| EntityKind.Bomb ->
300-
engine.Bombs[0].TopLeft.X
301-
| EntityKind.Fish ->
302-
engine.Fishes[0].TopLeft.X
303-
| EntityKind.StaticBonus | EntityKind.Lifebuoy | EntityKind.LifeBonus ->
304-
engine.Bonuses[0].TopLeft.X
305-
| _ -> failwithf $"Cannot get points for shot %s{Enum.GetName(entity)}"
331+
let enemyPosX = engine |> getEntityPos entity 0 |> _.X
306332

307333
let engine =
308334
{ engine with
@@ -354,6 +380,23 @@ module ScoreSystem =
354380
let engine = engine |> frameUpN timeZero 1
355381
let actualLives = engine.Player.Lives
356382
Assert.Equal(initialLives + 1, actualLives)
383+
384+
[<Fact>]
385+
let ``Picking up a life bonus adds ability to the player``(): unit =
386+
let level = createEmptyLevel 1 1
387+
388+
let engine = newEngine.ChangeLevel(level)
389+
let engine = engine |> spawnEntity EntityKind.Lifebuoy (0, 0)
390+
391+
let engine =
392+
{ engine with
393+
GameEngine.Player.TopLeft = engine.Bonuses[0].TopLeft }
394+
395+
let initialAbilities = engine.Player.Abilities.Length
396+
397+
let engine = engine |> frameUpN timeZero 1
398+
let actualAbilities = engine.Player.Abilities.Length
399+
Assert.Equal(initialAbilities + 1, actualAbilities)
357400

358401
module Geometry =
359402
open O21.Game.Engine.Geometry

O21.Tests/Helpers.fs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ let spawnEntity entityKind levelCoords engine =
3737

3838
match entityKind with
3939
| EntityKind.Player ->
40-
System.ArgumentException("There should be only one player") |> raise
40+
{ engine with
41+
GameEngine.Player.TopLeft = position
42+
}
4143
| EntityKind.Fish ->
4244
{ engine with
4345
Fishes = Array.append engine.Fishes [| { Fish.Default with TopLeft = position } |]
@@ -59,3 +61,17 @@ let spawnEntity entityKind levelCoords engine =
5961
Bonuses = Array.append engine.Bonuses [| { TopLeft = position; Type = BonusType.Life } |]
6062
}
6163
| _ -> System.ArgumentOutOfRangeException("Cannot spawn this entity exist") |> raise
64+
65+
let getEntityPos entityKind i (engine: GameEngine)=
66+
match entityKind with
67+
| EntityKind.Player ->
68+
engine.Player.TopLeft
69+
| EntityKind.Fish ->
70+
engine.Fishes[i].TopLeft
71+
| EntityKind.Bomb ->
72+
engine.Bombs[i].TopLeft
73+
| EntityKind.StaticBonus
74+
| EntityKind.Lifebuoy
75+
| EntityKind.LifeBonus ->
76+
engine.Bonuses[i].TopLeft
77+
| _ -> System.ArgumentOutOfRangeException("Cannot get TopLeft position from entity") |> raise

0 commit comments

Comments
 (0)