Skip to content

Commit b64201f

Browse files
committed
Fixed tests
1 parent 77fca2d commit b64201f

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

src/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Codec.CBOR.Cuddle.Huddle (
2727
HuddleStage,
2828
C.XCddl (..),
2929
C.XTerm (..),
30+
C.XRule (..),
3031
C.XXTopLevel (..),
3132
C.XXType2 (..),
3233

@@ -139,6 +140,8 @@ data instance C.XRule HuddleStage = HuddleXRule
139140
}
140141
deriving (Generic)
141142

143+
instance Default (XRule HuddleStage)
144+
142145
newtype instance C.XXTopLevel HuddleStage = HuddleXXTopLevel C.Comment
143146
deriving (Generic, Semigroup, Monoid, Show, Eq)
144147

@@ -757,7 +760,7 @@ infixl 8 ==>
757760

758761
-- | Assign a rule
759762
(=:=) :: IsType0 a => T.Text -> a -> Rule
760-
n =:= b = Rule (Named n (toType0 b) Nothing) (HuddleXRule undefined undefined)
763+
n =:= b = Rule (Named n (toType0 b) Nothing) def
761764

762765
infixl 1 =:=
763766

@@ -1069,7 +1072,7 @@ collectFrom topRs =
10691072
goT2 (T2Constrained (Constrained c _ refs)) =
10701073
( case c of
10711074
CValue _ -> pure ()
1072-
CRef r -> goRule $ Rule r undefined
1075+
CRef r -> goRule $ Rule r def
10731076
CGRef _ -> pure ()
10741077
)
10751078
>> mapM_ goRule refs

src/Codec/CBOR/Cuddle/Pretty.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Codec.CBOR.Cuddle.Pretty.Columnar (
2424
)
2525
import Codec.CBOR.Cuddle.Pretty.Utils (renderedLen, softspace)
2626
import Data.ByteString.Char8 qualified as BS
27+
import Data.Default.Class (Default)
2728
import Data.Foldable (Foldable (..))
2829
import Data.List.NonEmpty qualified as NE
2930
import Data.String (IsString, fromString)
@@ -50,6 +51,7 @@ newtype instance XCddl PrettyStage = PrettyXCddl [Comment]
5051

5152
newtype instance XRule PrettyStage = PrettyXRule {unPrettyXRule :: Comment}
5253
deriving (Generic, CollectComments, ToExpr, Show, Eq)
54+
deriving newtype (Default)
5355

5456
instance HasComment (XTerm PrettyStage) where
5557
commentL = #unPrettyXTerm

test/Test/Codec/CBOR/Cuddle/CDDL/Gen.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Codec.CBOR.Cuddle.CDDL
1010
import Codec.CBOR.Cuddle.CDDL.CtlOp
1111
import Codec.CBOR.Cuddle.Comments (Comment (..))
1212
import Codec.CBOR.Cuddle.Parser (ParserStage, XTerm (..))
13-
import Codec.CBOR.Cuddle.Pretty (PrettyStage, XTerm (..), XXTopLevel (..))
13+
import Codec.CBOR.Cuddle.Pretty (PrettyStage, XRule (..), XTerm (..), XXTopLevel (..))
1414
import Data.ByteString (ByteString)
1515
import Data.ByteString qualified as BS
1616
import Data.List.NonEmpty qualified as NE
@@ -27,6 +27,8 @@ deriving newtype instance Arbitrary (XXTopLevel PrettyStage)
2727

2828
deriving newtype instance Arbitrary (XTerm PrettyStage)
2929

30+
deriving newtype instance Arbitrary (XRule PrettyStage)
31+
3032
instance Arbitrary (TopLevel PrettyStage) where
3133
arbitrary =
3234
Gen.oneof
@@ -91,7 +93,13 @@ instance (Arbitrary (XTerm i), Monoid (XTerm i)) => Arbitrary (GenericArg i) whe
9193
arbitrary = GenericArg <$> nonEmpty arbitrary
9294
shrink (GenericArg neArg) = GenericArg <$> shrinkNE neArg
9395

94-
instance (Monoid (XTerm i), Arbitrary (XTerm i)) => Arbitrary (Rule i) where
96+
instance
97+
( Monoid (XTerm i)
98+
, Arbitrary (XTerm i)
99+
, Arbitrary (XRule i)
100+
) =>
101+
Arbitrary (Rule i)
102+
where
95103
arbitrary =
96104
Rule
97105
<$> arbitrary

test/Test/Codec/CBOR/Cuddle/CDDL/Pretty.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Codec.CBOR.Cuddle.CDDL (
2323
value,
2424
)
2525
import Codec.CBOR.Cuddle.Pretty (PrettyStage)
26+
import Data.Default.Class (Default (..))
2627
import Data.List.NonEmpty (NonEmpty (..))
2728
import Data.Text qualified as T
2829
import Data.TreeDiff (ToExpr (..), prettyExpr)
@@ -120,7 +121,7 @@ drep =
120121
)
121122
)
122123
)
123-
mempty
124+
def
124125

