66
77module Test.Codec.CBOR.Cuddle.Huddle where
88
9- import Codec.CBOR.Cuddle.CDDL (CDDL )
9+ import Codec.CBOR.Cuddle.CDDL (CDDL , sortCDDL )
1010import Codec.CBOR.Cuddle.Huddle
1111import Codec.CBOR.Cuddle.Parser
1212import Data.Text qualified as T
@@ -26,37 +26,37 @@ huddleSpec = describe "huddle" $ do
2626basicAssign :: Spec
2727basicAssign = describe " basic assignment" $ do
2828 it " Can assign a primitive" $
29- toCDDLNoRoot [" port" =:= VUInt ]
29+ toSortedCDDL [" port" =:= VUInt ]
3030 `shouldMatchParseCDDL` " port = uint"
3131 it " Can assign an int" $
32- toCDDLNoRoot [" one" =:= (int 1 )]
32+ toSortedCDDL [" one" =:= (int 1 )]
3333 `shouldMatchParseCDDL` " one = 1"
3434 -- it "Can assign a float" $
35- -- toCDDLNoRoot ["onepointone" =:= (1.1 :: Float)]
35+ -- toSortedCDDL ["onepointone" =:= (1.1 :: Float)]
3636 -- `shouldMatchParseCDDL` "onepointone = 1.1"
3737 it " Can assign a text string" $
38- toCDDLNoRoot [" hello" =:= (" Hello World" :: T. Text )]
38+ toSortedCDDL [" hello" =:= (" Hello World" :: T. Text )]
3939 `shouldMatchParseCDDL` " hello = \" Hello World\" "
4040 it " Can handle multiple assignments" $
41- toCDDLNoRoot [" age" =:= VUInt , " location" =:= VText ]
41+ toSortedCDDL [" age" =:= VUInt , " location" =:= VText ]
4242 `shouldMatchParseCDDL` " age = uint\n location = text"
4343
4444arraySpec :: Spec
4545arraySpec = describe " Arrays" $ do
4646 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 ]]
4848 `shouldMatchParseCDDL` " asl = [ uint, bool, text ]"
4949 it " Can quantify an upper bound" $
50- toCDDLNoRoot [" age" =:= arr [a VUInt +> 64 ]]
50+ toSortedCDDL [" age" =:= arr [a VUInt +> 64 ]]
5151 `shouldMatchParseCDDL` " age = [ *64 uint ]"
5252 it " Can quantify an optional" $
53- toCDDLNoRoot [" age" =:= arr [0 <+ a VUInt +> 1 ]]
53+ toSortedCDDL [" age" =:= arr [0 <+ a VUInt +> 1 ]]
5454 `shouldMatchParseCDDL` " age = [ ? uint ]"
5555 it " Can handle a choice" $
56- toCDDLNoRoot [" ageOrSex" =:= arr [a VUInt ] / arr [a VBool ]]
56+ toSortedCDDL [" ageOrSex" =:= arr [a VUInt ] / arr [a VBool ]]
5757 `shouldMatchParseCDDL` " ageOrSex = [ uint // bool ]"
5858 it " Can handle choices of groups" $
59- toCDDLNoRoot
59+ toSortedCDDL
6060 [ " asl"
6161 =:= arr [a VUInt , a VBool , a VText ]
6262 / arr
@@ -69,31 +69,31 @@ arraySpec = describe "Arrays" $ do
6969mapSpec :: Spec
7070mapSpec = describe " Maps" $ do
7171 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 ]]
7373 `shouldMatchParseCDDL` " asl = { age : uint, sex : bool, location : text }"
7474 it " Can quantify a lower bound" $
75- toCDDLNoRoot [" age" =:= mp [0 <+ " years" ==> VUInt ]]
75+ toSortedCDDL [" age" =:= mp [0 <+ " years" ==> VUInt ]]
7676 `shouldMatchParseCDDL` " age = { * years : uint }"
7777 it " Can quantify an upper bound" $
78- toCDDLNoRoot [" age" =:= mp [" years" ==> VUInt +> 64 ]]
78+ toSortedCDDL [" age" =:= mp [" years" ==> VUInt +> 64 ]]
7979 `shouldMatchParseCDDL` " age = { *64 years : uint }"
8080 it " Can handle a choice" $
81- toCDDLNoRoot [" ageOrSex" =:= mp [" age" ==> VUInt ] / mp [" sex" ==> VBool ]]
81+ toSortedCDDL [" ageOrSex" =:= mp [" age" ==> VUInt ] / mp [" sex" ==> VBool ]]
8282 `shouldMatchParseCDDL` " ageOrSex = { age : uint // sex : bool }"
8383 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 ]]]
8585 `shouldMatchParseCDDL` " mir = [ 0 / 1, { * test : uint }]"
8686
8787nestedSpec :: Spec
8888nestedSpec =
8989 describe " Nesting" $
9090 it " Handles references" $
9191 let headerBody = " header_body" =:= arr [" block_number" ==> VUInt , " slot" ==> VUInt ]
92- in toCDDLNoRoot
92+ in toSortedCDDL
9393 [ headerBody,
9494 " header" =:= arr [a headerBody, " body_signature" ==> VBytes ]
9595 ]
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 ]"
9797
9898genericSpec :: Spec
9999genericSpec =
@@ -105,11 +105,11 @@ genericSpec =
105105 dict = binding2 $ \ k v -> " dict" =:= mp [0 <+ asKey k ==> v]
106106 in do
107107 it " Should bind a single parameter" $
108- toCDDLNoRoot (collectFrom [" intset" =:= set VUInt ])
108+ toSortedCDDL (collectFrom [" intset" =:= set VUInt ])
109109 `shouldMatchParseCDDL` " intset = set<uint>\n set<a0> = [* a0]"
110110 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> "
113113
114114--------------------------------------------------------------------------------
115115-- Helper functions
@@ -128,3 +128,6 @@ shouldMatchParseCDDL ::
128128 String ->
129129 Expectation
130130shouldMatchParseCDDL x = shouldMatchParse x pCDDL
131+
132+ toSortedCDDL :: Huddle -> CDDL
133+ toSortedCDDL = sortCDDL . toCDDLNoRoot
0 commit comments