hamming/test/Hamming/App/ActionsSpec.hs

126 lines
3.4 KiB
Haskell

{-
hamming
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<https://www.gnu.org/licenses/>.
-}
module Hamming.App.ActionsSpec (spec) where
import Lens.Micro ((&), (.~))
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Hamming.App.Actions
import Hamming.App.Types
spec :: Spec
spec = describe "Actions" $ do
moveUpSpec
moveDownSpec
moveLeftSpec
moveRightSpec
flipBitSpec
moveUpSpec :: Spec
moveUpSpec = describe "moveUp" $ mapM_
( \(desc, state, expected) -> context desc $ let
actual = moveUp state
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "at the top", atTop, atTop )
, ( "elsewhere", elsewhere, movedUp )
] where
atTop = initialEditor & colNum .~ 1
elsewhere = atTop & rowNum .~ 2
movedUp = atTop & rowNum .~ 1
moveDownSpec :: Spec
moveDownSpec = describe "moveDown" $ mapM_
( \(desc, state, expected) -> context desc $ let
actual = moveDown state
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "at the bottom", atBottom, atBottom )
, ( "elsewhere", elsewhere, movedDown )
] where
atBottom = initialEditor
& colNum .~ 1
& rowNum .~ 3
elsewhere = initialEditor
& colNum .~ 1
& rowNum .~ 1
movedDown = initialEditor
& colNum .~ 1
& rowNum .~ 2
moveLeftSpec :: Spec
moveLeftSpec = describe "moveLeft" $ mapM_
( \(desc, state, expected) -> context desc $ let
actual = moveLeft state
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "at left", atLeft, atLeft )
, ( "elsewhere", elsewhere, movedLeft )
] where
atLeft = initialEditor & rowNum .~ 1
elsewhere = atLeft & colNum .~ 2
movedLeft = atLeft & colNum .~ 1
moveRightSpec :: Spec
moveRightSpec = describe "moveRight" $ mapM_
( \(desc, state, expected) -> context desc $ let
actual = moveRight state
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "at right", atRight, atRight )
, ( "elsewhere", elsewhere, movedRight )
] where
atRight = initialEditor
& rowNum .~ 1
& colNum .~ 3
elsewhere = initialEditor
& rowNum .~ 1
& colNum .~ 1
movedRight = initialEditor
& rowNum .~ 1
& colNum .~ 2
flipBitSpec :: Spec
flipBitSpec = describe "flipBit" $ mapM_
( \(desc, state, expected) -> context desc $ let
actual = flipBit state
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "turn on", unflipped, flipped )
, ( "turn off", flipped, unflipped )
, ( "non-edit mode", nonEdit, nonEdit )
] where
nonEdit = initialState & hammingCode .~ 0x3c5a
unflipped = nonEdit & appMode .~ EditMode
( initialEditor
& rowNum .~ 1
& colNum .~ 3
)
flipped = unflipped & hammingCode .~ 0x3cda
--jl