Skip to content

Commit 55d2c65

Browse files
committed
Fix test failures
The introduction of a pseudo root rule in 79d6a78 broke the Huddle tests, because they were compared against a definition with no root. Since we don't want to add a root element in each definition, we add a variant of 'toCDDL' which does not create the root rule.
1 parent 7c434d2 commit 55d2c65

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

src/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module Codec.CBOR.Cuddle.Huddle
7777
-- * Conversion to CDDL
7878
collectFrom,
7979
toCDDL,
80+
toCDDLNoRoot
8081
)
8182
where
8283

@@ -871,15 +872,25 @@ collectFrom topRs =
871872
--------------------------------------------------------------------------------
872873
-- Conversion to CDDL
873874
--------------------------------------------------------------------------------
875+
-- | Convert from Huddle to CDDL, generating a top level root element.
876+
toCDDL :: Huddle -> CDDL
877+
toCDDL = toCDDL' True
878+
879+
-- | Convert from Huddle to CDDL, skipping a root element.
880+
toCDDLNoRoot :: Huddle -> CDDL
881+
toCDDLNoRoot = toCDDL' False
874882

875883
-- | Convert from Huddle to CDDL for the purpose of pretty-printing.
876-
toCDDL :: Huddle -> CDDL
877-
toCDDL hdl =
878-
C.CDDL $
879-
toTopLevelPseudoRoot (roots hdl)
880-
NE.<| fmap toCDDLRule (rules hdl)
881-
`appendList` fmap toCDDLGroup (groups hdl)
882-
`appendList` fmap toGenRuleDef (gRules hdl)
884+
toCDDL' :: Bool -> Huddle -> CDDL
885+
toCDDL' mkPseudoRoot hdl =
886+
C.CDDL
887+
$ ( if mkPseudoRoot
888+
then (toTopLevelPseudoRoot (roots hdl) NE.<|)
889+
else id
890+
)
891+
$ fmap toCDDLRule (rules hdl)
892+
`appendList` fmap toCDDLGroup (groups hdl)
893+
`appendList` fmap toGenRuleDef (gRules hdl)
883894
where
884895
toTopLevelPseudoRoot :: [Rule] -> C.WithComments C.Rule
885896
toTopLevelPseudoRoot topRs =

test/Test/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,37 @@ huddleSpec = describe "huddle" $ do
2626
basicAssign :: Spec
2727
basicAssign = describe "basic assignment" $ do
2828
it "Can assign a primitive" $
29-
toCDDL ["port" =:= VUInt]
29+
toCDDLNoRoot ["port" =:= VUInt]
3030
`shouldMatchParseCDDL` "port = uint"
3131
it "Can assign an int" $
32-
toCDDL ["one" =:= (int 1)]
32+
toCDDLNoRoot ["one" =:= (int 1)]
3333
`shouldMatchParseCDDL` "one = 1"
3434
-- it "Can assign a float" $
35-
-- toCDDL ["onepointone" =:= (1.1 :: Float)]
35+
-- toCDDLNoRoot ["onepointone" =:= (1.1 :: Float)]
3636
-- `shouldMatchParseCDDL` "onepointone = 1.1"
3737
it "Can assign a text string" $
38-
toCDDL ["hello" =:= ("Hello World" :: T.Text)]
38+
toCDDLNoRoot ["hello" =:= ("Hello World" :: T.Text)]
3939
`shouldMatchParseCDDL` "hello = \"Hello World\""
4040
it "Can handle multiple assignments" $
41-
toCDDL ["age" =:= VUInt, "location" =:= VText]
41+
toCDDLNoRoot ["age" =:= VUInt, "location" =:= VText]
4242
`shouldMatchParseCDDL` "age = uint\n location = text"
4343

