@@ -11,11 +11,12 @@ import Codec.CBOR.Cuddle.Pretty ()
11
11
import Data.Default.Class (Default (.. ))
12
12
import Data.List.NonEmpty (NonEmpty (.. ))
13
13
import Data.Text qualified as T
14
- import Data.TreeDiff (ToExpr (.. ), ansiWlBgEditExprCompact , exprDiff )
14
+ import Data.TreeDiff (ToExpr (.. ), ansiWlBgEditExprCompact , exprDiff , prettyExpr )
15
15
import Prettyprinter (Pretty , defaultLayoutOptions , layoutPretty , pretty )
16
16
import Prettyprinter.Render.String (renderString )
17
17
import Prettyprinter.Render.Text (renderStrict )
18
18
import Test.Codec.CBOR.Cuddle.CDDL.Gen qualified as Gen ()
19
+ import Test.Codec.CBOR.Cuddle.Huddle (ruleMatches , shouldMatchParseRule , shouldMatchParseWith )
19
20
import Test.Hspec
20
21
import Test.Hspec.Megaparsec
21
22
import Test.QuickCheck
@@ -40,15 +41,14 @@ roundtripSpec = describe "Roundtripping should be id" $ do
40
41
xit " Trip Value" $ trip pValue
41
42
xit " Trip Type0" $ trip pType0
42
43
xit " Trip GroupEntry" $ trip pGrpEntry
43
- xit " Trip Rule" $ trip pRule
44
44
where
45
45
-- We show that, for a printed CDDL document p, print (parse p) == p. Note
46
46
-- that we do not show that parse (print p) is p for a given generated
47
47
-- 'CDDL' doc, since CDDL contains some statements that allow multiple
48
48
-- 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
51
- let printed = printText x
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
51
+ let printed = T. pack . show . prettyExpr $ toExpr x
52
52
case parse (pa <* eof) " " printed of
53
53
Left e ->
54
54
counterexample (show printed) $
@@ -106,63 +106,65 @@ nameSpec = describe "pName" $ do
106
106
genericSpec :: Spec
107
107
genericSpec = describe " generics" $ do
108
108
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>"
137
138
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>"
166
168
167
169
type2Spec :: SpecWith ()
168
170
type2Spec = describe " type2" $ do
@@ -616,10 +618,16 @@ type1Spec = describe "Type1" $ do
616
618
(Just (RangeOp ClOpen , T2Value (value $ VUInt 3 )))
617
619
mempty
618
620
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 =
621
623
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 (==)
623
631
624
632
-- | A bunch of cases found by hedgehog/QC
625
633
qcFoundSpec :: Spec
@@ -651,7 +659,7 @@ qcFoundSpec =
651
659
)
652
660
, t1Comment = Comment mempty
653
661
}
654
- parseExample " S = 0* ()" pRule $
662
+ parseExampleWith ruleMatches " S = 0* ()" pRule $
655
663
Rule
656
664
(Name " S" mempty )
657
665
Nothing
@@ -662,7 +670,9 @@ qcFoundSpec =
662
670
)
663
671
)
664
672
mempty
665
- parseExample
673
+ Nothing
674
+ parseExampleWith
675
+ ruleMatches
666
676
" W = \" 6 ybe2ddl8frq0vqa8zgrk07khrljq7p plrufpd1sff3p95\" : \" u\" "
667
677
pRule
668
678
( Rule
@@ -679,4 +689,5 @@ qcFoundSpec =
679
689
)
680
690
)
681
691
mempty
692
+ Nothing
682
693
)
0 commit comments