-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTemplate.hs
More file actions
1125 lines (1015 loc) · 39 KB
/
Template.hs
File metadata and controls
1125 lines (1015 loc) · 39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE CPP #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
-- Apostrophes confuse CPP macros
#define _APOS_(x) x'
#ifdef IS_MONAD
#define _DO_ do
#define _RETURN_(x) pure x
#define _STMT_(x) x
#endif
#ifdef IS_ARROW
#define _DO_ proc () -> do
#define _RETURN_(x) returnA -< x
#define _STMT_(x) x -< ()
#endif
#ifdef IS_ARROW
{-# LANGUAGE Arrows #-}
#endif
module KDL.Decoder.SharedSpec.MODULE_NAME (
apiSpec,
decodeNodeSpec,
decodeValueSpec,
) where
import Control.Monad (forM_, unless)
import Data.Map qualified as Map
import Data.Text (Text)
import Data.Text qualified as Text
import KDL.TestUtils.AST (scrubFormat)
import KDL.TestUtils.Error (decodeErrorMsg)
import KDL.Types (
Entry (..),
Identifier (..),
Node (..),
NodeList (..),
Value (..),
ValueData (..),
)
import Skeletest
#ifdef IS_MONAD
import KDL qualified
#endif
#ifdef IS_ARROW
import Control.Arrow (returnA)
import KDL.Arrow qualified as KDL
#endif
apiSpec :: Spec
apiSpec = do
describe "NodeListDecoder" $ do
describe "node" $ do
it "decodes a node" $ do
let config = "foo 1.0"
decoder = KDL.document $ _DO_
_STMT_(scrubFormat <$> KDL.node "foo")
expected =
Node
{ ann = Nothing
, name = Identifier{value = "foo", ext = KDL.def}
, entries =
[ Entry
{ name = Nothing
, value = Value{ann = Nothing, data_ = Number 1.0, ext = KDL.def}
, ext = KDL.def
}
]
, children = Nothing
, ext = KDL.def
}
KDL.decodeWith decoder config `shouldBe` Right expected
it "decodes multiple nodes" $ do
let config = "foo; foo"
decoder = KDL.document $ _DO_
_STMT_(fmap (map scrubFormat) . KDL.many $ KDL.node "foo")
expected = [fooNode, fooNode]
fooNode =
Node
{ ann = Nothing
, name = Identifier{value = "foo", ext = KDL.def}
, entries = []
, children = Nothing
, ext = KDL.def
}
KDL.decodeWith decoder config `shouldBe` Right expected
it "decodes nodes in any order" $ do
let config = "foo; bar"
decoder = KDL.document $ _DO_
bar <- _STMT_(scrubFormat <$> KDL.node "bar")
foo <- _STMT_(scrubFormat <$> KDL.node "foo")
_RETURN_((bar, foo))
expected = (node "bar", node "foo")
node name =
Node
{ ann = Nothing
, name = Identifier{value = name, ext = KDL.def}
, entries = []
, children = Nothing
, ext = KDL.def
}
KDL.decodeWith decoder config `shouldBe` Right expected
it "fails when not enough nodes" $ do
let config = "foo"
decoder = KDL.document $ _DO_
foo1 <- _STMT_(scrubFormat <$> KDL.node @Node "foo")
foo2 <- _STMT_(scrubFormat <$> KDL.node @Node "foo")
_RETURN_((foo1, foo2))
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: <root>"
, " Expected another node: foo"
]
-- Most behaviors tested with `node`
describe "nodeWith" $ do
it "decodes a node" $ do
let config = "foo 1.0 { hello world; }"
decodeFoo = _DO_
arg <- _STMT_(KDL.arg @Int)
child <- _STMT_(KDL.children $ KDL.argAt @Text "hello")
_RETURN_((arg, child))
decoder = KDL.document $ _DO_
_STMT_(KDL.nodeWith "foo" decodeFoo)
KDL.decodeWith decoder config `shouldBe` Right (1, "world")
it "fails when node fails to parse" $ do
let config = "foo 1.0"
decodeFoo = _DO_
_STMT_(KDL.arg @Text)
decoder = KDL.document $ _DO_
_STMT_(KDL.nodeWith "foo" decodeFoo)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected string, got: 1.0"
]
-- Most behaviors tested with `nodeWith`
describe "nodeWith'" $ do
it "decodes a node with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "(test)foo 1"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.nodeWith) "foo" anns $ KDL.arg @Int)
KDL.decodeWith decoder config `shouldBe` Right 1
it "decodes a node without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.nodeWith) "foo" anns $ KDL.arg @Int)
KDL.decodeWith decoder config `shouldBe` Right 1
it "fails when node has unexpected annotation" $ do
let config = "(test)foo 2"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.nodeWith) "foo" ["FOO"] $ KDL.arg @Int)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Expected annotation to be one of [\"FOO\"], got: test"
]
describe "remainingNodes" $ do
it "returns all remaining nodes" $ do
let config = "foo 1.0; foo 2.0; bar"
decoder = KDL.document $ _DO_
_ <- _STMT_(KDL.node @Node "foo")
_STMT_(fmap (map scrubFormat) <$> KDL.remainingNodes)
expected =
Map.fromList
[ ("foo", [fooNode2])
, ("bar", [barNode])
]
fooNode2 =
Node
{ ann = Nothing
, name = Identifier{value = "foo", ext = KDL.def}
, entries =
[ Entry
{ name = Nothing
, value = Value{ann = Nothing, data_ = Number 2.0, ext = KDL.def}
, ext = KDL.def
}
]
, children = Nothing
, ext = KDL.def
}
barNode =
Node
{ ann = Nothing
, name = Identifier{value = "bar", ext = KDL.def}
, entries = []
, children = Nothing
, ext = KDL.def
}
KDL.decodeWith decoder config `shouldBe` Right expected
-- Most behaviors tested with `remainingNodes`
describe "remainingNodesWith" $ do
it "returns all remaining nodes" $ do
let config = "foo 1.0; foo 2.0; bar"
decodeNode = _DO_
_STMT_(KDL.optional $ KDL.arg @Int)
decoder = KDL.document $ _DO_
_ <- _STMT_(KDL.node @Node "foo")
_STMT_(KDL.remainingNodesWith decodeNode)
expected =
Map.fromList
[ ("foo", [Just 2])
, ("bar", [Nothing])
]
KDL.decodeWith decoder config `shouldBe` Right expected
it "fails when node fails to parse" $ do
let config = "foo 1; bar 1; bar hello"
decodeNode = _DO_
_STMT_(KDL.arg @Int)
decoder = KDL.document $ _DO_
_STMT_(KDL.remainingNodesWith decodeNode)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: bar #1 > arg #0"
, " Expected number, got: hello"
]
-- Most behaviors tested with `remainingNodesWith`
describe "remainingNodesWith'" $ do
it "decodes a node with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "(test)foo 1"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.remainingNodesWith) anns $ KDL.arg @Int)
KDL.decodeWith decoder config
`shouldBe` (Right . Map.fromList) [("foo", [1])]
it "decodes a node without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.remainingNodesWith) anns $ KDL.arg @Int)
KDL.decodeWith decoder config
`shouldBe` (Right . Map.fromList) [("foo", [1])]
it "fails when node has unexpected annotation" $ do
let config = "(FOO)foo 1; (test)foo 2"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.remainingNodesWith) ["FOO"] $ KDL.arg @Int)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #1"
, " Expected annotation to be one of [\"FOO\"], got: test"
]
describe "argAt" $ do
it "gets argument at a node" $ do
let config = "foo bar; hello world"
decoder = KDL.document $ _DO_
hello <- _STMT_(KDL.argAt @Text "hello")
foo <- _STMT_(KDL.argAt @Text "foo")
_RETURN_((hello, foo))
KDL.decodeWith decoder config `shouldBe` Right ("world", "bar")
it "fails if no node" $ do
let config = "other_node"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAt @Int "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: <root>"
, " Expected node: foo"
]
it "fails if node has no args" $ do
let config = "foo"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAt @Int "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Expected arg #0"
]
it "fails if arg fails to parse" $ do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAt @Text "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected string, got: 1"
]
-- Most behaviors tested with `argAt`
describe "argAtWith" $ do
it "gets argument at a node" $ do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" $ show . (* 10) <$> KDL.number)
KDL.decodeWith decoder config `shouldBe` Right "10.0"
-- Most behaviors tested with `argAtWith`
describe "argAtWith'" $ do
it "decodes argument with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo (test)a"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.argAtWith) "foo" anns KDL.string)
KDL.decodeWith decoder config `shouldBe` Right "a"
it "decodes argument without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.argAtWith) "foo" anns KDL.string)
KDL.decodeWith decoder config `shouldBe` Right "a"
it "fails when argument has unexpected annotation" $ do
let config = "foo (test)a"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.argAtWith) "foo" ["VAL"] KDL.string)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected annotation to be one of [\"VAL\"], got: test"
]
describe "argsAt" $ do
it "gets arguments at a node" $ do
let config = "foo 1 2 3"
decoder = KDL.document $ _DO_
_STMT_(KDL.argsAt @Int "foo")
KDL.decodeWith decoder config `shouldBe` Right [1, 2, 3]
it "returns empty list if no node" $ do
let config = ""
decoder = KDL.document $ _DO_
_STMT_(KDL.argsAt @Int "foo")
KDL.decodeWith decoder config `shouldBe` Right []
it "returns empty list if node has no args" $ do
let config = "foo"
decoder = KDL.document $ _DO_
_STMT_(KDL.argsAt @Int "foo")
KDL.decodeWith decoder config `shouldBe` Right []
it "fails if any arg fails to parse" $ do
let config = "foo 1 asdf"
decoder = KDL.document $ _DO_
_STMT_(KDL.argsAt @Int "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #1"
, " Expected number, got: asdf"
]
-- Most behaviors tested with `argsAt`
describe "argsAtWith" $ do
it "gets arguments at a node" $ do
let config = "foo 1 2"
decoder = KDL.document $ _DO_
_STMT_(KDL.argsAtWith "foo" $ show . (* 10) <$> KDL.number)
KDL.decodeWith decoder config `shouldBe` Right ["10.0", "20.0"]
-- Most behaviors tested with `argsAtWith`
describe "argsAtWith'" $ do
it "decodes arguments with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo (test)a (test)b"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.argsAtWith) "foo" anns KDL.string)
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]
it "decodes arguments without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a b"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.argsAtWith) "foo" anns KDL.string)
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]
it "fails when argument has unexpected annotation" $ do
let config = "foo (VAL)a (test)b"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.argsAtWith) "foo" ["VAL"] KDL.string)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #1"
, " Expected annotation to be one of [\"VAL\"], got: test"
]
describe "dashChildrenAt" $ do
it "gets dash children at a node" $ do
let config = "foo { - 1; - 2; - 3; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAt @Int "foo")
KDL.decodeWith decoder config `shouldBe` Right [1, 2, 3]
it "returns empty list if no node" $ do
let config = ""
decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAt @Int "foo")
KDL.decodeWith decoder config `shouldBe` Right []
it "returns empty list if node has no dash children" $ do
forM_ ["foo", "foo {}"] $ \config -> do
let decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAt @Int "foo")
KDL.decodeWith decoder config `shouldBe` Right []
it "fails if dash children have multiple args" $ do
let config = "foo { - 1 2; - 3 4; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAt @Int "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > - #0"
, " Unexpected arg #1: 2"
]
it "fails if node has non-dash children" $ do
let config = "foo { - 1; bar 1 2 3; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAt @Int "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Unexpected node: bar #0"
, " Expected another node: -"
]
it "fails if any child fails to parse" $ do
let config = "foo { - 1; - asdf; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAt @Int "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > - #1 > arg #0"
, " Expected number, got: asdf"
]
-- Most behaviors tested with `dashChildrenAt`
describe "dashChildrenAtWith" $ do
it "gets dash children at a node" $ do
let config = "foo { - 1; - 2; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashChildrenAtWith "foo" $ show . (* 10) <$> KDL.number)
KDL.decodeWith decoder config `shouldBe` Right ["10.0", "20.0"]
-- Most behaviors tested with `dashChildrenAtWith`
describe "dashChildrenAtWith'" $ do
it "decodes dash children with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo { - (test)a; - (test)b; }"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.dashChildrenAtWith) "foo" anns KDL.string)
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]
it "decodes dash children without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo { - a; - b; }"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.dashChildrenAtWith) "foo" anns KDL.string)
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]
it "fails when child has unexpected annotation" $ do
let config = "foo { - (test)a; }"
decoder = KDL.document $ _DO_
_STMT_(_APOS_(KDL.dashChildrenAtWith) "foo" ["VAL"] KDL.string)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > - #0 > arg #0"
, " Expected annotation to be one of [\"VAL\"], got: test"
]
describe "dashNodesAt" $ do
it "gets dash nodes at a node" $ do
let config = "foo { - { bar; }; - { baz; }; }"
decoder = KDL.document $ _DO_
_STMT_(map scrubFormat <$> KDL.dashNodesAt "foo")
expected = [node "-" [node "bar" []], node "-" [node "baz" []]]
node name children =
Node
{ ann = Nothing
, name = Identifier{value = name, ext = KDL.def}
, entries = []
, children =
if null children
then Nothing
else Just NodeList{nodes = children, ext = KDL.def}
, ext = KDL.def
}
KDL.decodeWith decoder config `shouldBe` Right expected
it "returns empty list if no node" $ do
let config = ""
decoder = KDL.document $ _DO_
_STMT_(KDL.dashNodesAt @Node "foo")
KDL.decodeWith decoder config `shouldBe` Right []
it "returns empty list if node has no dash nodes" $ do
forM_ ["foo", "foo {}"] $ \config -> do
let decoder = KDL.document $ _DO_
_STMT_(KDL.dashNodesAt @Node "foo")
KDL.decodeWith decoder config `shouldBe` Right []
it "fails if node has non-dash nodes" $ do
let config = "foo { - 1; bar 1 2 3; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashNodesAt @Node "foo")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Unexpected node: bar #0"
, " Expected another node: -"
]
-- Most behaviors tested with `dashNodesAt`
describe "dashNodesAtWith" $ do
it "gets dash nodes at a node" $ do
let config = "foo { - 1 { bar hello; }; - 2 { bar world; }; }"
decodeChild = _DO_
arg <- _STMT_(KDL.arg @Int)
child <- _STMT_(KDL.children $ KDL.nodeWith "bar" $ KDL.arg @Text)
_RETURN_((arg, child))
decoder = KDL.document $ _DO_
_STMT_(KDL.dashNodesAtWith "foo" decodeChild)
KDL.decodeWith decoder config `shouldBe` Right [(1, "hello"), (2, "world")]
it "fails if any child fails to parse" $ do
let config = "foo { - { bar 1; }; - { bar test; }; }"
decoder = KDL.document $ _DO_
_STMT_(KDL.dashNodesAtWith "foo" $ KDL.children $ KDL.argAt @Int "bar")
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > - #1 > bar #0 > arg #0"
, " Expected number, got: test"
]
describe "NodeDecoder" $ do
let decodeNode name decoder config =
KDL.decodeWith
( KDL.document $ _DO_
_STMT_(KDL.nodeWith name decoder)
)
config
describe "arg" $ do
it "decodes an argument" $ do
let config = "foo 1 bar"
decoder = _DO_
arg1 <- _STMT_(KDL.arg @Int)
arg2 <- _STMT_(KDL.arg @Text)
_RETURN_((arg1, arg2))
decodeNode "foo" decoder config `shouldBe` Right (1, "bar")
it "decodes multiple arguments" $ do
let config = "foo 1 2 3"
decoder = _DO_
_STMT_(KDL.many $ KDL.arg @Int)
decodeNode "foo" decoder config `shouldBe` Right [1, 2, 3]
it "fails if argument doesn't exist" $ do
let config = "foo"
decoder = _DO_
_STMT_(KDL.arg @Int)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Expected arg #0"
]
it "fails if argument fails to parse" $ do
let config = "foo test"
decoder = _DO_
_STMT_(KDL.arg @Int)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected number, got: test"
]
it "fails if not all arguments are decoded" $ do
let config = "foo 1 2 3"
decoder = _DO_
_STMT_(KDL.arg @Int)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Unexpected arg #1: 2"
]
-- Most behaviors tested with `arg`
describe "argWith" $ do
it "decodes an argument" $ do
let config = "foo bar"
decoder = _DO_
_STMT_(KDL.argWith KDL.string)
decodeNode "foo" decoder config `shouldBe` Right "bar"
-- Most behaviors tested with `argWith`
describe "argWith'" $ do
it "decodes argument with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo (test)a"
decoder = _DO_
_STMT_(_APOS_(KDL.argWith) anns KDL.string)
decodeNode "foo" decoder config `shouldBe` Right "a"
it "decodes argument without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a"
decoder = _DO_
_STMT_(_APOS_(KDL.argWith) anns KDL.string)
decodeNode "foo" decoder config `shouldBe` Right "a"
it "fails when argument has unexpected annotation" $ do
let config = "foo (test)a"
decoder = _DO_
_STMT_(_APOS_(KDL.argWith) ["VAL"] KDL.string)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected annotation to be one of [\"VAL\"], got: test"
]
it "supports backtracking annotations" $ do
let config = "foo (l)1 (r)2"
decodeArg =
KDL.oneOf
[ Left <$> KDL.argWith' ["l"] KDL.number
, Right <$> KDL.argWith' ["r"] KDL.number
]
decoder = KDL.document $ _DO_
_STMT_(KDL.nodeWith "foo" . KDL.many $ decodeArg)
KDL.decodeWith decoder config `shouldBe` Right [Left 1, Right 2]
describe "prop" $ do
it "decodes a prop" $ do
let config = "foo test1=1 test2=hello"
decoder = _DO_
prop1 <- _STMT_(KDL.prop @Text "test2")
prop2 <- _STMT_(KDL.prop @Int "test1")
_RETURN_((prop1, prop2))
decodeNode "foo" decoder config `shouldBe` Right ("hello", 1)
it "can optionally decode a prop" $ do
let config = "foo a=1"
decoder = _DO_
a <- _STMT_(KDL.optional $ KDL.prop @Int "a")
b <- _STMT_(KDL.optional $ KDL.prop @Int "b")
_RETURN_((a, b))
decodeNode "foo" decoder config `shouldBe` Right (Just 1, Nothing)
it "decodes last prop" $ do
let config = "foo test=1 test=2"
decoder = _DO_
_STMT_(KDL.prop @Int "test")
decodeNode "foo" decoder config `shouldBe` Right 2
it "fails if prop doesn't exist" $ do
let config = "foo 123"
decoder = _DO_
_STMT_(KDL.prop @Int "test")
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Expected prop: test"
]
it "fails if prop fails to parse" $ do
let config = "foo hello=world"
decoder = _DO_
_STMT_(KDL.prop @Int "hello")
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > prop hello"
, " Expected number, got: world"
]
it "fails if not all props are decoded" $ do
let config = "foo a=1 b=2"
decoder = _DO_
_STMT_(KDL.prop @Int "a")
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Unexpected prop: b=2"
]
-- Most behaviors tested with `prop`
describe "propWith" $ do
it "decodes a prop" $ do
let config = "foo a=1"
decoder = _DO_
_STMT_(KDL.propWith "a" KDL.number)
decodeNode "foo" decoder config `shouldBe` Right 1
-- Most behaviors tested with `propWith`
describe "propWith'" $ do
it "decodes prop with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a=(test)1"
decoder = _DO_
_STMT_(_APOS_(KDL.propWith) "a" anns KDL.number)
decodeNode "foo" decoder config `shouldBe` Right 1
it "decodes prop without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a=1"
decoder = _DO_
_STMT_(_APOS_(KDL.propWith) "a" anns KDL.number)
decodeNode "foo" decoder config `shouldBe` Right 1
it "fails when prop has unexpected annotation" $ do
let config = "foo a=(test)1"
decoder = _DO_
_STMT_(_APOS_(KDL.propWith) "a" ["VAL"] KDL.number)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > prop a"
, " Expected annotation to be one of [\"VAL\"], got: test"
]
describe "remainingProps" $ do
it "decodes remaining props" $ do
let config = "foo a=1 b=2 c=3 b=4"
decoder = _DO_
_ <- _STMT_(KDL.prop @Int "a")
_STMT_(KDL.remainingProps @Int)
decodeNode "foo" decoder config
`shouldBe` (Right . Map.fromList) [("b", 4), ("c", 3)]
it "returns empty map if no props left" $ do
let config = "foo a=1"
decoder = _DO_
_ <- _STMT_(KDL.prop @Int "a")
_STMT_(KDL.remainingProps @Int)
decodeNode "foo" decoder config `shouldBe` Right Map.empty
it "fails if prop fails to parse" $ do
let config = "foo a=1 b=1 c=2 c=test"
decoder = _DO_
_ <- _STMT_(KDL.prop @Int "a")
_STMT_(KDL.remainingProps @Int)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > prop c"
, " Expected number, got: test"
]
-- Most behaviors tested with `remainingProps`
describe "remainingPropsWith" $ do
it "decodes remaining props" $ do
let config = "foo a=1 b=2 c=3 b=4"
decoder = _DO_
_ <- _STMT_(KDL.prop @Int "a")
_STMT_(KDL.remainingPropsWith KDL.number)
decodeNode "foo" decoder config
`shouldBe` (Right . Map.fromList) [("b", 4), ("c", 3)]
-- Most behaviors tested with `remainingPropsWith`
describe "remainingPropsWith'" $ do
it "decodes props with an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a=(test)1 b=(test)2"
decoder = _DO_
_STMT_(_APOS_(KDL.remainingPropsWith) anns KDL.number)
decodeNode "foo" decoder config
`shouldBe` (Right . Map.fromList) [("a", 1), ("b", 2)]
it "decodes props without an annotation" $ do
let testCases =
[ []
, ["test"]
, ["test", "other"]
]
forM_ testCases $ \anns -> do
let config = "foo a=1 b=2"
decoder = _DO_
_STMT_(_APOS_(KDL.remainingPropsWith) anns KDL.number)
decodeNode "foo" decoder config
`shouldBe` (Right . Map.fromList) [("a", 1), ("b", 2)]
it "fails when prop has unexpected annotation" $ do
let config = "foo a=(VAL)1 b=(test)2"
decoder = _DO_
_STMT_(_APOS_(KDL.remainingPropsWith) ["VAL"] KDL.number)
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > prop b"
, " Expected annotation to be one of [\"VAL\"], got: test"
]
describe "children" $ do
it "decodes children" $ do
let config = "foo { bar test; }"
decoder = _DO_
_STMT_(fmap scrubFormat . KDL.children $ KDL.node @Node "bar")
expected =
Node
{ ann = Nothing
, name = Identifier{value = "bar", ext = KDL.def}
, entries =
[ Entry
{ name = Nothing
, value = Value{ann = Nothing, data_ = String "test", ext = KDL.def}
, ext = KDL.def
}
]
, children = Nothing
, ext = KDL.def
}
decodeNode "foo" decoder config `shouldBe` Right expected
it "can be re-entered" $ do
let config = "foo { bar a; baz b; }"
decoder = _DO_
arg1 <- _STMT_(KDL.children $ KDL.argAt @Text "bar")
arg2 <- _STMT_(KDL.children $ KDL.argAt @Text "baz")
_RETURN_((arg1, arg2))
decodeNode "foo" decoder config `shouldBe` Right ("a", "b")
it "fails if not all children are decoded" $ do
let config = "foo { asdf; bar; }"
decoder = _DO_
_STMT_(KDL.children $ KDL.node @Node "bar")
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0"
, " Unexpected node: asdf #0"
]
describe "ValueDecoder" $ do
describe "any" $ do
it "decodes any value" $ do
let config = "foo 1.0 asdf #true"
decoder = KDL.document $ _DO_
_STMT_(map scrubFormat <$> KDL.argsAtWith "foo" KDL.any)
val data_ =
Value
{ ann = Nothing
, data_ = data_
, ext = KDL.def
}
KDL.decodeWith decoder config
`shouldBe` Right [val $ Number 1, val $ String "asdf", val $ Bool True]
describe "string" $ do
it "decodes string value" $ do
let config = "foo asdf"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.string)
KDL.decodeWith decoder config `shouldBe` Right "asdf"
it "fails when value is not string" $ do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.string)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected string, got: 1"
]
describe "number" $ do
it "decodes number value" $ do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.number)
KDL.decodeWith decoder config `shouldBe` Right 1
it "fails when value is not number" $ do
let config = "foo asdf"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.number)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected number, got: asdf"
]
describe "bool" $ do
it "decodes bool value" $ do
let config = "foo #true"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.bool)
KDL.decodeWith decoder config `shouldBe` Right True
it "fails when value is not bool" $ do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.bool)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected bool, got: 1"
]
describe "null" $ do
it "decodes null value" $ do
let config = "foo #null"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.null)
KDL.decodeWith decoder config `shouldBe` Right ()
it "fails when value is not null" $ do
let config = "foo 1"
decoder = KDL.document $ _DO_
_STMT_(KDL.argAtWith "foo" KDL.null)
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected null, got: 1"
]
describe "Combinators" $ do