fakedata-quickcheck (empty) → 0.1.0
raw patch · 6 files changed
+197/−0 lines, 6 filesdep +QuickCheckdep +basedep +fakedata
Dependencies added: QuickCheck, base, fakedata, fakedata-quickcheck, hspec, hspec-core, random, regex-tdfa, text
Files
- LICENSE +21/−0
- Readme.md +24/−0
- fakedata-quickcheck.cabal +64/−0
- src/Test/QuickCheck/Gen/Faker.hs +48/−0
- test/Spec.hs +1/−0
- test/Test/FakeQuickCheckSpec.hs +39/−0
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2019 Jappie Klooster++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Readme.md view
@@ -0,0 +1,24 @@+> If the truth shall kill them, let them die.++This library allows you to make [quickcheck Generators](https://hackage.haskell.org/package/QuickCheck-2.14.1/docs/Test-QuickCheck-Gen.html#t:Gen)+out of [Fake](https://hackage.haskell.org/package/fakedata-0.8.0/docs/Faker.html#t:Fake) functions from fakedata.+++```haskell+import qualified Faker.Company as CM++isDomain :: Text -> Bool+isDomain = (=~ "^[A-Za-z_]+\\.[a-z]{1,4}$") . T.unpack++spec :: Spec+spec = do+ describe "fakedata quickcheck" $+ it "forall domain fullfils is a domain name regex" $+ Q.property $ Q.forAll (fakeQuickcheck CM.domain) isDomain+```++See the full code in the tests.++---++Added to a separate project as discussed in [here](https://github.com/psibi/fakedata/pull/27)
+ fakedata-quickcheck.cabal view
@@ -0,0 +1,64 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.2.+--+-- see: https://github.com/sol/hpack++name: fakedata-quickcheck+version: 0.1.0+synopsis: Fake a -> Gen a+description: Use fakedata Fake monad for quicheck tests. See readme for examples at <https://github.com/fakedata-haskell/fakedata-quickcheck>.+category: Random, Fake, FakeData+homepage: https://github.com/fakedata-haskell/fakedata-quickcheck#readme+bug-reports: https://github.com/fakedata-haskell/fakedata-quickcheck/issues+author: Jappie Klooster+maintainer: jappieklooster@hotmail.com+copyright: 2020 Jappie Klooster+license: MIT+license-file: LICENSE+build-type: Simple+extra-source-files:+ Readme.md+ LICENSE++source-repository head+ type: git+ location: https://github.com/fakedata-haskell/fakedata-quickcheck++library+ exposed-modules:+ Test.QuickCheck.Gen.Faker+ other-modules:+ Paths_fakedata_quickcheck+ hs-source-dirs:+ src+ default-extensions: EmptyCase FlexibleContexts FlexibleInstances InstanceSigs MultiParamTypeClasses LambdaCase MultiWayIf NamedFieldPuns TupleSections DeriveFoldable DeriveFunctor DeriveGeneric DeriveLift DeriveTraversable DerivingStrategies GeneralizedNewtypeDeriving StandaloneDeriving OverloadedStrings TypeApplications+ ghc-options: -Wall -Wcompat -Wincomplete-uni-patterns -Wredundant-constraints -Wincomplete-record-updates -Widentities+ build-depends:+ QuickCheck >=2.6 && <2.15+ , base >=4.7 && <5+ , fakedata >=0.6.0 && <0.9.0+ , random >=1 && <1.2.0+ default-language: Haskell2010++test-suite unit+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Test.FakeQuickCheckSpec+ Paths_fakedata_quickcheck+ hs-source-dirs:+ test+ default-extensions: EmptyCase FlexibleContexts FlexibleInstances InstanceSigs MultiParamTypeClasses LambdaCase MultiWayIf NamedFieldPuns TupleSections DeriveFoldable DeriveFunctor DeriveGeneric DeriveLift DeriveTraversable DerivingStrategies GeneralizedNewtypeDeriving StandaloneDeriving OverloadedStrings TypeApplications+ ghc-options: -Wall -Wcompat -Wincomplete-uni-patterns -Wredundant-constraints -Wincomplete-record-updates -Widentities+ build-depends:+ QuickCheck >=2.6 && <2.15+ , base >=4.7 && <5+ , fakedata >=0.6.0 && <0.9.0+ , fakedata-quickcheck+ , hspec+ , hspec-core+ , random >=1 && <1.2.0+ , regex-tdfa+ , text+ default-language: Haskell2010
+ src/Test/QuickCheck/Gen/Faker.hs view
@@ -0,0 +1,48 @@+module Test.QuickCheck.Gen.Faker+ ( fakeQuickcheck'+ , fakeQuickcheck+ )+where++import Faker+import System.IO.Unsafe (unsafePerformIO)+import System.Random (mkStdGen)+import qualified Test.QuickCheck as Q++fakeQuickcheck :: Fake a -> Q.Gen a+fakeQuickcheck = fakeQuickcheck' defaultFakerSettings++-- | copied from https://github.com/parsonsmatt/hedgehog-fakedata/blob/d342c6eb5aeb9990bb36ede1d1f08becc7d71e16/src/Hedgehog/Gen/Faker.hs+-- Works in Quickcheck gen instead of hedgehog+--+-- Select a value 'Fake' program in 'Gen'.+--+-- Note that the implementation relies on 'unsafePerformIO'.+-- The faker library uses IO internally for looking up data files.+-- but 'liftIO' in the 'Fake' monad is a gateway to arbitrary side effects.+-- This library doesn't solve that.+fakeQuickcheck' :: FakerSettings -> Fake a -> Q.Gen a+fakeQuickcheck' fakerSettings f = do+ randomGen <- mkStdGen <$> Q.choose (minBound, maxBound)+ pure $!+ unsafePerformIO $+ -- (parsonsmatt): OK so `unsafePerformIO` is bad, unless you know exactly+ -- what you're doing, so do I know exactly what I am doing? Perhaps I can+ -- convince you.+ --+ -- The Faker library doesn't keep the data as Haskell values, but stores it+ -- in `data-files`. The code that generates this fake data loads the values+ -- from the `data-files` for the library. That's what happens in IO. It is+ -- possible that the data-file is missing, and an exception will be thrown.+ -- However, no mutating actions are performed. I believe this is a safe use+ -- of 'unsafePerformIO'.+ --+ -- The alternative would be to lift it into `GenT IO a`, which is+ -- undesirable, as it would harm composition with basically any other+ -- generator.+ Faker.generateWithSettings+ (Faker.setRandomGen+ randomGen+ fakerSettings+ )+ f
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+ test/Test/FakeQuickCheckSpec.hs view
@@ -0,0 +1,39 @@+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE OverloadedStrings #-}++module Test.FakeQuickCheckSpec+ ( spec+ )+where++import qualified Faker.Company as CM+import Faker+import Test.QuickCheck.Gen.Faker+import Data.Text (Text)+import Text.Regex.TDFA hiding (empty)+import qualified Data.Text as T+import qualified Faker.Internet as F+import Test.Hspec+import qualified Test.QuickCheck as Q+import Data.Char(isAlphaNum)+++isDomain :: Text -> Bool+isDomain = (=~ ddd) . T.unpack+ where+ ddd :: Text+ ddd = "^[A-Za-z_]+\\.[a-z]{1,4}$"++spec :: Spec+spec = do+ describe "fakedata quickcheck" $+ it "forall domain fullfils is a domain name regex" $ Q.property $ Q.forAll (fakeQuickcheck domain) isDomain++domain :: Fake Text+domain = do+ suffix <- F.domainSuffix+ companyName <- CM.name+ pure $ fixupName companyName <> "." <> suffix++fixupName :: Text -> Text+fixupName = T.filter (\c -> isAlphaNum c || c == '_') . T.replace " " "_"