packages feed

hspec-contrib (empty) → 0.1.0

raw patch · 8 files changed

+198/−0 lines, 8 filesdep +HUnitdep +QuickCheckdep +basesetup-changed

Dependencies added: HUnit, QuickCheck, base, hspec, hspec-contrib, hspec-core

Files

+ LICENSE view
@@ -0,0 +1,21 @@+Copyright (c) 2011-2014 Simon Hengel <sol@typeful.net>+Copyright (c) 2011-2012 Trystan Spangler <trystan.s@comcast.net>+Copyright (c) 2011-2011 Greg Weber <greg@gregweber.info>++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.
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ hspec-contrib.cabal view
@@ -0,0 +1,58 @@+name:             hspec-contrib+version:          0.1.0+license:          MIT+license-file:     LICENSE+copyright:        (c) 2011-2014 Simon Hengel,+                  (c) 2014 Junji Hashimoto++maintainer:       Simon Hengel <sol@typeful.net>+build-type:       Simple+cabal-version:    >= 1.10+category:         Testing+stability:        experimental+bug-reports:      https://github.com/hspec/hspec/issues+homepage:         http://hspec.github.io/+synopsis:         Contributed functionality for Hspec+description:      Contributed functionality for Hspec++source-repository head+  type: git+  location: https://github.com/hspec/hspec+  subdir: hspec-contrib++library+  ghc-options:+      -Wall+  hs-source-dirs:+      src+  build-depends:+      base == 4.*+    , hspec-core+    , HUnit+  exposed-modules:+      Test.Hspec.Contrib.Retry+      Test.Hspec.Contrib.HUnit+  other-modules:+  default-language: Haskell2010++test-suite spec+  type:+      exitcode-stdio-1.0+  hs-source-dirs:+      test+  main-is:+      Spec.hs+  other-modules:+      Test.Hspec.Contrib.RetrySpec+      Test.Hspec.Contrib.HUnitSpec+  ghc-options:+      -Wall+  build-depends:+      base == 4.*+    , hspec-core+    , HUnit++    , hspec-contrib+    , hspec+    , QuickCheck+  default-language: Haskell2010
+ src/Test/Hspec/Contrib/HUnit.hs view
@@ -0,0 +1,23 @@+module Test.Hspec.Contrib.HUnit (+-- * Interoperability with HUnit+  fromHUnitTest+) where++import           Test.Hspec.Core.Spec+import           Test.HUnit (Test (..))++-- |+-- Convert a HUnit test suite to a spec.  This can be used to run existing+-- HUnit tests with Hspec.+fromHUnitTest :: Test -> Spec+fromHUnitTest t = case t of+  TestList xs -> mapM_ go xs+  x -> go x+  where+    go :: Test -> Spec+    go t_ = case t_ of+      TestLabel s (TestCase e) -> it s e+      TestLabel s (TestList xs) -> describe s (mapM_ go xs)+      TestLabel s x -> describe s (go x)+      TestList xs -> describe "<unlabeled>" (mapM_ go xs)+      TestCase e -> it "<unlabeled>" e
+ src/Test/Hspec/Contrib/Retry.hs view
@@ -0,0 +1,24 @@+{-# LANGUAGE TypeFamilies #-}+module Test.Hspec.Contrib.Retry (retryWith) where++import           Test.Hspec.Core.Spec++data Retry a = Retry Int a++instance Example a => Example (Retry a) where+  type Arg (Retry a) = Arg a+  evaluateExample (Retry n example) a b c = do+    v <- evaluateExample example a b c+    case v of+      Success -> return v+      _ | n > 1 -> evaluateExample (Retry (pred n) example) a b c+      _ -> return v++-- | Retry evaluating example that may be failed until success.+retryWith :: Int+          -- ^ number of retries, when this number is 1, just evaluate example and finish.+          -> a+          -- ^ retried example+          -> Retry a+          -- ^ Retry is instance of Example.+retryWith = Retry
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+ test/Test/Hspec/Contrib/HUnitSpec.hs view
@@ -0,0 +1,46 @@+{-# OPTIONS_GHC -fno-warn-orphans #-}+module Test.Hspec.Contrib.HUnitSpec (main, spec) where++import           Test.Hspec+import           Test.QuickCheck+import           Control.Applicative++import           Test.Hspec.Core.Spec+import           Test.Hspec.Contrib.HUnit+import           Test.HUnit++main :: IO ()+main = hspec spec++shouldYield :: Test -> [Tree (ActionWith ()) String] -> Expectation+a `shouldYield` b = map (Blind . fmap itemRequirement) <$> runSpecM (fromHUnitTest a) `shouldReturn` map Blind b++instance Eq a => Eq (Tree c a) where+  x == y = case (x, y) of+    (Node s1 xs, Node s2 ys) -> s1 == s2 && xs == ys+    (Leaf x1, Leaf x2) -> x1 == x2+    _ -> False++spec :: Spec+spec = do+  describe "fromHUnitTest" $ do+    let e = TestCase $ pure ()++    it "works for a TestCase" $ do+      e `shouldYield` [Leaf "<unlabeled>"]++    it "works for a labeled TestCase" $ do+      TestLabel "foo" e+        `shouldYield` [Leaf "foo"]++    it "works for a TestCase with nested labels" $ do+      (TestLabel "foo" . TestLabel "bar") e+        `shouldYield` [Node "foo" [Leaf "bar"]]++    it "works for a flat TestList" $ do+      TestList [e, e, e]+        `shouldYield` [Leaf "<unlabeled>", Leaf "<unlabeled>", Leaf "<unlabeled>"]++    it "works for a nested TestList" $ do+      (TestLabel "foo" . TestLabel "bar" . TestList) [TestLabel "one" e, TestLabel "two" e, TestLabel "three" e]+        `shouldYield` [Node "foo" [Node "bar" [Leaf "one", Leaf "two", Leaf "three"]]]
+ test/Test/Hspec/Contrib/RetrySpec.hs view
@@ -0,0 +1,22 @@+module Test.Hspec.Contrib.RetrySpec (main, spec) where++import           Data.IORef++import           Test.Hspec+import           Test.Hspec.Contrib.Retry++main :: IO ()+main = hspec spec++spec :: Spec+spec = do+  describe "retry test" $ do+    ref <- runIO $ newIORef (0::Int)+    it "retry 11 times, then check the value" $ do+      let incr :: IO Int+          incr = do+            val <- readIORef ref+            writeIORef ref (val+1)+            return val+      retryWith 11 $ do+        incr `shouldReturn` (10::Int)