implemented editStateL

This commit is contained in:
Jonathan Lamothe 2024-07-26 20:35:54 -04:00
parent e20dda8c9f
commit 408e0f0f82
5 changed files with 121 additions and 4 deletions

View File

@ -67,6 +67,8 @@ test-suite hamming-test
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Spec.hs main-is: Spec.hs
other-modules: other-modules:
Hamming.App.Types.AppModeSpec
Hamming.App.TypesSpec
Hamming.App.Widgets.InternalSpec Hamming.App.Widgets.InternalSpec
Hamming.App.WidgetsSpec Hamming.App.WidgetsSpec
Hamming.AppSpec Hamming.AppSpec

View File

@ -24,7 +24,7 @@ License along with this program. If not, see
|-} |-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE LambdaCase, TemplateHaskell #-}
module Hamming.App.Types ( module Hamming.App.Types (
-- * Application state data -- * Application state data
@ -35,6 +35,8 @@ module Hamming.App.Types (
hammingCode, hammingCode,
-- ** AppMode -- ** AppMode
AppMode (..), AppMode (..),
-- *** Lenses
editStateL,
-- ** EditState, -- ** EditState,
EditState (..), EditState (..),
-- *** Lenses -- *** Lenses
@ -52,18 +54,20 @@ module Hamming.App.Types (
import Brick.Types (BrickEvent, EventM) import Brick.Types (BrickEvent, EventM)
import Data.Word (Word16) import Data.Word (Word16)
import Lens.Micro (Lens', lens)
import Lens.Micro.TH (makeLenses) import Lens.Micro.TH (makeLenses)
-- | The main state of the application -- | The main state of the application
data AppState = AppState data AppState = AppState
{ _appMode :: AppMode { _appMode :: AppMode
, _hammingCode :: Word16 , _hammingCode :: Word16
} } deriving (Eq, Show)
-- | The application's main mode -- | The application's main mode
data AppMode data AppMode
= DisplayMode = DisplayMode
| EditMode EditState | EditMode EditState
deriving (Eq, Show)
-- | The state of the editor -- | The state of the editor
data EditState = EditState data EditState = EditState
@ -71,7 +75,7 @@ data EditState = EditState
-- ^ The selected row number -- ^ The selected row number
, _colNum :: Int , _colNum :: Int
-- ^ The selected column -- ^ The selected column
} } deriving (Eq, Show)
-- | Identifies a resource -- | Identifies a resource
type ResName = () type ResName = ()
@ -90,6 +94,17 @@ concat <$> mapM makeLenses
, ''EditState , ''EditState
] ]
editStateL :: Lens' AppMode (Maybe EditState)
editStateL = lens
( \case
DisplayMode -> Nothing
EditMode s -> Just s
)
( const $ \case
Just s -> EditMode s
Nothing -> DisplayMode
)
-- | Initial application state -- | Initial application state
initialState :: AppState initialState :: AppState
initialState = AppState DisplayMode 0 initialState = AppState DisplayMode 0

View File

@ -0,0 +1,66 @@
{-
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.Types.AppModeSpec (spec) where
import Lens.Micro ((^.), (&), (.~))
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Hamming.App.Types
spec :: Spec
spec = describe "AppMode"
editStateLSpec
editStateLSpec :: Spec
editStateLSpec = describe "editStateL" $ do
eslSetterSpec
eslGetterSpec
eslSetterSpec :: Spec
eslSetterSpec = context "setter" $ mapM_
( \(desc, mode, val, expected) -> context desc $ let
actual = mode & editStateL .~ val
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "set from display", DisplayMode, Just s1, EditMode s1 )
, ( "clear from display", DisplayMode, Nothing, DisplayMode )
, ( "set from edit", EditMode s1, Just s2, EditMode s2 )
, ( "clear from edit", EditMode s1, Nothing, DisplayMode )
]
where
s1 = EditState 2 3
s2 = EditState 3 5
eslGetterSpec :: Spec
eslGetterSpec = context "getter" $ mapM_
( \(desc, mode, expected) -> context desc $ let
actual = mode^.editStateL
in it ("should be " ++ show expected) $
actual `shouldBe` expected
)
[ ( "no editor", DisplayMode, Nothing )
, ( "with editor", EditMode s, Just s )
]
where s = EditState 2 3
--jl

View File

@ -0,0 +1,32 @@
{-
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.TypesSpec (spec) where
import Test.Hspec (Spec, describe)
import qualified Hamming.App.Types.AppModeSpec as AppMode
spec :: Spec
spec = describe "Types"
AppMode.spec
--jl

View File

@ -23,10 +23,12 @@ module Hamming.AppSpec (spec) where
import Test.Hspec (Spec, describe) import Test.Hspec (Spec, describe)
import qualified Hamming.App.TypesSpec as Types
import qualified Hamming.App.WidgetsSpec as Widgets import qualified Hamming.App.WidgetsSpec as Widgets
spec :: Spec spec :: Spec
spec = describe "App" spec = describe "App" $ do
Types.spec
Widgets.spec Widgets.spec
--jl --jl