hamming/test/Hamming/App/Widgets/InternalSpec.hs

204 lines
5.5 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.Widgets.InternalSpec (spec) where
import Brick.Types (Location (..))
import Lens.Micro ((&), (.~))
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Hamming.App
import Hamming.App.Types
import Hamming.App.Widgets.Internal
spec :: Spec
spec = describe "Internal"
hammingW'Spec
hammingW'Spec :: Spec
hammingW'Spec = describe "hammingW'" $ mapM_
( \(desc, state, expRes, expLoc) -> context desc $ do
let
(actRes, actLoc) = hammingW' state
numActRows = length actRes
numExpRows = length expRes
context "location" $
it ("should be " ++ show expLoc) $
actLoc `shouldBe` expLoc
context "number of rows" $
it ("should be " ++ show numExpRows) $
numActRows `shouldBe` numExpRows
mapM_
( \(row, actRow, expRow) -> let
numActCols = length actRow
numExpCols = length expRow
in context ("row " ++ show row) $ do
context "number of columns" $
it ("should be " ++ show numExpCols) $
numActCols `shouldBe` numExpCols
mapM_
( \(col, actBit, expBit) ->
context ("column " ++ show col) $
it ("should be " ++ show expBit) $
actBit `shouldBe` expBit
) $ zip3 [(0 :: Int)..] actRow expRow
) $ zip3 [(0 :: Int)..] actRes expRes
)
[ ( "all zero", mkState 0, allZero, Nothing )
, ( "all one", mkState 0xffff, allOne, Nothing )
, ( "arbitrary", mkState 0x3c5a, arbitrary, Nothing )
, ( "invalid", mkState 0x3c5b, invalid, Nothing )
, ( "edit", es, allZero, Just eLoc )
] where
mkState c = initialState & hammingCode .~ c
es = mkState 0 & appMode .~ EditMode initialEditor
eLoc = Location (1, 1)
allZero =
[ [ ( validAttr, '✓' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '0' )
, ( checkAttr, '0' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
]
, [ ( marginAttr, '1' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
]
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
]
, [ ( marginAttr, '3' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
]
]
allOne =
[ [ ( validAttr, '✓' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '1' )
, ( checkAttr, '1' )
, ( checkAttr , '1' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '2' )
, ( checkAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '3' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
]
]
arbitrary =
[ [ ( validAttr, '✓' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '0' )
, ( checkAttr, '1' )
, ( checkAttr, '0' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( bodyAttr, '0' )
, ( bodyAttr, '1' )
, ( bodyAttr, '0' )
]
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '3' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
]
]
invalid =
[ [ ( invalidAttr, 'X' )
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
]
, [ ( marginAttr, '0' )
, ( zeroAttr, '1' )
, ( checkAttr, '1' )
, ( checkAttr, '0' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( bodyAttr, '0' )
, ( bodyAttr, '1' )
, ( bodyAttr, '0' )
]
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
]
, [ ( marginAttr, '3' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
]
]
--jl