4444
arraySpec :: Spec
4545
arraySpec = describe "Arrays" $ do
4646
it "Can assign a small array" $
47-
toCDDL ["asl" =:= arr [a VUInt, a VBool, a VText]]
47+
toCDDLNoRoot ["asl" =:= arr [a VUInt, a VBool, a VText]]
4848
`shouldMatchParseCDDL` "asl = [ uint, bool, text ]"
4949
it "Can quantify an upper bound" $
50-
toCDDL ["age" =:= arr [a VUInt +> 64]]
50+
toCDDLNoRoot ["age" =:= arr [a VUInt +> 64]]
5151
`shouldMatchParseCDDL` "age = [ *64 uint ]"
5252
it "Can quantify an optional" $
53-
toCDDL ["age" =:= arr [0 <+ a VUInt +> 1]]
53+
toCDDLNoRoot ["age" =:= arr [0 <+ a VUInt +> 1]]
5454
`shouldMatchParseCDDL` "age = [ ? uint ]"
5555
it "Can handle a choice" $
56-
toCDDL ["ageOrSex" =:= arr [a VUInt] / arr [a VBool]]
56+
toCDDLNoRoot ["ageOrSex" =:= arr [a VUInt] / arr [a VBool]]
5757
`shouldMatchParseCDDL` "ageOrSex = [ uint // bool ]"
5858
it "Can handle choices of groups" $
59-
toCDDL
59+
toCDDLNoRoot
6060
[ "asl"
6161
=:= arr [a VUInt, a VBool, a VText]
6262
/ arr
@@ -69,27 +69,27 @@ arraySpec = describe "Arrays" $ do
6969
mapSpec :: Spec
7070
mapSpec = describe "Maps" $ do
7171
it "Can assign a small map" $
72-
toCDDL ["asl" =:= mp ["age" ==> VUInt, "sex" ==> VBool, "location" ==> VText]]
72+
toCDDLNoRoot ["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-
toCDDL ["age" =:= mp [0 <+ "years" ==> VUInt]]
75+
toCDDLNoRoot ["age" =:= mp [0 <+ "years" ==> VUInt]]
7676
`shouldMatchParseCDDL` "age = { * years : uint }"
7777
it "Can quantify an upper bound" $
78-
toCDDL ["age" =:= mp ["years" ==> VUInt +> 64]]
78+
toCDDLNoRoot ["age" =:= mp ["years" ==> VUInt +> 64]]
7979
`shouldMatchParseCDDL` "age = { *64 years : uint }"
8080
it "Can handle a choice" $
81-
toCDDL ["ageOrSex" =:= mp ["age" ==> VUInt] / mp ["sex" ==> VBool]]
81+
toCDDLNoRoot ["ageOrSex" =:= mp ["age" ==> VUInt] / mp ["sex" ==> VBool]]
8282
`shouldMatchParseCDDL` "ageOrSex = { age : uint // sex : bool }"
8383
it "Can handle a choice with an entry" $
84-
toCDDL ["mir" =:= arr [a (int 0 / int 1), a $ mp [0 <+ "test" ==> VUInt]]]
84+
toCDDLNoRoot ["mir" =:= arr [a (int 0 / int 1), a $ mp [0 <+ "test" ==> VUInt]]]
8585
`shouldMatchParseCDDL` "mir = [ 0 / 1, { * test : uint }]"
8686

8787
nestedSpec :: Spec
8888
nestedSpec =
8989
describe "Nesting" $
9090
it "Handles references" $
9191
let headerBody = "header_body" =:= arr ["block_number" ==> VUInt, "slot" ==> VUInt]
92-
in toCDDL
92+
in toCDDLNoRoot
9393
[ headerBody,
9494
"header" =:= arr [a headerBody, "body_signature" ==> VBytes]
9595
]
@@ -105,10 +105,10 @@ genericSpec =
105105
dict = binding2 $ \k v -> "dict" =:= mp [0 <+ asKey k ==> v]
106106
in do
107107
it "Should bind a single parameter" $
108-
toCDDL (collectFrom ["intset" =:= set VUInt])
108+
toCDDLNoRoot (collectFrom ["intset" =:= set VUInt])
109109
`shouldMatchParseCDDL` "intset = set<uint>\n set<a0> = [* a0]"
110110
it "Should bind two parameters" $
111-
toCDDL (collectFrom ["mymap" =:= dict VUInt VText])
111+
toCDDLNoRoot (collectFrom ["mymap" =:= dict VUInt VText])
112112
`shouldMatchParseCDDL` "mymap = dict<uint, text>\n dict<a0, b0> = {* a0 => b0}"
113113

114114
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)