Skip to content

Commit a32f413

Browse files
committed
Added more validator tests
1 parent 7fe3abb commit a32f413

File tree

8 files changed

+61
-22
lines changed

8 files changed

+61
-22
lines changed

example/cddl-files/basic_assign.cddl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,3 @@ big_group = (
4444
5 ...10,
4545
h'11aaff3351bc'
4646
)
47-
48-
test = ~ aaaa .. "j" /
49-
# /
50-
{xco, lhXH // // } .cborseq # /
51-
& (* kkhw // // ) /
52-
"b"

example/cddl-files/pretty.cddl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
set<a > = [* a]
2+
13
a = [ 2*30 2 : uint
24
, ? 33 : bytes
35
, 4444 : set<uint>
@@ -6,6 +8,6 @@ a = [ 2*30 2 : uint
68

79
b = [1,uint,(3,4)]
810

9-
c = { x
10-
, y ; hello
11+
c = { a
12+
, b ; hello
1113
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x = uint<3>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo<a, b> = [a, b]
2+
3+
x = foo<uint>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x = a

src/Codec/CBOR/Cuddle/CDDL/CTree.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE UndecidableInstances #-}
23

34
module Codec.CBOR.Cuddle.CDDL.CTree where
45

@@ -46,6 +47,8 @@ data CTree f
4647
| Tag Word64 (Node f)
4748
deriving (Generic)
4849

50+
deriving instance Eq (Node f) => Eq (CTree f)
51+
4952
-- | Traverse the CTree, carrying out the given operation at each node
5053
traverseCTree :: Monad m => (Node f -> m (Node g)) -> CTree f -> m (CTree g)
5154
traverseCTree _ (Literal a) = pure $ Literal a

src/Codec/CBOR/Cuddle/CDDL/Resolve.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Codec.CBOR.Cuddle.CDDL.Resolve (
3030
buildMonoCTree,
3131
fullResolveCDDL,
3232
MonoRef (..),
33+
OrRef (..),
3334
NameResolutionFailure (..),
3435
)
3536
where
@@ -128,7 +129,7 @@ data OrRef a
128129
It a
129130
| -- | Reference to another node with possible generic arguments supplied
130131
Ref Name [CTree.Node OrRef]
131-
deriving (Show, Functor)
132+
deriving (Eq, Show, Functor)
132133

133134
type RefCTree = CTreeRoot OrRef
134135

@@ -303,6 +304,8 @@ data NameResolutionFailure
303304
| ArgsToPostlude PTerm [CTree.Node OrRef]
304305
deriving (Show)
305306

307+
deriving instance Eq (OrRef (CTree OrRef)) => Eq NameResolutionFailure
308+
306309
postludeBinding :: Map.Map Name PTerm
307310
postludeBinding =
308311
Map.fromList
@@ -342,8 +345,6 @@ instance Hashable a => Hashable (DistRef a)
342345

343346
deriving instance Show (CTree DistRef)
344347

345-
deriving instance Eq (CTree DistRef)
346-
347348
instance Hashable (CTree DistRef)
348349

349350
deriving instance Show (CTreeRoot DistRef)
Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
13
module Test.Codec.CBOR.Cuddle.CDDL.Examples (spec) where
24

5+
import Codec.CBOR.Cuddle.CDDL (Value (..), ValueVariant (..))
6+
import Codec.CBOR.Cuddle.CDDL.CTree (CTree (..), CTreeRoot')
7+
import Codec.CBOR.Cuddle.CDDL.Postlude (PTerm (..))
38
import Codec.CBOR.Cuddle.CDDL.Prelude (prependPrelude)
4-
import Codec.CBOR.Cuddle.CDDL.Resolve (fullResolveCDDL)
9+
import Codec.CBOR.Cuddle.CDDL.Resolve (
10+
MonoRef,
11+
NameResolutionFailure (..),
12+
OrRef (..),
13+
fullResolveCDDL,
14+
)
515
import Codec.CBOR.Cuddle.Parser (pCDDL)
6-
import Data.Either (isRight)
16+
import Data.Functor.Identity (Identity)
717
import Data.Text.IO qualified as T
18+
import Test.HUnit (assertFailure)
819
import Test.Hspec
920
import Text.Megaparsec (parse)
1021
import Text.Megaparsec.Error (errorBundlePretty)
1122

12-
validateFile :: FilePath -> Spec
13-
validateFile filePath = it ("Successfully validates " <> filePath) $ do
23+
tryValidateFile :: FilePath -> IO (Either NameResolutionFailure (CTreeRoot' Identity MonoRef))
24+
tryValidateFile filePath = do
1425
contents <- T.readFile filePath
1526
cddl <- case parse pCDDL "" contents of
1627
Right x -> pure $ prependPrelude x
1728
Left x -> fail $ "Failed to parse the file:\n" <> errorBundlePretty x
18-
fullResolveCDDL cddl `shouldSatisfy` isRight
29+
pure $ fullResolveCDDL cddl
30+
31+
validateExpectSuccess :: FilePath -> Spec
32+
validateExpectSuccess filePath = it ("Successfully validates " <> filePath) $ do
33+
res <- tryValidateFile filePath
34+
case res of
35+
Right _ -> pure ()
36+
Left err -> assertFailure $ "Failed to validate:\n" <> show err
37+
38+
validateExpectFailure :: FilePath -> NameResolutionFailure -> Spec
39+
validateExpectFailure filePath expectedFailure = it ("Fails to validate " <> filePath) $ do
40+
res <- tryValidateFile filePath
41+
case res of
42+
Right _ -> assertFailure "Expected a failure, but succeeded instead"
43+
Left e -> e `shouldBe` expectedFailure
1944

2045
spec :: Spec
2146
spec = do
22-
validateFile "example/cddl-files/byron.cddl"
23-
validateFile "example/cddl-files/conway.cddl"
24-
validateFile "example/cddl-files/shelley.cddl"
25-
26-
-- TODO this one does not seem to terminate
27-
-- validateFile "example/cddl-files/basic_assign.cddl"
47+
describe "Validator" $ do
48+
describe "Positive" $ do
49+
validateExpectSuccess "example/cddl-files/byron.cddl"
50+
validateExpectSuccess "example/cddl-files/conway.cddl"
51+
validateExpectSuccess "example/cddl-files/shelley.cddl"
52+
validateExpectSuccess "example/cddl-files/basic_assign.cddl"
53+
validateExpectSuccess "example/cddl-files/issue80-min.cddl"
54+
validateExpectSuccess "example/cddl-files/pretty.cddl"
55+
describe "Negative" $ do
56+
validateExpectFailure "example/cddl-files/validator/negative/unknown-name.cddl" $
57+
UnboundReference "a"
58+
validateExpectFailure "example/cddl-files/validator/negative/mismatching-args.cddl" $
59+
MismatchingArgs "foo" ["a", "b"]
60+
validateExpectFailure "example/cddl-files/validator/negative/args-to-postlude.cddl" $
61+
ArgsToPostlude PTUInt [It (Literal (Value (VUInt 3) mempty))]

0 commit comments

Comments
 (0)