From 26bafc1696b7f329f3abfd1ac144a43245d3ccdb Mon Sep 17 00:00:00 2001 From: Tom Sydney Kerckhove Date: Tue, 2 Nov 2021 21:55:11 +0100 Subject: [PATCH] golden test for the show output --- .../autodocodec-api-usage.cabal | 1 + .../test/Autodocodec/ShowSpec.hs | 61 +++++++++++++++++++ .../test_resources/show-codec/bool.txt | 1 + .../test_resources/show-codec/char.txt | 1 + .../show-codec/either-bool-text.txt | 1 + .../either-either-bool-scientific-text.txt | 1 + .../test_resources/show-codec/example.txt | 1 + .../test_resources/show-codec/fruit.txt | 1 + .../test_resources/show-codec/int.txt | 1 + .../test_resources/show-codec/int16.txt | 1 + .../test_resources/show-codec/int32.txt | 1 + .../test_resources/show-codec/int64.txt | 1 + .../test_resources/show-codec/int8.txt | 1 + .../test_resources/show-codec/lazy-text.txt | 1 + .../test_resources/show-codec/list-text.txt | 1 + .../test_resources/show-codec/maybe-text.txt | 1 + .../test_resources/show-codec/ordering.txt | 1 + .../test_resources/show-codec/recursive.txt | 1 + .../test_resources/show-codec/scientific.txt | 1 + .../test_resources/show-codec/string.txt | 1 + .../test_resources/show-codec/text.txt | 1 + .../test_resources/show-codec/value.txt | 1 + .../test_resources/show-codec/via.txt | 1 + .../test_resources/show-codec/word.txt | 1 + .../test_resources/show-codec/word16.txt | 1 + .../test_resources/show-codec/word32.txt | 1 + .../test_resources/show-codec/word64.txt | 1 + .../test_resources/show-codec/word8.txt | 1 + 28 files changed, 88 insertions(+) create mode 100644 autodocodec-api-usage/test/Autodocodec/ShowSpec.hs create mode 100644 autodocodec-api-usage/test_resources/show-codec/bool.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/char.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/either-bool-text.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/either-either-bool-scientific-text.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/example.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/fruit.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/int.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/int16.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/int32.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/int64.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/int8.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/lazy-text.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/list-text.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/maybe-text.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/ordering.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/recursive.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/scientific.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/string.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/text.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/value.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/via.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/word.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/word16.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/word32.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/word64.txt create mode 100644 autodocodec-api-usage/test_resources/show-codec/word8.txt diff --git a/autodocodec-api-usage/autodocodec-api-usage.cabal b/autodocodec-api-usage/autodocodec-api-usage.cabal index ef51121..e3cf581 100644 --- a/autodocodec-api-usage/autodocodec-api-usage.cabal +++ b/autodocodec-api-usage/autodocodec-api-usage.cabal @@ -52,6 +52,7 @@ test-suite autodocodec-api-usage-test other-modules: Autodocodec.Aeson.SchemaSpec Autodocodec.AesonSpec + Autodocodec.ShowSpec Autodocodec.SwaggerSpec Autodocodec.Yaml.DocumentSpec Paths_autodocodec_api_usage diff --git a/autodocodec-api-usage/test/Autodocodec/ShowSpec.hs b/autodocodec-api-usage/test/Autodocodec/ShowSpec.hs new file mode 100644 index 0000000..692541a --- /dev/null +++ b/autodocodec-api-usage/test/Autodocodec/ShowSpec.hs @@ -0,0 +1,61 @@ +{-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} + +module Autodocodec.ShowSpec (spec) where + +import Autodocodec +import Autodocodec.Aeson +import Autodocodec.Usage +import Data.Aeson (FromJSON (..), ToJSON (..)) +import qualified Data.Aeson as JSON +import qualified Data.Aeson.Types as JSON +import Data.Data +import Data.GenValidity +import Data.GenValidity.Aeson () +import Data.GenValidity.Scientific () +import Data.GenValidity.Text () +import Data.Int +import Data.Scientific +import Data.Text (Text) +import qualified Data.Text.Lazy as LT +import Data.Word +import Test.Syd +import Test.Syd.Validity +import Test.Syd.Validity.Utils + +spec :: Spec +spec = do + showCodecSpec @Bool "bool" + showCodecSpec @Ordering "ordering" + showCodecSpec @Char "char" + showCodecSpec @Text "text" + showCodecSpec @LT.Text "lazy-text" + showCodecSpec @String "string" + showCodecSpec @Scientific "scientific" + showCodecSpec @JSON.Value "value" + showCodecSpec @Int "int" + showCodecSpec @Int8 "int8" + showCodecSpec @Int16 "int16" + showCodecSpec @Int32 "int32" + showCodecSpec @Int64 "int64" + showCodecSpec @Word "word" + showCodecSpec @Word8 "word8" + showCodecSpec @Word16 "word16" + showCodecSpec @Word32 "word32" + showCodecSpec @Word64 "word64" + showCodecSpec @(Maybe Text) "maybe-text" + showCodecSpec @(Either Bool Text) "either-bool-text" + showCodecSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text" + showCodecSpec @[Text] "list-text" + showCodecSpec @Fruit "fruit" + showCodecSpec @Example "example" + showCodecSpec @Recursive "recursive" + showCodecSpec @Via "via" + +showCodecSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, HasCodec a) => FilePath -> Spec +showCodecSpec filePath = + describe ("showCodecSpec " <> nameOf @a) $ + it "outputs the same shown codec information as before" $ + pureGoldenStringFile ("test_resources/show-codec/" <> filePath <> ".txt") (showCodecABit (codec @a)) diff --git a/autodocodec-api-usage/test_resources/show-codec/bool.txt b/autodocodec-api-usage/test_resources/show-codec/bool.txt new file mode 100644 index 0000000..2ee0212 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/bool.txt @@ -0,0 +1 @@ +BoolCodec Nothing \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/char.txt b/autodocodec-api-usage/test_resources/show-codec/char.txt new file mode 100644 index 0000000..b0c550d --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/char.txt @@ -0,0 +1 @@ +MapCodec (MapCodec (StringCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/either-bool-text.txt b/autodocodec-api-usage/test_resources/show-codec/either-bool-text.txt new file mode 100644 index 0000000..261914a --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/either-bool-text.txt @@ -0,0 +1 @@ +EitherCodec (ObjectOfCodec Nothing (RequiredKeyCodec "Left" Nothing (BoolCodec Nothing))) (ObjectOfCodec Nothing (RequiredKeyCodec "Right" Nothing (StringCodec Nothing))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/either-either-bool-scientific-text.txt b/autodocodec-api-usage/test_resources/show-codec/either-either-bool-scientific-text.txt new file mode 100644 index 0000000..ea764ab --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/either-either-bool-scientific-text.txt @@ -0,0 +1 @@ +EitherCodec (ObjectOfCodec Nothing (RequiredKeyCodec "Left" Nothing (EitherCodec (ObjectOfCodec Nothing (RequiredKeyCodec "Left" Nothing (BoolCodec Nothing))) (ObjectOfCodec Nothing (RequiredKeyCodec "Right" Nothing (NumberCodec Nothing)))))) (ObjectOfCodec Nothing (RequiredKeyCodec "Right" Nothing (StringCodec Nothing))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/example.txt b/autodocodec-api-usage/test_resources/show-codec/example.txt new file mode 100644 index 0000000..10383b3 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/example.txt @@ -0,0 +1 @@ +ObjectOfCodec Just "Example" (ApCodec (ApCodec (ApCodec (ApCodec (ApCodec (ApCodec (MapCodec (MapCodec (RequiredKeyCodec "text" (Just "a text") (StringCodec Nothing)))) (MapCodec (RequiredKeyCodec "bool" (Just "a bool") (BoolCodec Nothing)))) (MapCodec (RequiredKeyCodec "maybe" (Just "a maybe text") (MapCodec (EitherCodec NullCodec (StringCodec Nothing)))))) (MapCodec (OptionalKeyCodec "optional" (Just "an optional text") (StringCodec Nothing)))) (MapCodec (MapCodec (OptionalKeyCodec "optional-or-null" (Just "an optional-or-null text") (MapCodec (EitherCodec NullCodec (StringCodec Nothing))))))) (MapCodec (OptionalKeyWithDefaultCodec "optional-with-default" (StringCodec Nothing) (Just "an optional text with a default")))) (MapCodec (RequiredKeyCodec "fruit" (Just "fruit!!") (MapCodec (EitherCodec (MapCodec (EqCodec "Apple" (StringCodec Nothing))) (MapCodec (EitherCodec (MapCodec (EqCodec "Orange" (StringCodec Nothing))) (MapCodec (EitherCodec (MapCodec (EqCodec "Banana" (StringCodec Nothing))) (MapCodec (EqCodec "Melon" (StringCodec Nothing)))))))))))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/fruit.txt b/autodocodec-api-usage/test_resources/show-codec/fruit.txt new file mode 100644 index 0000000..7fcc1bf --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/fruit.txt @@ -0,0 +1 @@ +MapCodec (EitherCodec (MapCodec (EqCodec "Apple" (StringCodec Nothing))) (MapCodec (EitherCodec (MapCodec (EqCodec "Orange" (StringCodec Nothing))) (MapCodec (EitherCodec (MapCodec (EqCodec "Banana" (StringCodec Nothing))) (MapCodec (EqCodec "Melon" (StringCodec Nothing)))))))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/int.txt b/autodocodec-api-usage/test_resources/show-codec/int.txt new file mode 100644 index 0000000..8882ec2 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/int.txt @@ -0,0 +1 @@ +CommentCodec "Int" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/int16.txt b/autodocodec-api-usage/test_resources/show-codec/int16.txt new file mode 100644 index 0000000..e9fb88e --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/int16.txt @@ -0,0 +1 @@ +CommentCodec "Int16" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/int32.txt b/autodocodec-api-usage/test_resources/show-codec/int32.txt new file mode 100644 index 0000000..2c5019c --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/int32.txt @@ -0,0 +1 @@ +CommentCodec "Int32" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/int64.txt b/autodocodec-api-usage/test_resources/show-codec/int64.txt new file mode 100644 index 0000000..da9deca --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/int64.txt @@ -0,0 +1 @@ +CommentCodec "Int64" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/int8.txt b/autodocodec-api-usage/test_resources/show-codec/int8.txt new file mode 100644 index 0000000..2b72d68 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/int8.txt @@ -0,0 +1 @@ +CommentCodec "Int8" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/lazy-text.txt b/autodocodec-api-usage/test_resources/show-codec/lazy-text.txt new file mode 100644 index 0000000..0d62b49 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/lazy-text.txt @@ -0,0 +1 @@ +MapCodec (StringCodec Nothing) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/list-text.txt b/autodocodec-api-usage/test_resources/show-codec/list-text.txt new file mode 100644 index 0000000..e5d8bc6 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/list-text.txt @@ -0,0 +1 @@ +MapCodec (ArrayOfCodec Nothing (StringCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/maybe-text.txt b/autodocodec-api-usage/test_resources/show-codec/maybe-text.txt new file mode 100644 index 0000000..b416ddc --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/maybe-text.txt @@ -0,0 +1 @@ +MapCodec (EitherCodec NullCodec (StringCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/ordering.txt b/autodocodec-api-usage/test_resources/show-codec/ordering.txt new file mode 100644 index 0000000..b8ed304 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/ordering.txt @@ -0,0 +1 @@ +MapCodec (EitherCodec (MapCodec (EqCodec "LT" (StringCodec Nothing))) (MapCodec (EitherCodec (MapCodec (EqCodec "EQ" (StringCodec Nothing))) (MapCodec (EqCodec "GT" (StringCodec Nothing)))))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/recursive.txt b/autodocodec-api-usage/test_resources/show-codec/recursive.txt new file mode 100644 index 0000000..7996387 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/recursive.txt @@ -0,0 +1 @@ +ReferenceCodec "recursive" MapCodec (EitherCodec (CommentCodec "base case" (CommentCodec "Int" (MapCodec (NumberCodec Nothing)))) (ObjectOfCodec (Just "Recurse") (RequiredKeyCodec "recurse" (Just "recursive case") (ReferenceCodec "recursive")))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/scientific.txt b/autodocodec-api-usage/test_resources/show-codec/scientific.txt new file mode 100644 index 0000000..b7d773e --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/scientific.txt @@ -0,0 +1 @@ +NumberCodec Nothing \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/string.txt b/autodocodec-api-usage/test_resources/show-codec/string.txt new file mode 100644 index 0000000..0d62b49 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/string.txt @@ -0,0 +1 @@ +MapCodec (StringCodec Nothing) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/text.txt b/autodocodec-api-usage/test_resources/show-codec/text.txt new file mode 100644 index 0000000..7688e2e --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/text.txt @@ -0,0 +1 @@ +StringCodec Nothing \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/value.txt b/autodocodec-api-usage/test_resources/show-codec/value.txt new file mode 100644 index 0000000..7ed6c34 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/value.txt @@ -0,0 +1 @@ +ValueCodec \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/via.txt b/autodocodec-api-usage/test_resources/show-codec/via.txt new file mode 100644 index 0000000..9c07132 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/via.txt @@ -0,0 +1 @@ +ObjectOfCodec Just "Via" (ApCodec (MapCodec (MapCodec (RequiredKeyCodec "one" (Just "first field") (StringCodec Nothing)))) (MapCodec (RequiredKeyCodec "two" (Just "second field") (StringCodec Nothing)))) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/word.txt b/autodocodec-api-usage/test_resources/show-codec/word.txt new file mode 100644 index 0000000..95e3d58 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/word.txt @@ -0,0 +1 @@ +CommentCodec "Word" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/word16.txt b/autodocodec-api-usage/test_resources/show-codec/word16.txt new file mode 100644 index 0000000..eb6c0b1 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/word16.txt @@ -0,0 +1 @@ +CommentCodec "Word16" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/word32.txt b/autodocodec-api-usage/test_resources/show-codec/word32.txt new file mode 100644 index 0000000..8328fc9 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/word32.txt @@ -0,0 +1 @@ +CommentCodec "Word32" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/word64.txt b/autodocodec-api-usage/test_resources/show-codec/word64.txt new file mode 100644 index 0000000..6f1af93 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/word64.txt @@ -0,0 +1 @@ +CommentCodec "Word64" (MapCodec (NumberCodec Nothing)) \ No newline at end of file diff --git a/autodocodec-api-usage/test_resources/show-codec/word8.txt b/autodocodec-api-usage/test_resources/show-codec/word8.txt new file mode 100644 index 0000000..249ebb2 --- /dev/null +++ b/autodocodec-api-usage/test_resources/show-codec/word8.txt @@ -0,0 +1 @@ +CommentCodec "Word8" (MapCodec (NumberCodec Nothing)) \ No newline at end of file