6
6
7
7
module Test.Codec.CBOR.Cuddle.Huddle where
8
8
9
- import Codec.CBOR.Cuddle.CDDL (CDDL )
9
+ import Codec.CBOR.Cuddle.CDDL (CDDL , sortCDDL )
10
10
import Codec.CBOR.Cuddle.Huddle
11
11
import Codec.CBOR.Cuddle.Parser
12
12
import Data.Text qualified as T
@@ -26,37 +26,37 @@ huddleSpec = describe "huddle" $ do
26
26
basicAssign :: Spec
27
27
basicAssign = describe " basic assignment" $ do
28
28
it " Can assign a primitive" $
29
- toCDDLNoRoot [" port" =:= VUInt ]
29
+ toSortedCDDL [" port" =:= VUInt ]
30
30
`shouldMatchParseCDDL` " port = uint"
31
31
it " Can assign an int" $
32
- toCDDLNoRoot [" one" =:= (int 1 )]
32
+ toSortedCDDL [" one" =:= (int 1 )]
33
33
`shouldMatchParseCDDL` " one = 1"
34
34
-- it "Can assign a float" $
35
- -- toCDDLNoRoot ["onepointone" =:= (1.1 :: Float)]
35
+ -- toSortedCDDL ["onepointone" =:= (1.1 :: Float)]
36
36
-- `shouldMatchParseCDDL` "onepointone = 1.1"
37
37
it " Can assign a text string" $
38
- toCDDLNoRoot [" hello" =:= (" Hello World" :: T. Text )]
38
+ toSortedCDDL [" hello" =:= (" Hello World" :: T. Text )]
39
39
`shouldMatchParseCDDL` " hello = \" Hello World\" "
40
40
it " Can handle multiple assignments" $
41
- toCDDLNoRoot [" age" =:= VUInt , " location" =:= VText ]
41
+ toSortedCDDL [" age" =:= VUInt , " location" =:= VText ]
42
42
`shouldMatchParseCDDL` " age = uint\n location = text"
43
43
44
44
arraySpec :: Spec
45
45
arraySpec = describe " Arrays" $ do
46
46
it " Can assign a small array" $
47
- toCDDLNoRoot [" asl" =:= arr [a VUInt , a VBool , a VText ]]
47
+ toSortedCDDL [" asl" =:= arr [a VUInt , a VBool , a VText ]]
48
48
`shouldMatchParseCDDL` " asl = [ uint, bool, text ]"
49
49
it " Can quantify an upper bound" $
50
- toCDDLNoRoot [" age" =:= arr [a VUInt +> 64 ]]
50
+ toSortedCDDL [" age" =:= arr [a VUInt +> 64 ]]
51
51
`shouldMatchParseCDDL` " age = [ *64 uint ]"
52
52
it " Can quantify an optional" $
53
- toCDDLNoRoot [" age" =:= arr [0 <+ a VUInt +> 1 ]]
53
+ toSortedCDDL [" age" =:= arr [0 <+ a VUInt +> 1 ]]
54
54
`shouldMatchParseCDDL` " age = [ ? uint ]"
55
55
it " Can handle a choice" $
56
- toCDDLNoRoot [" ageOrSex" =:= arr [a VUInt ] / arr [a VBool ]]
56
+ toSortedCDDL [" ageOrSex" =:= arr [a VUInt ] / arr [a VBool ]]
57
57
`shouldMatchParseCDDL` " ageOrSex = [ uint // bool ]"
58
58
it " Can handle choices of groups" $
59
- toCDDLNoRoot
59
+ toSortedCDDL
60
60
[ " asl"
61
61
=:= arr [a VUInt , a VBool , a VText ]
62
62
/ arr
@@ -69,31 +69,31 @@ arraySpec = describe "Arrays" $ do
69
69
mapSpec :: Spec
70
70
mapSpec = describe " Maps" $ do
71
71
it " Can assign a small map" $
72
- toCDDLNoRoot [" asl" =:= mp [" age" ==> VUInt , " sex" ==> VBool , " location" ==> VText ]]
72
+ toSortedCDDL [" asl" =:= mp [" age" ==> VUInt , " sex" ==> VBool , " location" ==> VText ]]
73
73
`shouldMatchParseCDDL` " asl = { age : uint, sex : bool, location : text }"
74
74
it " Can quantify a lower bound" $
75
- toCDDLNoRoot [" age" =:= mp [0 <+ " years" ==> VUInt ]]
75
+ toSortedCDDL [" age" =:= mp [0 <+ " years" ==> VUInt ]]
76
76
`shouldMatchParseCDDL` " age = { * years : uint }"
77
77
it " Can quantify an upper bound" $
78
- toCDDLNoRoot [" age" =:= mp [" years" ==> VUInt +> 64 ]]
78
+ toSortedCDDL [" age" =:= mp [" years" ==> VUInt +> 64 ]]
79
79
`shouldMatchParseCDDL` " age = { *64 years : uint }"
80
80
it " Can handle a choice" $
81
- toCDDLNoRoot [" ageOrSex" =:= mp [" age" ==> VUInt ] / mp [" sex" ==> VBool ]]
81
+ toSortedCDDL [" ageOrSex" =:= mp [" age" ==> VUInt ] / mp [" sex" ==> VBool ]]
82
82
`shouldMatchParseCDDL` " ageOrSex = { age : uint // sex : bool }"
83
83
it " Can handle a choice with an entry" $
84
- toCDDLNoRoot [" mir" =:= arr [a (int 0 / int 1 ), a $ mp [0 <+ " test" ==> VUInt ]]]
84
+ toSortedCDDL [" mir" =:= arr [a (int 0 / int 1 ), a $ mp [0 <+ " test" ==> VUInt ]]]
85
85
`shouldMatchParseCDDL` " mir = [ 0 / 1, { * test : uint }]"
86
86
87
87
nestedSpec :: Spec
88
88
nestedSpec =
89
89
describe " Nesting" $
90
90
it " Handles references" $
91
91
let headerBody = " header_body" =:= arr [" block_number" ==> VUInt , " slot" ==> VUInt ]
92
- in toCDDLNoRoot
92
+ in toSortedCDDL
93
93
[ headerBody,
94
94
" header" =:= arr [a headerBody, " body_signature" ==> VBytes ]
95
95
]
96
- `shouldMatchParseCDDL` " header_body = [block_number : uint, slot : uint ]\n header = [header_body, body_signature : bytes ]"
96
+ `shouldMatchParseCDDL` " header = [header_body, body_signature : bytes ]\n header_body = [block_number : uint, slot : uint ]"
97
97
98
98
genericSpec :: Spec
99
99
genericSpec =
@@ -105,11 +105,11 @@ genericSpec =
105
105
dict = binding2 $ \ k v -> " dict" =:= mp [0 <+ asKey k ==> v]
106
106
in do
107
107
it " Should bind a single parameter" $
108
- toCDDLNoRoot (collectFrom [" intset" =:= set VUInt ])
108
+ toSortedCDDL (collectFrom [" intset" =:= set VUInt ])
109
109
`shouldMatchParseCDDL` " intset = set<uint>\n set<a0> = [* a0]"
110
110
it " Should bind two parameters" $
111
- toCDDLNoRoot (collectFrom [" mymap" =:= dict VUInt VText ])
112
- `shouldMatchParseCDDL` " mymap = dict<uint, text> \n dict< a0, b0> = {* a0 => b0}"
111
+ toSortedCDDL (collectFrom [" mymap" =:= dict VUInt VText ])
112
+ `shouldMatchParseCDDL` " dict<a0, b0> = {* a0 => b0}\n mymap = dict<uint, text> "
113
113
114
114
--------------------------------------------------------------------------------
115
115
-- Helper functions
@@ -128,3 +128,6 @@ shouldMatchParseCDDL ::
128
128
String ->
129
129
Expectation
130
130
shouldMatchParseCDDL x = shouldMatchParse x pCDDL
131
+
132
+ toSortedCDDL :: Huddle -> CDDL
133
+ toSortedCDDL = sortCDDL . toCDDLNoRoot
0 commit comments