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

204 lines
5.5 KiB
Haskell
Raw Normal View History

2024-07-19 20:50:33 -04:00
{-
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
2024-08-01 16:16:22 -04:00
import Brick.Types (Location (..))
2024-07-19 20:50:33 -04:00
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_
2024-08-01 16:16:22 -04:00
( \(desc, state, expRes, expLoc) -> context desc $ do
2024-07-20 16:27:41 -04:00
let
2024-08-01 16:16:22 -04:00
(actRes, actLoc) = hammingW' state
2024-08-01 16:04:37 -04:00
numActRows = length actRes
numExpRows = length expRes
2024-08-01 16:16:22 -04:00
context "location" $
it ("should be " ++ show expLoc) $
actLoc `shouldBe` expLoc
2024-07-19 20:50:33 -04:00
context "number of rows" $
2024-07-20 16:27:41 -04:00
it ("should be " ++ show numExpRows) $
numActRows `shouldBe` numExpRows
2024-07-19 20:50:33 -04:00
mapM_
2024-07-25 20:06:13 -04:00
( \(row, actRow, expRow) -> let
2024-07-20 16:27:41 -04:00
numActCols = length actRow
numExpCols = length expRow
2024-07-25 20:06:13 -04:00
in context ("row " ++ show row) $ do
2024-07-19 20:50:33 -04:00
context "number of columns" $
2024-07-20 16:27:41 -04:00
it ("should be " ++ show numExpCols) $
numActCols `shouldBe` numExpCols
2024-07-19 20:50:33 -04:00
mapM_
2024-07-25 20:06:13 -04:00
( \(col, actBit, expBit) ->
context ("column " ++ show col) $
2024-07-19 20:50:33 -04:00
it ("should be " ++ show expBit) $
actBit `shouldBe` expBit
) $ zip3 [(0 :: Int)..] actRow expRow
) $ zip3 [(0 :: Int)..] actRes expRes
)
2024-08-01 16:16:22 -04:00
[ ( "all zero", mkState 0, allZero, Nothing )
, ( "all one", mkState 0xffff, allOne, Nothing )
, ( "arbitrary", mkState 0x3c5a, arbitrary, Nothing )
2024-08-01 18:33:33 -04:00
, ( "invalid", mkState 0x3c5b, invalid, Nothing )
2024-08-01 16:16:22 -04:00
, ( "edit", es, allZero, Just eLoc )
2024-07-19 20:50:33 -04:00
] where
mkState c = initialState & hammingCode .~ c
2024-08-01 16:16:22 -04:00
es = mkState 0 & appMode .~ EditMode initialEditor
eLoc = Location (1, 1)
2024-07-19 20:50:33 -04:00
allZero =
2024-08-01 18:33:33 -04:00
[ [ ( validAttr, '✓' )
2024-07-31 18:22:12 -04:00
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
2024-07-20 16:27:41 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '0' )
, ( zeroAttr, '0' )
, ( checkAttr, '0' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '1' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '3' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
2024-07-19 20:50:33 -04:00
]
]
allOne =
2024-08-01 18:33:33 -04:00
[ [ ( validAttr, '✓' )
2024-07-31 18:22:12 -04:00
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
2024-07-20 16:27:41 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '0' )
, ( zeroAttr, '1' )
, ( checkAttr, '1' )
, ( checkAttr , '1' )
, ( bodyAttr, '1' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '2' )
, ( checkAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '3' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
2024-07-19 20:50:33 -04:00
]
]
arbitrary =
2024-08-01 18:33:33 -04:00
[ [ ( validAttr, '✓' )
2024-07-31 18:22:12 -04:00
, ( marginAttr, '0' )
, ( marginAttr, '1' )
, ( marginAttr, '2' )
, ( marginAttr, '3' )
2024-07-20 16:27:41 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '0' )
, ( zeroAttr, '0' )
, ( checkAttr, '1' )
, ( checkAttr, '0' )
, ( bodyAttr, '1' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '1' )
, ( checkAttr, '1' )
, ( bodyAttr, '0' )
, ( bodyAttr, '1' )
, ( bodyAttr, '0' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '2' )
, ( checkAttr, '0' )
, ( bodyAttr, '0' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
2024-07-19 20:50:33 -04:00
]
2024-07-31 18:22:12 -04:00
, [ ( marginAttr, '3' )
, ( bodyAttr, '1' )
, ( bodyAttr, '1' )
, ( bodyAttr, '0' )
, ( bodyAttr, '0' )
2024-07-19 20:50:33 -04:00
]
]
2024-08-01 18:33:33 -04:00
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' )
]
]
2024-07-19 20:50:33 -04:00
--jl