55module KDL.Decoder.ArrowSpec (spec ) where
66
77import Control.Arrow (returnA )
8- import Control.Monad (forM_ , unless , when )
8+ import Control.Monad (forM_ , unless )
99import Data.Int (Int64 )
1010import Data.Map qualified as Map
1111import Data.Proxy (Proxy (.. ))
1212import Data.Text (Text )
1313import Data.Text qualified as Text
1414import Data.Typeable (typeRep )
1515import KDL.Arrow qualified as KDL
16+ import KDL.TestUtils.Error (decodeErrorMsg )
1617import KDL.Types (
1718 Entry (.. ),
1819 Identifier (.. ),
@@ -22,12 +23,6 @@ import KDL.Types (
2223 ValueData (.. ),
2324 )
2425import Skeletest
25- import Skeletest.Predicate qualified as P
26-
27- decodeErrorMsg :: [Text ] -> Predicate IO (Either KDL. DecodeError a )
28- decodeErrorMsg msgs = P. left (KDL. renderDecodeError P. >>> P. eq msg)
29- where
30- msg = Text. intercalate " \n " msgs
3126
3227spec :: Spec
3328spec = do
@@ -38,49 +33,6 @@ spec = do
3833
3934apiSpec :: Spec
4035apiSpec = do
41- describe " decodeWith" $ do
42- it " fails with helpful error if parsing fails" $ do
43- let config = " foo hello= 123"
44- decoder = KDL. document $ KDL. node @ Node " foo"
45- KDL. decodeWith decoder config
46- `shouldSatisfy` decodeErrorMsg
47- [ " At: <root>"
48- , " 1:10:"
49- , " |"
50- , " 1 | foo hello= 123"
51- , " | ^^"
52- , " unexpected \" = \" "
53- , " expecting Node Child, Node Space, or Node Terminator"
54- ]
55-
56- it " fails with user-defined error" $ do
57- let config = " foo -1"
58- decoder =
59- KDL. document . KDL. argAtWith " foo" $
60- KDL. withDecoder KDL. number $ \ x -> do
61- when (x < 0 ) $ do
62- KDL. failM $ " Got negative number: " <> (Text. pack . show ) x
63- pure x
64- KDL. decodeWith decoder config
65- `shouldSatisfy` decodeErrorMsg
66- [ " At: foo #0 > arg #0"
67- , " Got negative number: -1.0"
68- ]
69-
70- it " shows context in deeply nested error" $ do
71- let config = " foo; foo { bar { baz; baz; baz; baz a=1; }; }"
72- decoder =
73- KDL. document
74- . (KDL. many . KDL. nodeWith " foo" . KDL. children)
75- . (KDL. many . KDL. nodeWith " bar" . KDL. children)
76- . (KDL. many . KDL. nodeWith " baz" )
77- $ KDL. optional (KDL. prop @ Text " a" )
78- KDL. decodeWith decoder config
79- `shouldSatisfy` decodeErrorMsg
80- [ " At: foo #1 > bar #0 > baz #3 > prop a"
81- , " Expected text, got: 1"
82- ]
83-
8436 describe " NodeListDecoder" $ do
8537 describe " node" $ do
8638 it " decodes a node" $ do
0 commit comments