|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
1 | 3 | module Test.Codec.CBOR.Cuddle.CDDL.Examples (spec) where
|
2 | 4 |
|
| 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 (..)) |
3 | 8 | 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 | + ) |
5 | 15 | import Codec.CBOR.Cuddle.Parser (pCDDL)
|
6 |
| -import Data.Either (isRight) |
| 16 | +import Data.Functor.Identity (Identity) |
7 | 17 | import Data.Text.IO qualified as T
|
| 18 | +import Test.HUnit (assertFailure) |
8 | 19 | import Test.Hspec
|
9 | 20 | import Text.Megaparsec (parse)
|
10 | 21 | import Text.Megaparsec.Error (errorBundlePretty)
|
11 | 22 |
|
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 |
14 | 25 | contents <- T.readFile filePath
|
15 | 26 | cddl <- case parse pCDDL "" contents of
|
16 | 27 | Right x -> pure $ prependPrelude x
|
17 | 28 | 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 |
19 | 44 |
|
20 | 45 | spec :: Spec
|
21 | 46 | 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