packages feed

proto-lens-arbitrary 0.1.1.1 → 0.1.2.0

raw patch · 3 files changed

+66/−39 lines, 3 filesdep ~proto-lensPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: proto-lens

API changes (from Hackage documentation)

+ Data.ProtoLens.Arbitrary: shrinkMessage :: Message a => a -> [a]

Files

Changelog.md view
@@ -1,6 +1,12 @@ # Changelog for `proto-lens-arbitrary` -## Unreleased changes+## v0.1.2.0+- Remove support for `ghc-7.10`. (#136)+- Use a `.cabal` file that's auto-generated from `hpack`. (#138)+- Track `proto-lens` changes to the `Message` class. (#139, #147)+- Expose `shrinkMessage` (#159)+- Improve the `Arbitrary` generator for nested structures (#163)+  ## v0.1.1.1 - Bump the dependency on `base` to support `ghc-8.2.1`.
proto-lens-arbitrary.cabal view
@@ -1,28 +1,45 @@-name:                proto-lens-arbitrary-version:             0.1.1.1-synopsis:            Arbitrary instances for proto-lens.-description:-  The proto-lens-arbitrary allows generating arbitrary messages for-  use with QuickCheck.+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: 9e84da8b472df0355cae508146fec0dacd00229345ed731b95cc3936649a2364 -homepage:            https://github.com/google/proto-lens-license:             BSD3-license-file:        LICENSE-author:              Aden Grue-maintainer:          agrue+protolens@google.com-copyright:           Google Inc.-category:            Data-build-type:          Simple-cabal-version:       >=1.8-extra-source-files:  Changelog.md+name:           proto-lens-arbitrary+version:        0.1.2.0+synopsis:       Arbitrary instances for proto-lens.+description:    The proto-lens-arbitrary allows generating arbitrary messages for use with QuickCheck.+category:       Data+homepage:       https://github.com/google/proto-lens#readme+bug-reports:    https://github.com/google/proto-lens/issues+author:         Aden Grue+maintainer:     agrue+protolens@google.com+copyright:      Google Inc.+license:        BSD3+license-file:   LICENSE+build-type:     Simple+cabal-version:  >= 1.10 +extra-source-files:+    Changelog.md++source-repository head+  type: git+  location: https://github.com/google/proto-lens+  subdir: proto-lens-arbitrary+ library-  hs-source-dirs: src-  exposed-modules:     Data.ProtoLens.Arbitrary-  build-depends:  proto-lens >= 0.1 && < 0.3-                , base >= 4.8 && < 4.11-                , bytestring == 0.10.*-                , containers == 0.5.*-                , text == 1.2.*-                , lens-family == 1.2.*-                , QuickCheck >= 2.8 && < 2.11+  hs-source-dirs:+      src+  build-depends:+      QuickCheck >=2.8 && <2.11+    , base >=4.8 && <4.11+    , bytestring ==0.10.*+    , containers ==0.5.*+    , lens-family ==1.2.*+    , proto-lens ==0.3.*+    , text ==1.2.*+  exposed-modules:+      Data.ProtoLens.Arbitrary+  other-modules:+      Paths_proto_lens_arbitrary+  default-language: Haskell2010
src/Data/ProtoLens/Arbitrary.hs view
@@ -6,11 +6,13 @@  {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE GADTs #-}+{-# LANGUAGE LambdaCase #-} {-# LANGUAGE RankNTypes #-} -- | An Arbitrary instance for protocol buffer Messages to use with QuickCheck. module Data.ProtoLens.Arbitrary     ( ArbitraryMessage(..)     , arbitraryMessage+    , shrinkMessage     ) where  import Data.ProtoLens.Message@@ -25,7 +27,7 @@ import Lens.Family2 (Lens', view, set) import Lens.Family2.Unchecked (lens) import Test.QuickCheck (Arbitrary(..), Gen, suchThat, frequency, listOf,-                        shrinkList)+                        shrinkList, scale)   -- | A newtype wrapper that provides an Arbitrary instance for the underlying@@ -38,9 +40,7 @@     shrink (ArbitraryMessage a) = ArbitraryMessage <$> shrinkMessage a  arbitraryMessage :: Message a => Gen a-arbitraryMessage = foldM (flip arbitraryField) def fields-  where-    fields = M.elems (fieldsByTag descriptor)+arbitraryMessage = foldM (flip arbitraryField) def allFields  -- | Imitation of the (Arbitrary a => Arbitrary (Maybe a)) instance from -- QuickCheck.@@ -66,9 +66,12 @@     fieldGen = arbitraryFieldValue ftd  arbitraryFieldValue :: FieldTypeDescriptor value -> Gen value-arbitraryFieldValue ftd = case ftd of-    MessageField -> arbitraryMessage-    GroupField -> arbitraryMessage+arbitraryFieldValue = \case+    MessageField _ -> scale (`div` 2) arbitraryMessage+    ScalarField f -> arbitraryScalarValue f++arbitraryScalarValue :: ScalarField value -> Gen value+arbitraryScalarValue = \case     -- For enum fields, all we know is that the value is an instance of     -- MessageEnum, meaning we can only use fromEnum, toEnum, or maybeToEnum. So     -- we must rely on the instance of Arbitrary for Int and filter out only the@@ -96,9 +99,7 @@ -- | Shrink each field individually and append all shrinks together into -- a single list. shrinkMessage :: Message a => a -> [a]-shrinkMessage msg = concatMap (`shrinkField` msg) fields-  where-    fields = M.elems (fieldsByTag descriptor)+shrinkMessage msg = concatMap (`shrinkField` msg) allFields  shrinkMaybe :: (a -> [a]) -> Maybe a -> [Maybe a] shrinkMaybe f (Just v) = Nothing : (Just <$> f v)@@ -119,9 +120,12 @@     fieldShrinker = shrinkFieldValue ftd  shrinkFieldValue :: FieldTypeDescriptor value -> value -> [value]-shrinkFieldValue ftd = case ftd of-    MessageField -> shrinkMessage-    GroupField -> map unArbitraryMessage . shrink . ArbitraryMessage+shrinkFieldValue = \case+    MessageField _ -> shrinkMessage+    ScalarField f -> shrinkScalarValue f++shrinkScalarValue :: ScalarField value -> value -> [value]+shrinkScalarValue = \case     -- Shrink to the 0-equivalent Enum value if it's both a valid Enum value     -- and the value isn't already 0.     EnumField -> case maybeToEnum 0 of