|
| 1 | +-- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io/> |
| 2 | +-- |
| 3 | +-- SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +module Test.Lens where |
| 6 | + |
| 7 | +import Test.Tasty (TestTree, testGroup) |
| 8 | +import Test.Tasty.HUnit (testCase, (@?=)) |
| 9 | + |
| 10 | +import Text.Interpolation.Nyan.Lens |
| 11 | + |
| 12 | +data Pair = Pair { first :: Int, second :: String } |
| 13 | + deriving stock (Show, Eq) |
| 14 | + |
| 15 | +$(makeLenses ''Pair) |
| 16 | + |
| 17 | +newtype Single = Single { value :: Maybe String } |
| 18 | + deriving stock (Show, Eq) |
| 19 | + |
| 20 | +$(makeLenses ''Single) |
| 21 | + |
| 22 | +test_makeLenses :: TestTree |
| 23 | +test_makeLenses = testGroup "Lenses produced by 'makeLenses' work as expected" |
| 24 | + [ testGroup "Basic lenses operators work as expected" |
| 25 | + [ testCase "(^.) operator works" do |
| 26 | + (pair ^. firstL, pair ^. secondL) |
| 27 | + @?= (100, "Hundred") |
| 28 | + |
| 29 | + , testCase "(%~) operator works" do |
| 30 | + pair & (firstL %~ (+ 1)) & (secondL %~ (<> " and one")) |
| 31 | + @?= Pair 101 "Hundred and one" |
| 32 | + |
| 33 | + , testCase "(.~) operator works" do |
| 34 | + pair & (firstL .~ 102) & (secondL .~ "Hundred and two") |
| 35 | + @?= Pair 102 "Hundred and two" |
| 36 | + |
| 37 | + , testCase "(?~) operator works" do |
| 38 | + single & (valueL ?~ "Some value") |
| 39 | + @?= Single (Just "Some value") |
| 40 | + ] |
| 41 | + |
| 42 | + , testGroup "Operators leveraging 'MonadState', 'State' work as expected" |
| 43 | + [ testCase "(&~) and (.=) work" do |
| 44 | + (pair &~ do firstL .= 102) |
| 45 | + @?= Pair 102 "Hundred" |
| 46 | + |
| 47 | + , testCase "(&~) and (?=) work" do |
| 48 | + (single &~ do valueL ?= "Some value") |
| 49 | + @?= Single (Just "Some value") |
| 50 | + |
| 51 | + , testCase "(&~) and (%=) work" do |
| 52 | + (single &~ do valueL %= (const $ Just "Some value")) |
| 53 | + @?= Single (Just "Some value") |
| 54 | + ] |
| 55 | + ] |
| 56 | + where |
| 57 | + a & f = f a |
| 58 | + pair = Pair 100 "Hundred" |
| 59 | + single = Single Nothing |
0 commit comments