better lens for fetching EditState
from AppMode
This commit is contained in:
parent
408e0f0f82
commit
36bc32a688
|
@ -67,8 +67,6 @@ test-suite hamming-test
|
|||
type: exitcode-stdio-1.0
|
||||
main-is: Spec.hs
|
||||
other-modules:
|
||||
Hamming.App.Types.AppModeSpec
|
||||
Hamming.App.TypesSpec
|
||||
Hamming.App.Widgets.InternalSpec
|
||||
Hamming.App.WidgetsSpec
|
||||
Hamming.AppSpec
|
||||
|
|
|
@ -36,7 +36,7 @@ module Hamming.App.Types (
|
|||
-- ** AppMode
|
||||
AppMode (..),
|
||||
-- *** Lenses
|
||||
editStateL,
|
||||
editState,
|
||||
-- ** EditState,
|
||||
EditState (..),
|
||||
-- *** Lenses
|
||||
|
@ -54,7 +54,6 @@ module Hamming.App.Types (
|
|||
|
||||
import Brick.Types (BrickEvent, EventM)
|
||||
import Data.Word (Word16)
|
||||
import Lens.Micro (Lens', lens)
|
||||
import Lens.Micro.TH (makeLenses)
|
||||
|
||||
-- | The main state of the application
|
||||
|
@ -66,7 +65,9 @@ data AppState = AppState
|
|||
-- | The application's main mode
|
||||
data AppMode
|
||||
= DisplayMode
|
||||
| EditMode EditState
|
||||
| EditMode
|
||||
{ _editState :: EditState
|
||||
}
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | The state of the editor
|
||||
|
@ -91,20 +92,10 @@ type Handler = Event -> Action ()
|
|||
|
||||
concat <$> mapM makeLenses
|
||||
[ ''AppState
|
||||
, ''AppMode
|
||||
, ''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
|
||||
initialState :: AppState
|
||||
initialState = AppState DisplayMode 0
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -23,12 +23,10 @@ module Hamming.AppSpec (spec) where
|
|||
|
||||
import Test.Hspec (Spec, describe)
|
||||
|
||||
import qualified Hamming.App.TypesSpec as Types
|
||||
import qualified Hamming.App.WidgetsSpec as Widgets
|
||||
|
||||
spec :: Spec
|
||||
spec = describe "App" $ do
|
||||
Types.spec
|
||||
Widgets.spec
|
||||
|
||||
--jl
|
||||
|
|
Loading…
Reference in New Issue
Block a user