{-Copyright: (c) 2016 Stephen Diehl (c) 20016-2018 Serokell (c) 2018 KowainikLicense: MIT-}module Test.Relude.Property ( hedgehogTestTree ) whereimport Reludeimport Data.List (nub)import Hedgehog (Gen, Property, assert, forAll, property, (===))import Test.Tasty (TestTree, testGroup)import Test.Tasty.Hedgehogimport qualified Hedgehog.Gen as Genimport qualified Hedgehog.Range as RangehedgehogTestTree :: TestTreehedgehogTestTree = testGroup "Tests" [utfProps, listProps, boolMProps]------------------------------------------------------------------------------ utf8 conversion----------------------------------------------------------------------------utfProps :: TestTreeutfProps = testGroup "utf8 conversion property tests" [ testProperty "String to ByteString invertible" prop_StringToBytes , testProperty "Text to ByteString invertible" prop_TextToBytes , testProperty "ByteString to Text or String invertible" prop_BytesTo ]utf8String :: Gen Stringutf8String = Gen.string (Range.linear 0 1000) Gen.unicodeutf8Text :: Gen Textutf8Text = Gen.text (Range.linear 0 1000) Gen.unicodeutf8Bytes :: Gen ByteStringutf8Bytes = Gen.utf8 (Range.linear 0 1000) Gen.unicode-- "\65534" fails, but this is from BU.toString-- > import qualified Data.ByteString.UTF8 as BU-- > BU.toString (BU.fromString "\65534") == "\65533"-- > Trueprop_StringToBytes :: Propertyprop_StringToBytes = property $ do str <- forAll utf8String assert $ str == decodeUtf8 @_ @ByteString (encodeUtf8 str) && str == decodeUtf8 @_ @LByteString (encodeUtf8 str)prop_TextToBytes :: Propertyprop_TextToBytes = property $ do txt <- forAll utf8Text assert $ txt == decodeUtf8 @_ @ByteString (encodeUtf8 txt) && txt == decodeUtf8 @_ @LByteString (encodeUtf8 txt)-- "\239\191\190" fails, but this is the same as "\65534" :: Stringprop_BytesTo :: Propertyprop_BytesTo = property $ do utf <- forAll utf8Bytes assert $ utf == encodeUtf8 @String (decodeUtf8 utf) && utf == encodeUtf8 @Text (decodeUtf8 utf) && utf == encodeUtf8 @LText (decodeUtf8 utf)------------------------------------------------------------------------------ ordNub----------------------------------------------------------------------------listProps :: TestTreelistProps = testGroup "list function property tests" [ testProperty "ordNub xs == nub xs" prop_ordNubCorrect , testProperty "hashNub xs == nub xs" prop_hashNubCorrect , testProperty "sortNub xs == sort (nub xs)" prop_sortNubCorrect , testProperty "sort (unstableNub xs) == sort (nub xs)" prop_unstableNubCorrect ]genIntList :: Gen [Int]genIntList = Gen.list (Range.linear 0 1000) Gen.enumBoundedprop_ordNubCorrect :: Propertyprop_ordNubCorrect = property $ do xs <- forAll genIntList ordNub xs === nub xsprop_hashNubCorrect :: Propertyprop_hashNubCorrect = property $ do xs <- forAll genIntList hashNub xs === ordNub xsprop_sortNubCorrect :: Propertyprop_sortNubCorrect = property $ do xs <- forAll genIntList sortNub xs === sort (ordNub xs)prop_unstableNubCorrect :: Propertyprop_unstableNubCorrect = property $ do xs <- forAll genIntList sort (unstableNub xs) === sortNub xs------------------------------------------------------------------------------ logicM----------------------------------------------------------------------------genBoolList :: Gen [Bool]genBoolList = Gen.list (Range.linear 0 1000) Gen.boolboolMProps :: TestTreeboolMProps = testGroup "lifted logic function property tests" [ testProperty "andM" prop_andM , testProperty "orM" prop_orM ]prop_andM :: Propertyprop_andM = property $ do bs <- forAll genBoolList andM (pure <$> bs) === pure @Maybe (and bs)prop_orM :: Propertyprop_orM = property $ do bs <- forAll genBoolList orM (pure <$> bs) === pure @Maybe (or bs)