generic-random 1.3.0.0 → 1.3.0.1
raw patch · 6 files changed
+106/−20 lines, 6 filesdep +inspection-testingdep ~basePVP ok
version bump matches the API change (PVP)
Dependencies added: inspection-testing
Dependency ranges changed: base
API changes (from Hackage documentation)
Files
- CHANGELOG.md +4/−0
- README.md +18/−2
- generic-random.cabal +30/−12
- src/Generic/Random/Internal/Generic.hs +3/−3
- src/Generic/Random/Tutorial.hs +5/−3
- test/Inspect.hs +46/−0
CHANGELOG.md view
@@ -1,5 +1,9 @@ https://github.com/Lysxia/generic-random/blob/master/changelog.md +# 1.3.0.1++- Fix small typos in documentation.+ # 1.3.0.0 - Add `ConstrGen` (custom generators for fields specified by constructor name
README.md view
@@ -1,9 +1,10 @@ Generic random generators [](https://hackage.haskell.org/package/generic-random) [](https://travis-ci.org/Lysxia/generic-random) ========================= -Derive simple random generators for [QuickCheck](https://hackage.haskell.org/package/QuickCheck) using generics.+Generic random generators+to implement `Arbitrary` instances for [QuickCheck](https://hackage.haskell.org/package/QuickCheck) -Automating the `Arbitrary` boilerplate also ensures that when a type changes to+Automating the `arbitrary` boilerplate also ensures that when a type changes to have more or fewer constructors, then the generator either fixes itself to generate that new case (when using the `uniform` distribution) or causes a compilation error so you remember to fix it (when using an explicit@@ -47,3 +48,18 @@ main :: IO () main = sample (arbitrary :: Gen (Tree ())) ```++Related+-------++- The following two packages also derive random generators, but only with a uniform+ distribution of constructors:++ + [quickcheck-arbitrary-template](https://hackage.haskell.org/package/quickcheck-arbitrary-template) (TH)+ + [generic-arbitrary](https://hackage.haskell.org/package/generic-arbitrary-0.1.0) (GHC Generics)++- [testing-feat](http://hackage.haskell.org/package/testing-feat):+ derive enumerations for algebraic data types, which can be turned into random generators (TH).++- [boltzmann-samplers](https://hackage.haskell.org/package/boltzmann-samplers):+ derive Boltzmann samplers (SYB).
generic-random.cabal view
@@ -1,9 +1,14 @@ name: generic-random-version: 1.3.0.0-synopsis: Generic random generators+version: 1.3.0.1+synopsis: Generic random generators for QuickCheck description:+ Derive instances of @Arbitrary@ for QuickCheck,+ with various options to customize implementations.+ . For more information .+ - See the README+ . - "Generic.Random.Tutorial" . - http://blog.poisson.chat/posts/2018-01-05-generic-random-tour.html@@ -11,7 +16,7 @@ homepage: http://github.com/lysxia/generic-random license: MIT license-file: LICENSE-stability: Experimental+stability: Stable author: Li-yao Xia maintainer: lysxia@gmail.com category: Generics, Testing@@ -38,12 +43,25 @@ location: https://github.com/lysxia/generic-random test-suite unit- Hs-source-dirs: test- Main-is: Unit.hs- Build-depends:- base,- deepseq,- QuickCheck,- generic-random- Type: exitcode-stdio-1.0- Default-language: Haskell2010+ hs-source-dirs: test+ main-is: Unit.hs+ build-depends:+ base,+ deepseq,+ QuickCheck,+ generic-random+ type: exitcode-stdio-1.0+ default-language: Haskell2010++test-suite inspect+ hs-source-dirs: test+ main-is: Inspect.hs+ build-depends:+ base,+ QuickCheck,+ inspection-testing,+ generic-random+ type: exitcode-stdio-1.0+ default-language: Haskell2010+ if !impl(ghc >= 8.0.2)+ buildable: False
src/Generic/Random/Internal/Generic.hs view
@@ -113,14 +113,13 @@ -- customGens :: Gen String ':+' Gen Int -- customGens = -- (filter (/= '\NUL') '<$>' arbitrary) ':+'--- (getNonNegative '<$>' arbitrary) ':+'--- ()+-- (getNonNegative '<$>' arbitrary) -- @ -- -- === Note on multiple matches -- -- If the list contains multiple matching types for a field @x@ of type @a@--- (i.e., either @a@ or @'FieldGen' "x" a@), the generator for the first+-- (i.e., either @Gen a@ or @'FieldGen' "x" a@), the generator for the first -- match will be picked. genericArbitraryG :: (GArbitrary (SetGens genList UnsizedOpts) a)@@ -527,6 +526,7 @@ -- > ---------------------+----------------------------- -- > (), () | END -- > (), g :+ gs | g, gs+-- > (), g | g, () when g is not (_ :+ _) -- > g :+ h, gs | g, h :+ gs -- > Gen a, gs | END if matching, else (), gs -- > FieldGen a, gs | idem
src/Generic/Random/Tutorial.hs view
@@ -19,11 +19,13 @@ -- arbitrary = 'genericArbitrary' (9 '%' 8 '%' ()) -- @ ----- @arbitrary :: 'Test.QuickCheck.Gen' (Tree a)@ picks a @Leaf@ with probability 9\/17, or a+-- That random generator @arbitrary :: 'Test.QuickCheck.Gen' (Tree a)@ picks a+-- @Leaf@ with probability 9\/17, or a -- @Node@ with probability 8\/17, and recursively fills their fields with -- @arbitrary@. ----- For @Tree@, 'genericArbitrary' produces code equivalent to the following:+-- For @Tree@, the generic implementation 'genericArbitrary' is equivalent to+-- the following: -- -- @ -- 'genericArbitrary' :: Arbitrary a => 'Weights' (Tree a) -> Gen (Tree a)@@ -43,7 +45,7 @@ -- -- The list of weights is built up with the @('%')@ operator as a cons, and using -- the unit @()@ as the empty list, in the order corresponding to the data type--- definition. The uniform distribution can be obtained with 'uniform'.+-- definition. -- -- == Uniform distribution --
+ test/Inspect.hs view
@@ -0,0 +1,46 @@+{-# LANGUAGE+ DeriveGeneric,+ TemplateHaskell+ #-}+++import GHC.Generics (Generic)+import Test.QuickCheck (Arbitrary(arbitrary), Gen, choose)++import Test.Inspection (inspect, (===))++import Generic.Random++arbMaybe :: Arbitrary a => Gen (Maybe a)+arbMaybe = genericArbitraryU++arbMaybe' :: Arbitrary a => Gen (Maybe a)+arbMaybe' = do+ i <- choose (0, 1 :: Int)+ if i < 1 then+ pure Nothing+ else+ Just <$> arbitrary++data T = A | B | C Int [Bool]+ deriving Generic++arbT :: Gen T+arbT = genericArbitrary (1 % 2 % 3 % ())++arbT' :: Gen T+arbT' = do+ i <- choose (0, 5 :: Int)+ if i < 1 then+ pure A+ else+ if i - 1 < 2 then+ pure B+ else+ C <$> arbitrary <*> arbitrary++main :: IO ()+main = pure ()++inspect $ 'arbMaybe === 'arbMaybe'+inspect $ 'arbT === 'arbT'