Skip to content

Commit

Permalink
Add coverage for the update function
Browse files Browse the repository at this point in the history
  • Loading branch information
franckverrot committed Feb 24, 2017
1 parent ea41445 commit f66b6fa
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 41 deletions.
25 changes: 13 additions & 12 deletions build/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8985,6 +8985,18 @@ var _user$project$MyEvent$Clicked = function (a) {
return {ctor: 'Clicked', _0: a};
};

var _user$project$GameLogic$noneUnclaimed = function (ary) {
return _elm_lang$core$Native_Utils.eq(
0,
_elm_lang$core$Array$length(
A2(
_elm_lang$core$Array$filter,
function (x) {
return _elm_lang$core$Native_Utils.eq(x, _user$project$Player$Unclaimed);
},
ary)));
};

var _user$project$EventHandlers_OnClicked$onClicked = F2(
function (model, index) {
var leaveBoardIntact = function (player) {
Expand Down Expand Up @@ -9207,17 +9219,6 @@ var _user$project$Update$playerWon = F2(
return _elm_lang$core$Result$Err('Not there yet!');
}
});
var _user$project$Update$noneUnclaimed = function (ary) {
return _elm_lang$core$Native_Utils.eq(
0,
_elm_lang$core$Array$length(
A2(
_elm_lang$core$Array$filter,
function (x) {
return _elm_lang$core$Native_Utils.eq(x, _user$project$Player$Unclaimed);
},
ary)));
};
var _user$project$Update$changePlayer = function (p) {
var _p1 = p;
switch (_p1.ctor) {
Expand All @@ -9240,7 +9241,7 @@ var _user$project$Update$update = F2(
{ctor: '[]'});
case 'CheckWinner':
var _p5 = _p2._0;
if (_user$project$Update$noneUnclaimed(model.boxes)) {
if (_user$project$GameLogic$noneUnclaimed(model.boxes)) {
return A2(
_elm_lang$core$Platform_Cmd_ops['!'],
_elm_lang$core$Native_Utils.update(
Expand Down
15 changes: 8 additions & 7 deletions src/EventHandlers/OnClicked.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ module EventHandlers.OnClicked exposing ( onClicked )
@docs onClicked
-}
import Array exposing (..)
import Maybe exposing (..)
import Model exposing (..)
import MyEvent exposing (..)
import Player exposing (..)
import Task exposing (..)
import Time exposing (..)
import Array exposing (..)
import Maybe exposing (..)
import Model exposing (..)
import MyEvent exposing (..)
import Player exposing (..)
import Task exposing (..)
import Time exposing (..)
import GameLogic exposing (noneUnclaimed)

{-| onClicked -}
onClicked : Model -> Int -> (Model, Cmd MyEvent)
Expand Down
7 changes: 7 additions & 0 deletions src/GameLogic.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GameLogic exposing (noneUnclaimed)

import Array exposing (..)
import Player exposing (..)

noneUnclaimed : Array Player -> Bool
noneUnclaimed ary = (0 == length(filter (\x -> x == Unclaimed) ary))
6 changes: 2 additions & 4 deletions src/Update.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Update exposing ( update
, changePlayer
, noneUnclaimed
, currentPlayerWinning
)

import Array exposing (..)
import EventHandlers.OnClicked
import GameLogic exposing (noneUnclaimed)
import Model exposing (..)
import MyEvent exposing (..)
import Player exposing (..)
Expand All @@ -16,8 +16,6 @@ changePlayer p = case p of
B -> A
_ -> A

noneUnclaimed : Array Player -> Bool
noneUnclaimed ary = (0 == length(filter (\x -> x == Unclaimed) ary))

type alias GameStatus = Result String Player

Expand Down Expand Up @@ -57,8 +55,8 @@ update msg model =

CheckWinner player currentPlayerShouldChange _ ->
if noneUnclaimed model.boxes then
-- it's a draw
{ model | winner = Just Unclaimed } ! []

else
case currentPlayerShouldChange of
-- Legal move
Expand Down
8 changes: 5 additions & 3 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import Fuzz exposing (list, int, tuple, string)
import String
import List exposing (append)

import Unit.ModelTests exposing (modelTests)
import Unit.PlayerTests exposing (playerTests)
import Unit.UpdateTests exposing (updateTests)
import Unit.ModelTests exposing (modelTests)
import Unit.PlayerTests exposing (playerTests)
import Unit.UpdateTests exposing (updateTests)
import Unit.GameLogicTests exposing (gameLogicTests)


all : Test
all =
modelTests
|> append playerTests
|> append updateTests
|> append gameLogicTests
|> describe "Test Suite"
21 changes: 21 additions & 0 deletions tests/unit/GameLogicTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Unit.GameLogicTests exposing (gameLogicTests)

import Test exposing (..)
import Expect
import Array
import GameLogic exposing (noneUnclaimed)
import Model exposing (Model, initialModel)
import Player exposing (Player (A, B, Unclaimed))

gameLogicTests : List Test
gameLogicTests = [ describe "GameLogic.noneUnclaimed"
[ test "returns True if none of the boxes are unclaimed" <|
\() ->
Expect.equal True
<| noneUnclaimed <| Array.fromList <| [A, B, A]
, test "returns False if at least one box is unclaimed" <|
\() ->
Expect.equal False
<| noneUnclaimed <| Array.fromList <| [A, Unclaimed, A]
]
]
59 changes: 44 additions & 15 deletions tests/unit/UpdateTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,55 @@ module Unit.UpdateTests exposing (updateTests)
import Test exposing (..)
import Expect
import Array
import Player exposing (Player (A, B, Unclaimed))
import Update exposing (changePlayer, noneUnclaimed, currentPlayerWinning)
import Model exposing (initialModel)
import MyEvent exposing (MyEvent(Clicked, CheckWinner, Reset))
import Player exposing (Player (A, B, Unclaimed))
import Update exposing (changePlayer, currentPlayerWinning, update)
import Time exposing (second)

updateTests : List Test
updateTests = [ describe "Update.changePlayer"
[ test "switches turn from A to B" <|
updateTests = [ describe "Update.update"
[ test "Handles Reset" <|
\() ->
Expect.equal B <| Update.changePlayer A
, test "switches turn from B to A" <|
Expect.equal (initialModel ! [])
<| update Reset initialModel
, test "Handles CheckWinner -- none unclaimed" <|
\() ->
Expect.equal A <| Update.changePlayer B
]
, describe "Update.noneUnclaimed"
[ test "returns True if none of the boxes are unclaimed" <|
let
message = (CheckWinner A False Time.second)
previousModel = { initialModel
| boxes = (Array.fromList [A, A, A, A, B, B, B, A, B])
}
(newModel, _) = update message previousModel
in
Expect.equal (Just Unclaimed) (newModel.winner)
, test "Handles CheckWinner -- legal move with winner" <|
\() ->
let
message = (CheckWinner A True Time.second)
previousModel = { initialModel
| boxes = (Array.fromList [A, A, A, B, B, Unclaimed, Unclaimed, Unclaimed, Unclaimed])
}
(newModel, _) = update message previousModel
in
Expect.equal (Just A) newModel.winner
, test "Handles CheckWinner -- legal move with no winner" <|
\() ->
Expect.equal True
<| noneUnclaimed <| Array.fromList <| [A, B, A]
, test "returns False if at least one box is unclaimed" <|
let
message = (CheckWinner A True Time.second)
previousModel = { initialModel
| boxes = (Array.fromList [A, A, Unclaimed, B, B, Unclaimed, Unclaimed, Unclaimed, Unclaimed])
}
(newModel, _) = update message previousModel
in
Expect.equal Nothing newModel.winner
, test "Handles CheckWinner -- illegal move" <|
\() ->
Expect.equal False
<| noneUnclaimed <| Array.fromList <| [A, Unclaimed, A]
let
msg = (CheckWinner A False Time.second)
model = initialModel
in
Expect.equal (initialModel ! [])
<| update msg model
]
]

0 comments on commit f66b6fa

Please sign in to comment.