From 36bc32a68873268fb12092f64c347675d4bc3781 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 27 Jul 2024 16:06:30 -0400 Subject: [PATCH] better lens for fetching `EditState` from `AppMode` --- hamming.cabal | 2 - src/Hamming/App/Types.hs | 19 ++------ test/Hamming/App/Types/AppModeSpec.hs | 66 --------------------------- test/Hamming/App/TypesSpec.hs | 32 ------------- test/Hamming/AppSpec.hs | 2 - 5 files changed, 5 insertions(+), 116 deletions(-) delete mode 100644 test/Hamming/App/Types/AppModeSpec.hs delete mode 100644 test/Hamming/App/TypesSpec.hs diff --git a/hamming.cabal b/hamming.cabal index 2e90d74..c3bfecc 100644 --- a/hamming.cabal +++ b/hamming.cabal @@ -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 diff --git a/src/Hamming/App/Types.hs b/src/Hamming/App/Types.hs index 909d427..e26ac98 100644 --- a/src/Hamming/App/Types.hs +++ b/src/Hamming/App/Types.hs @@ -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 diff --git a/test/Hamming/App/Types/AppModeSpec.hs b/test/Hamming/App/Types/AppModeSpec.hs deleted file mode 100644 index 2090d59..0000000 --- a/test/Hamming/App/Types/AppModeSpec.hs +++ /dev/null @@ -1,66 +0,0 @@ -{- - -hamming -Copyright (C) Jonathan Lamothe - -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 -. - --} - -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 diff --git a/test/Hamming/App/TypesSpec.hs b/test/Hamming/App/TypesSpec.hs deleted file mode 100644 index bf91b2a..0000000 --- a/test/Hamming/App/TypesSpec.hs +++ /dev/null @@ -1,32 +0,0 @@ -{- - -hamming -Copyright (C) Jonathan Lamothe - -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 -. - --} - -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 diff --git a/test/Hamming/AppSpec.hs b/test/Hamming/AppSpec.hs index a285cdf..07543a6 100644 --- a/test/Hamming/AppSpec.hs +++ b/test/Hamming/AppSpec.hs @@ -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