{-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, MonadGen, Property, assert, forAll, property, (===))import Test.Tasty (TestTree, testGroup)import Test.Tasty.Hedgehogimport qualified Data.ByteString as Bimport qualified Data.ByteString.Lazy as LBimport qualified Data.Text as Timport qualified Data.Text.Lazy as LTimport qualified Hedgehog.Gen as Genimport qualified Hedgehog.Range as Rangeimport qualified Relude as UhedgehogTestTree :: TestTreehedgehogTestTree = testGroup "Tests" [utfProps, listProps, boolMProps]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 ]unicode' :: MonadGen m => m U.Charunicode' = do a <- Gen.unicode if elem a ['\65534', '\65535'] then unicode' else return autf8String :: Gen U.Stringutf8String = Gen.string (Range.linear 0 10000) unicode'utf8Text :: Gen T.Textutf8Text = Gen.text (Range.linear 0 10000) unicode'utf8Bytes :: Gen B.ByteStringutf8Bytes = Gen.utf8 (Range.linear 0 10000) 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 (encodeUtf8 str :: B.ByteString)) && str == (decodeUtf8 (encodeUtf8 str :: LB.ByteString))prop_TextToBytes :: Propertyprop_TextToBytes = property $ do txt <- forAll utf8Text assert $ txt == (decodeUtf8 (encodeUtf8 txt :: B.ByteString)) && txt == (decodeUtf8 (encodeUtf8 txt :: LB.ByteString))-- "\239\191\190" fails, but this is the same as "\65534" :: Stringprop_BytesTo :: Propertyprop_BytesTo = property $ do utf <- forAll utf8Bytes assert $ utf == (encodeUtf8 (decodeUtf8 utf :: U.String)) && utf == (encodeUtf8 (decodeUtf8 utf :: T.Text)) && utf == (encodeUtf8 (decodeUtf8 utf :: LT.Text))-- ordNublistProps :: TestTreelistProps = testGroup "list function property tests" [ testProperty "Hedgehog ordNub xs == nub xs" prop_ordNubCorrect , testProperty "Hedgehog hashNub xs == nub xs" prop_hashNubCorrect , testProperty "Hedgehog sortNub xs == sort $ nub xs" prop_sortNubCorrect , testProperty "Hedgehog sort $ unstableNub xs == sort $ nub xs" prop_unstableNubCorrect ]genIntList :: Gen [U.Int]genIntList = Gen.list (Range.linear 0 10000) Gen.enumBoundedprop_ordNubCorrect :: Propertyprop_ordNubCorrect = property $ do xs <- forAll genIntList U.ordNub xs === nub xsprop_hashNubCorrect :: Propertyprop_hashNubCorrect = property $ do xs <- forAll genIntList U.hashNub xs === nub xsprop_sortNubCorrect :: Propertyprop_sortNubCorrect = property $ do xs <- forAll genIntList U.sortNub xs === (U.sort $ nub xs)prop_unstableNubCorrect :: Propertyprop_unstableNubCorrect = property $ do xs <- forAll genIntList (U.sort $ U.unstableNub xs) === (U.sort $ nub xs)-- logicM-- this section needs a little more thoughtgenBoolList :: Gen [U.Bool]genBoolList = Gen.list (Range.linear 0 1000) Gen.boolboolMProps :: TestTreeboolMProps = testGroup "lifted logic function property tests" [ testProperty "Hedgehog andM" prop_andM , testProperty "Hedgehog orM" prop_orM ]prop_andM :: Propertyprop_andM = property $ do bs <- forAll genBoolList U.andM (return <$> bs) === ((return $ U.and bs) :: U.Maybe U.Bool)prop_orM :: Propertyprop_orM = property $ do bs <- forAll genBoolList U.orM (return <$> bs) === ((return $ U.or bs) :: U.Maybe U.Bool)