-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for GHC 9.6 * Fix bug in `mkNullableNonEmptyText` that incorrectly counted leading and trailing whitespace against the character limit. * Improve documentation for `NullableNonEmptyText`
- Loading branch information
1 parent
40db4d1
commit 598048d
Showing
9 changed files
with
150 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
-- HI! for pants reasons, if you add anything load bearing in here, you have to | ||
-- add it to raw-project in cabal.haskell-ci | ||
-- add it to `generate cabal.project` in `.github/workflows/haskell-ci.yml` | ||
packages: . | ||
|
||
tests: True | ||
|
||
allow-newer: | ||
refined:aeson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{-# LANGUAGE CPP #-} | ||
|
||
module Specs.JsonEncodingSpec (spec) where | ||
|
||
import Data.Aeson | ||
import Data.StringVariants.NonEmptyText | ||
import Data.StringVariants.NonEmptyText.Internal | ||
import Data.StringVariants.NullableNonEmptyText | ||
import GHC.Generics | ||
import Prelude | ||
import Test.Hspec | ||
|
||
data Person = Person {name :: NonEmptyText 6, catchphrase :: NullableNonEmptyText 15} | ||
deriving stock (Eq, Generic, Show) | ||
deriving anyclass (FromJSON, ToJSON) | ||
|
||
spec :: Spec | ||
spec = describe "FromJSON instances" $ do | ||
describe "NonEmptyText" $ do | ||
it "rejects strings that are too long" $ | ||
assertParseFailure $ | ||
object ["name" .= replicate 7 'a', "catchphrase" .= String "Yabba-Dabba Do!"] | ||
it "rejects missing properties" $ | ||
assertParseFailure $ | ||
object ["catchphrase" .= String "Yabba-Dabba Do!"] | ||
it "rejects null properties" $ | ||
assertParseFailure $ | ||
object ["name" .= Null, "catchphrase" .= String "Yabba-Dabba Do!"] | ||
it "rejects empty strings" $ | ||
assertParseFailure $ | ||
object ["name" .= String "", "catchphrase" .= String "Yabba-Dabba Do!"] | ||
it "rejects whitespace strings" $ | ||
assertParseFailure $ | ||
object ["name" .= String " ", "catchphrase" .= String "Yabba-Dabba Do!"] | ||
it "strips whitespace" $ | ||
object ["name" .= String " Daniel ", "catchphrase" .= String "Yabba-Dabba Do!"] | ||
`shouldParseAs` | ||
Person (NonEmptyText "Daniel") (NullableNonEmptyText $ Just $ NonEmptyText "Yabba-Dabba Do!") | ||
describe "NullableNonEmptyText" $ do | ||
it "rejects strings that are too long" $ | ||
assertParseFailure $ | ||
object ["name" .= String "Daniel", "catchphrase" .= replicate 16 'a'] | ||
#if MIN_VERSION_aeson(2,2,0) | ||
it "accepts missing properties" $ | ||
object ["name" .= String "Daniel"] | ||
`shouldParseAs` | ||
Person (NonEmptyText "Daniel") (NullableNonEmptyText Nothing) | ||
it "accepts null properties" $ | ||
object ["name" .= String "Daniel", "catchphrase" .= Null] | ||
`shouldParseAs` | ||
Person (NonEmptyText "Daniel") (NullableNonEmptyText Nothing) | ||
#endif | ||
it "accepts empty strings" $ | ||
object ["name" .= String "Daniel", "catchphrase" .= String ""] | ||
`shouldParseAs` | ||
Person (NonEmptyText "Daniel") (NullableNonEmptyText Nothing) | ||
it "accepts whitespace strings" $ | ||
object ["name" .= String "Daniel", "catchphrase" .= String " "] | ||
`shouldParseAs` | ||
Person (NonEmptyText "Daniel") (NullableNonEmptyText Nothing) | ||
it "strips whitespace" $ | ||
object ["name" .= String "Daniel", "catchphrase" .= String " Yabba-Dabba Do! "] | ||
`shouldParseAs` | ||
Person (NonEmptyText "Daniel") (NullableNonEmptyText $ Just $ NonEmptyText "Yabba-Dabba Do!") | ||
where | ||
assertParseFailure :: HasCallStack => Value -> IO () | ||
assertParseFailure val = | ||
decode @Person (encode val) `shouldBe` Nothing | ||
|
||
shouldParseAs :: HasCallStack => Value -> Person -> IO () | ||
shouldParseAs val person = | ||
decode (encode val) `shouldBe` Just person |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters