better lens for fetching EditState from AppMode

This commit is contained in:
Jonathan Lamothe 2024-07-27 16:06:30 -04:00
parent 408e0f0f82
commit 36bc32a688
5 changed files with 5 additions and 116 deletions

View File

@ -67,8 +67,6 @@ 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

@ -36,7 +36,7 @@ module Hamming.App.Types (
-- ** AppMode -- ** AppMode
AppMode (..), AppMode (..),
-- *** Lenses -- *** Lenses
editStateL, editState,
-- ** EditState, -- ** EditState,
EditState (..), EditState (..),
-- *** Lenses -- *** Lenses
@ -54,7 +54,6 @@ 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
@ -66,7 +65,9 @@ data AppState = AppState
-- | The application's main mode -- | The application's main mode
data AppMode data AppMode
= DisplayMode = DisplayMode
| EditMode EditState | EditMode
{ _editState :: EditState
}
deriving (Eq, Show) deriving (Eq, Show)
-- | The state of the editor -- | The state of the editor
@ -91,20 +92,10 @@ type Handler = Event -> Action ()
concat <$> mapM makeLenses concat <$> mapM makeLenses
[ ''AppState [ ''AppState
, ''AppMode
, ''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

@ -1,66 +0,0 @@
{-
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

@ -1,32 +0,0 @@
{-
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,12 +23,10 @@ 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" $ do spec = describe "App" $ do
Types.spec
Widgets.spec Widgets.spec
--jl --jl