packages feed

prop-unit (empty) → 0.1.0

raw patch · 6 files changed

+246/−0 lines, 6 filesdep +basedep +hedgehogdep +prop-unitsetup-changed

Dependencies added: base, hedgehog, prop-unit, tasty, tasty-hedgehog, tasty-hunit

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2022++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,17 @@+# prop-unit++[![CircleCI](https://circleci.com/gh/ejconlon/prop-unit/tree/master.svg?style=svg)](https://circleci.com/gh/ejconlon/prop-unit/tree/master)++Conveniences for using Hedgehog as a unit test runner++It's a pain to share assertions between Hedgehog and HUnit. My solution is to basically turn unit tests into really simple property-based tests!++Useful environment variables:++    # Turn on "debug mode" (default 0)+    # Changes buffering and threading for easier debugging+    PROP_UNIT_DEBUG=1++    # Increase test examples (default 100)+    # Only has effect on true property-based tests+    PROP_UNIT_LIMIT=1000
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ prop-unit.cabal view
@@ -0,0 +1,109 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name:           prop-unit+version:        0.1.0+synopsis:       Conveniences for using Hedgehog as a unit test runner+description:    Please see the README on GitHub at <https://github.com/ejconlon/prop-unit#readme>+category:       Testing+homepage:       https://github.com/ejconlon/prop-unit#readme+bug-reports:    https://github.com/ejconlon/prop-unit/issues+author:         Eric Conlon+maintainer:     ejconlon@gmail.com+copyright:      (c) 2022 Eric Conlon+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md++source-repository head+  type: git+  location: https://github.com/ejconlon/prop-unit++library+  exposed-modules:+      PropUnit+  other-modules:+      Paths_prop_unit+  hs-source-dirs:+      src+  default-extensions:+      BangPatterns+      ConstraintKinds+      DeriveAnyClass+      DeriveFunctor+      DeriveFoldable+      DeriveGeneric+      DeriveTraversable+      DerivingStrategies+      DerivingVia+      FlexibleContexts+      FlexibleInstances+      FunctionalDependencies+      GADTs+      GeneralizedNewtypeDeriving+      LambdaCase+      KindSignatures+      MultiParamTypeClasses+      Rank2Types+      ScopedTypeVariables+      StandaloneDeriving+      TemplateHaskell+      TupleSections+      TypeApplications+      TypeOperators+      TypeFamilies+  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints -fno-warn-unused-top-binds -fwrite-ide-info -hiedir=.hie+  build-depends:+      base >=4.12 && <5+    , hedgehog ==1.0.*+    , tasty ==1.4.*+    , tasty-hedgehog ==1.1.*+  default-language: Haskell2010++test-suite prop-unit-test+  type: exitcode-stdio-1.0+  main-is: Main.hs+  other-modules:+      Paths_prop_unit+  hs-source-dirs:+      test+  default-extensions:+      BangPatterns+      ConstraintKinds+      DeriveAnyClass+      DeriveFunctor+      DeriveFoldable+      DeriveGeneric+      DeriveTraversable+      DerivingStrategies+      DerivingVia+      FlexibleContexts+      FlexibleInstances+      FunctionalDependencies+      GADTs+      GeneralizedNewtypeDeriving+      LambdaCase+      KindSignatures+      MultiParamTypeClasses+      Rank2Types+      ScopedTypeVariables+      StandaloneDeriving+      TemplateHaskell+      TupleSections+      TypeApplications+      TypeOperators+      TypeFamilies+  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints -fno-warn-unused-top-binds -fwrite-ide-info -hiedir=.hie -threaded -rtsopts -with-rtsopts=-N+  build-depends:+      base >=4.12 && <5+    , hedgehog ==1.0.*+    , prop-unit+    , tasty+    , tasty-hedgehog ==1.1.*+    , tasty-hunit+  default-language: Haskell2010
+ src/PropUnit.hs view
@@ -0,0 +1,65 @@+module PropUnit+  ( DependencyType (..)+  , Gen+  , MonadTest+  , Property+  , PropertyT+  , Range+  , TestLimit+  , TestName+  , TestTree+  , (===)+  , (/==)+  , after+  , assert+  , forAll+  , testProp+  , testUnit+  , defaultTestLimit+  , setupTests+  , testGroup+  , testMain+  , withResource+  ) where++import Control.Monad (when)+import Hedgehog (DiscardLimit, Gen, MonadTest, Property, PropertyT, Range, ShrinkLimit, ShrinkRetries, TestLimit,+                 assert, forAll, property, withDiscards, withRetries, withShrinks, withTests, (/==), (===))+import System.Environment (lookupEnv, setEnv)+import System.IO (BufferMode (..), hSetBuffering, stderr, stdout)+import Test.Tasty (DependencyType (..), TestName, TestTree, after, defaultMain, testGroup, withResource)+import Test.Tasty.Hedgehog (testProperty)++unitProperty :: PropertyT IO () -> Property+unitProperty =+  withTests (1 :: TestLimit) .+  withDiscards (1 :: DiscardLimit) .+  withShrinks (0 :: ShrinkLimit) .+  withRetries (0 :: ShrinkRetries) .+  property++testUnit :: TestName -> PropertyT IO () -> TestTree+testUnit name = testProperty name . unitProperty++testProp :: TestName -> TestLimit -> PropertyT IO () -> TestTree+testProp name lim = testProperty name . withTests lim . property++-- 100 is Hedgehog's defaultMinTests+defaultTestLimit :: TestLimit+defaultTestLimit = 100++setupTests :: IO TestLimit+setupTests = do+  mayDebugStr <- lookupEnv "PROP_UNIT_DEBUG"+  let debug = Just "1" == mayDebugStr+  when debug $ do+    setEnv "TASTY_NUM_THREADS" "1"+    hSetBuffering stdout NoBuffering+    hSetBuffering stderr NoBuffering+  mayLimStr <- lookupEnv "PROP_UNIT_LIMIT"+  pure (maybe defaultTestLimit (fromInteger . read) mayLimStr)++testMain :: (TestLimit -> TestTree) -> IO ()+testMain f = do+  lim <- setupTests+  defaultMain (f lim)
+ test/Main.hs view
@@ -0,0 +1,23 @@+module Main (main) where++import qualified Hedgehog.Gen as Gen+import qualified Hedgehog.Range as Range+import PropUnit (TestLimit, TestTree, forAll, testGroup, testMain, testProp, testUnit, (===))++testAsUnit :: TestTree+testAsUnit = testUnit "as unit" $ do+    let actual = (1 + 1) :: Int+        expected = 2 :: Int+    actual === expected++testAsProp :: TestLimit -> TestTree+testAsProp lim = testProp "as gen" lim $ do+  x <- forAll (Gen.int (Range.constant 1 10))+  abs x * abs x === x * x++main :: IO ()+main = testMain $ \lim ->+  testGroup "PropUnit"+    [ testAsUnit+    , testAsProp lim+    ]