hedgehog-fakedata (empty) → 0.0.1.0
raw patch · 7 files changed
+157/−0 lines, 7 filesdep +basedep +containersdep +fakedatasetup-changed
Dependencies added: base, containers, fakedata, hedgehog, hedgehog-fakedata, random
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +16/−0
- Setup.hs +2/−0
- hedgehog-fakedata.cabal +48/−0
- src/Hedgehog/Gen/Faker.hs +42/−0
- test/Main.hs +16/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for hedgehog-faker++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2020++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,16 @@+# hedgehog-faker++[](https://travis-ci.org/parsonsmatt/hedgehog-fakedata)+++This library lets you re-use the [fakedata](https://hackage.haskell.org/package/fakedata) library to quickly and easily generate fake data for use in [`hedgehog`](https://hackage.haskell.org/package/hedgehog) generators.++```haskell+import qualified Hedgehog.Gen as Gen+import qualified Faker.Name as Faker+import Hedgehog.Gen.Faker (fake)++main :: IO ()+main = do+ print =<< Gen.sample (fake Faker.name)+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hedgehog-fakedata.cabal view
@@ -0,0 +1,48 @@+cabal-version: 1.12+name: hedgehog-fakedata+version: 0.0.1.0+description: Please see the README on GitHub at <https://github.com/parsonsmatt/hedgehog-fakedata#readme>+synopsis: Use 'fakedata' with 'hedgehog'+homepage: https://github.com/parsonsmatt/hedgehog-fakedata#readme+bug-reports: https://github.com/parsonsmatt/hedgehog-fakedata/issues+author: Matt Parsons+maintainer: parsonsmatt@gmail.com+copyright: 2020 Matt Parsons+license: BSD3+license-file: LICENSE+build-type: Simple+category: Testing+extra-source-files:+ README.md+ ChangeLog.md+tested-with:+ GHC ==8.8.2 || ==8.6.5 || ==8.4.4 || ==8.2.2++source-repository head+ type: git+ location: https://github.com/parsonsmatt/hedgehog-fakedata++library+ exposed-modules:+ Hedgehog.Gen.Faker+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ , fakedata >= 0.1.0 && < 0.6.0+ , hedgehog >= 0.1 && < 1.1+ , random >= 1.0.0.0 && < 1.2+ default-language: Haskell2010++test-suite specs+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Main.hs+ default-language: Haskell2010+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N+ build-depends: + base+ , hedgehog-fakedata+ , hedgehog+ , fakedata+ , containers
+ src/Hedgehog/Gen/Faker.hs view
@@ -0,0 +1,42 @@+-- | A compatibility module to use "Faker" data in your "Hedgehog" tests.+module Hedgehog.Gen.Faker where++import System.IO.Unsafe (unsafePerformIO)+import qualified Faker+import Faker (Fake)+import System.Random (mkStdGen)++import Hedgehog (Gen)+import qualified Hedgehog.Gen as Gen+import qualified Hedgehog.Range as Range+import qualified Hedgehog.Internal.Gen as InternalGen+import qualified Hedgehog.Internal.Seed as InternalSeed++-- | Select a value 'Fake' program in 'Gen'.+--+-- @since 0.0.1.0+fake :: Fake a -> Gen a+fake f = do+ randomGen <- mkStdGen <$> Gen.integral Range.linearBounded+ 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+ Faker.defaultFakerSettings+ )+ f
+ test/Main.hs view
@@ -0,0 +1,16 @@+module Main where++import Hedgehog+import Faker+import Faker.Name+import Hedgehog.Gen.Faker+import qualified Hedgehog.Gen as Gen+import qualified Data.Set as Set+import Control.Monad++main :: IO ()+main = do+ let nameGen = 5+ xs <- Set.fromList <$> replicateM nameGen (Gen.sample (fake name))+ when (length xs /= nameGen) $+ fail "Should be 5"