-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
671e5aa
commit cddcaea
Showing
7 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,19 +1,57 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | | ||
-- Module : Test.Lowarn.Property | ||
-- SPDX-License-Identifier : MIT | ||
-- Stability : stable | ||
-- Portability : portable | ||
-- | ||
-- Module for property testing utilities for Lowarn. | ||
module Test.Lowarn.Property (roundTripProperty) where | ||
module Test.Lowarn.Property | ||
( roundTripPropertyShow, | ||
parserCombinatorRoundTripProperty, | ||
aesonRoundTripProperty, | ||
) | ||
where | ||
|
||
import Data.Aeson | ||
import Data.Proxy | ||
import Data.Text.Lazy | ||
import Data.Text.Lazy.Encoding | ||
import Lowarn.ParserCombinators | ||
import Test.QuickCheck | ||
import Test.QuickCheck hiding (Success) | ||
import Text.ParserCombinators.ReadP | ||
|
||
-- | Give the property that the result of applying a custom function to a type | ||
-- and parsing the result is the original value, with a custom show function | ||
-- used for error messages. | ||
roundTripPropertyShow :: | ||
(Arbitrary a, Eq a) => (a -> String) -> (a -> b) -> (b -> Maybe a) -> Property | ||
roundTripPropertyShow customShow to parse = | ||
propertyForAllShrinkShow arbitrary shrink (return . customShow) $ | ||
\x -> Just x == parse (to x) | ||
|
||
-- | Give the property that the result of parsing the result of showing a value, | ||
-- with a given custom parser and show function, is the original value. | ||
roundTripProperty :: (Arbitrary a, Eq a) => (a -> String) -> ReadP a -> Property | ||
roundTripProperty customShow customParse = | ||
propertyForAllShrinkShow arbitrary shrink (return . customShow) $ | ||
\a -> Just a == readWithParser customParse (customShow a) | ||
parserCombinatorRoundTripProperty :: | ||
(Arbitrary a, Eq a) => (a -> String) -> ReadP a -> Property | ||
parserCombinatorRoundTripProperty customShow customParse = | ||
roundTripPropertyShow customShow customShow (readWithParser customParse) | ||
|
||
-- | Give the property that the result of converting a given value to and from | ||
-- JSON is the original value. | ||
aesonRoundTripProperty :: | ||
forall a. (Arbitrary a, Eq a, ToJSON a, FromJSON a) => Proxy a -> Property | ||
aesonRoundTripProperty = | ||
const $ | ||
roundTripPropertyShow @a | ||
(unpack . decodeUtf8 . encode) | ||
toJSON | ||
( ( \case | ||
Error _ -> Nothing | ||
Success x -> Just x | ||
) | ||
. fromJSON | ||
) |
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