@@ -11,11 +11,12 @@ import Codec.CBOR.Cuddle.Pretty ()
1111import Data.Default.Class (Default (.. ))
1212import Data.List.NonEmpty (NonEmpty (.. ))
1313import Data.Text qualified as T
14- import Data.TreeDiff (ToExpr (.. ), ansiWlBgEditExprCompact , exprDiff )
14+ import Data.TreeDiff (ToExpr (.. ), ansiWlBgEditExprCompact , exprDiff , prettyExpr )
1515import Prettyprinter (Pretty , defaultLayoutOptions , layoutPretty , pretty )
1616import Prettyprinter.Render.String (renderString )
1717import Prettyprinter.Render.Text (renderStrict )
1818import Test.Codec.CBOR.Cuddle.CDDL.Gen qualified as Gen ()
19+ import Test.Codec.CBOR.Cuddle.Huddle (ruleMatches , shouldMatchParseRule , shouldMatchParseWith )
1920import Test.Hspec
2021import Test.Hspec.Megaparsec
2122import Test.QuickCheck
@@ -40,14 +41,13 @@ roundtripSpec = describe "Roundtripping should be id" $ do
4041 xit " Trip Value" $ trip pValue
4142 xit " Trip Type0" $ trip pType0
4243 xit " Trip GroupEntry" $ trip pGrpEntry
43- xit " Trip Rule" $ trip pRule
4444 where
4545 -- We show that, for a printed CDDL document p, print (parse p) == p. Note
4646 -- that we do not show that parse (print p) is p for a given generated
4747 -- 'CDDL' doc, since CDDL contains some statements that allow multiple
4848 -- parsings.
49- trip :: forall a . (Eq a , ToExpr a , Show a , Pretty a , Arbitrary a ) => Parser a -> Property
50- trip pa = property $ \ (x :: a ) -> within 1000000 $ do
49+ trip :: forall a . (ToExpr a , Arbitrary a , Pretty a ) => Parser a -> Property
50+ trip pa = forAllShow arbitrary ( show . prettyExpr . toExpr) $ \ (x :: a ) -> within 1000000 $ do
5151 let printed = printText x
5252 case parse (pa <* eof) " " printed of
5353 Left e ->
@@ -106,63 +106,65 @@ nameSpec = describe "pName" $ do
106106genericSpec :: Spec
107107genericSpec = describe " generics" $ do
108108 it " Parses a simple value generic" $
109- parse pRule " " " a = b<0>"
110- `shouldParse` Rule
111- (Name " a" mempty )
112- Nothing
113- AssignEq
114- ( TOGType
115- ( Type0
116- ( Type1
117- ( T2Name
118- (Name " b" mempty )
119- ( Just
120- ( GenericArg
121- ( Type1
122- (T2Value (value $ VUInt 0 ))
123- Nothing
124- mempty
125- :| []
126- )
127- )
128- )
129- )
130- Nothing
131- mempty
132- :| []
133- )
134- )
135- )
136- mempty
109+ Rule
110+ (Name " a" mempty )
111+ Nothing
112+ AssignEq
113+ ( TOGType
114+ ( Type0
115+ ( Type1
116+ ( T2Name
117+ (Name " b" mempty )
118+ ( Just
119+ ( GenericArg
120+ ( Type1
121+ (T2Value (value $ VUInt 0 ))
122+ Nothing
123+ mempty
124+ :| []
125+ )
126+ )
127+ )
128+ )
129+ Nothing
130+ mempty
131+ :| []
132+ )
133+ )
134+ )
135+ mempty
136+ Nothing
137+ `shouldMatchParseRule` " a = b<0>"
137138 it " Parses a range as a generic" $
138- parse pRule " " " a = b<0 ... 1>"
139- `shouldParse` Rule
140- (Name " a" mempty )
141- Nothing
142- AssignEq
143- ( TOGType
144- ( Type0
145- ( Type1
146- ( T2Name
147- (Name " b" mempty )
148- ( Just
149- ( GenericArg
150- ( Type1
151- (T2Value (value $ VUInt 0 ))
152- (Just (RangeOp ClOpen , T2Value (value $ VUInt 1 )))
153- mempty
154- :| []
155- )
156- )
157- )
158- )
159- Nothing
160- mempty
161- :| []
162- )
163- )
164- )
165- mempty
139+ Rule
140+ (Name " a" mempty )
141+ Nothing
142+ AssignEq
143+ ( TOGType
144+ ( Type0
145+ ( Type1
146+ ( T2Name
147+ (Name " b" mempty )
148+ ( Just
149+ ( GenericArg
150+ ( Type1
151+ (T2Value (value $ VUInt 0 ))
152+ (Just (RangeOp ClOpen , T2Value (value $ VUInt 1 )))
153+ mempty
154+ :| []
155+ )
156+ )
157+ )
158+ )
159+ Nothing
160+ mempty
161+ :| []
162+ )
163+ )
164+ )
165+ mempty
166+ Nothing
167+ `shouldMatchParseRule` " a = b<0 ... 1>"
166168
167169type2Spec :: SpecWith ()
168170type2Spec = describe " type2" $ do
@@ -616,10 +618,16 @@ type1Spec = describe "Type1" $ do
616618 (Just (RangeOp ClOpen , T2Value (value $ VUInt 3 )))
617619 mempty
618620
619- parseExample :: ( Show a , Eq a ) = > T. Text -> Parser a -> a -> Spec
620- parseExample str parser val =
621+ parseExampleWith :: ToExpr a => ( a -> a -> Bool ) - > T. Text -> Parser a -> a -> Spec
622+ parseExampleWith matches str parser val =
621623 it (T. unpack str) $
622- parse (parser <* eof) " " str `shouldParse` val
624+ shouldMatchParseWith matches val parser $
625+ T. unpack str
626+
627+ -- parse (parser <* eof) "" str `shouldParse` val
628+
629+ parseExample :: (Show a , ToExpr a , Eq a ) => T. Text -> Parser a -> a -> Spec
630+ parseExample = parseExampleWith (==)
623631
624632-- | A bunch of cases found by hedgehog/QC
625633qcFoundSpec :: Spec
@@ -651,7 +659,7 @@ qcFoundSpec =
651659 )
652660 , t1Comment = Comment mempty
653661 }
654- parseExample " S = 0* ()" pRule $
662+ parseExampleWith ruleMatches " S = 0* ()" pRule $
655663 Rule
656664 (Name " S" mempty )
657665 Nothing
@@ -662,7 +670,9 @@ qcFoundSpec =
662670 )
663671 )
664672 mempty
665- parseExample
673+ Nothing
674+ parseExampleWith
675+ ruleMatches
666676 " W = \" 6 ybe2ddl8frq0vqa8zgrk07khrljq7p plrufpd1sff3p95\" : \" u\" "
667677 pRule
668678 ( Rule
@@ -679,4 +689,5 @@ qcFoundSpec =
679689 )
680690 )
681691 mempty
692+ Nothing
682693 )
0 commit comments