125126
unitSpec :: Spec
126127
unitSpec = describe "HUnit" $ do
@@ -185,7 +186,7 @@ unitSpec = describe "HUnit" $ do
185186
Nothing
186187
AssignEq
187188
(TOGType (Type0 (Type1 (T2Name (Name "b" mempty) mempty) Nothing mempty :| [])))
188-
mempty
189+
def
189190
`prettyPrintsTo` "a = b"
190191
xit "drep" $
191192
drep

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,73 @@
11
{-# LANGUAGE OverloadedLists #-}
22
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE TypeData #-}
4+
{-# LANGUAGE TypeFamilies #-}
35

46
{- HLINT ignore "Redundant bracket" -}
57

68
module Test.Codec.CBOR.Cuddle.Huddle where
79

810
import Codec.CBOR.Cuddle.CDDL (CDDL, fromRules, sortCDDL)
11+
import Codec.CBOR.Cuddle.Comments (Comment)
912
import Codec.CBOR.Cuddle.Huddle
1013
import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..))
1114
import Codec.CBOR.Cuddle.Parser
1215
import Data.Text qualified as T
16+
import Data.Void (Void)
17+
import GHC.Generics (Generic)
1318
import Test.Codec.CBOR.Cuddle.CDDL.Pretty qualified as Pretty
1419
import Test.Hspec
1520
import Test.Hspec.Megaparsec
1621
import Text.Megaparsec
1722
import Prelude hiding ((/))
1823

24+
type data TestStage
25+
26+
newtype instance XCddl TestStage = TestXCddl [Comment]
27+
deriving (Generic, Show, Eq)
28+
29+
instance IndexMappable XCddl ParserStage TestStage where
30+
mapIndex (ParserXCddl x) = TestXCddl x
31+
32+
instance IndexMappable XCddl HuddleStage TestStage where
33+
mapIndex (HuddleXCddl x) = TestXCddl x
34+
35+
newtype instance XTerm TestStage = TestXTerm Comment
36+
deriving (Generic, Show, Eq)
37+
38+
instance IndexMappable XTerm ParserStage TestStage where
39+
mapIndex (ParserXTerm x) = TestXTerm x
40+
41+
instance IndexMappable XTerm HuddleStage TestStage where
42+
mapIndex (HuddleXTerm x) = TestXTerm x
43+
44+
newtype instance XRule TestStage = TestXRule Comment
45+
deriving (Generic, Show, Eq)
46+
47+
instance IndexMappable XRule ParserStage TestStage where
48+
mapIndex (ParserXRule x) = TestXRule x
49+
50+
instance IndexMappable XRule HuddleStage TestStage where
51+
mapIndex (HuddleXRule x _) = TestXRule x
52+
53+
newtype instance XXTopLevel TestStage = TestXXTopLevel Comment
54+
deriving (Generic, Show, Eq)
55+
56+
instance IndexMappable XXTopLevel ParserStage TestStage where
57+
mapIndex (ParserXXTopLevel x) = TestXXTopLevel x
58+
59+
instance IndexMappable XXTopLevel HuddleStage TestStage where
60+
mapIndex (HuddleXXTopLevel x) = TestXXTopLevel x
61+
62+
newtype instance XXType2 TestStage = TestXXType2 Void
63+
deriving (Generic, Show, Eq)
64+
65+
instance IndexMappable XXType2 ParserStage TestStage where
66+
mapIndex (ParserXXType2 x) = TestXXType2 x
67+
68+
instance IndexMappable XXType2 HuddleStage TestStage where
69+
mapIndex (HuddleXXType2 x) = TestXXType2 x
70+
1971
huddleSpec :: Spec
2072
huddleSpec = describe "huddle" $ do
2173
basicAssign
@@ -156,10 +208,10 @@ shouldMatchParse ::
156208
shouldMatchParse x parseFun input = parse parseFun "" (T.pack input) `shouldParse` x
157209

158210
shouldMatchParseCDDL ::
159-
CDDL HuddleStage ->
211+
CDDL TestStage ->
160212
String ->
161213
Expectation
162214
shouldMatchParseCDDL x = shouldMatchParse x . fmap mapIndex $ pCDDL
163215

164-
toSortedCDDL :: Huddle -> CDDL HuddleStage
165-
toSortedCDDL = fromRules . sortCDDL . toCDDLNoRoot
216+
toSortedCDDL :: Huddle -> CDDL TestStage
217+
toSortedCDDL = mapIndex . fromRules . sortCDDL . toCDDLNoRoot

0 commit comments

Comments
 (0)