hspec-expectations-lens 0.1.0.0 → 0.2.0.0
raw patch · 5 files changed
+147/−74 lines, 5 filesdep ~hspecdep ~hspec-expectations
Dependency ranges changed: hspec, hspec-expectations
Files
- CHANGELOG.md +11/−1
- README.md +1/−0
- hspec-expectations-lens.cabal +7/−4
- src/Test/Hspec/Expectations/Lens.hs +100/−69
- src/Test/Hspec/Lens.hs +28/−0
CHANGELOG.md view
@@ -1,4 +1,14 @@+0.2.0.0+=======++ * Add a `shouldThrow` expectation, a lensy variant of `shouldThrow`++ * Add a `Test.Hspec.Lens` module, re-exporting `Test.Hspec` with+`Test.Hspec.Expectations.Lens` expectations++ * More documentation with fake type signatures+ 0.1.0.0 ======= -`shouldHave`, `shouldNotHave`, `shouldView`, `shouldPreview`, and `shouldList`+ * `shouldHave`, `shouldNotHave`, `shouldView`, `shouldPreview`, and `shouldList`
README.md view
@@ -1,5 +1,6 @@ hspec-expectations-lens ======+[](http://hackage.haskell.org/package/hspec-expectations-lens) [](http://travis-ci.org/supki/hspec-expectations-lens) Hspec expectations for the lens stuff
hspec-expectations-lens.cabal view
@@ -1,10 +1,10 @@ name: hspec-expectations-lens-version: 0.1.0.0+version: 0.2.0.0 synopsis: Hspec expectations for the lens stuff description: Package adds hspec expectations (@\`shouldX\`@ things) that work nicely with the "lens" library-homepage: https://github.com/supki/hspec-expectations-lens+homepage: http://supki.github.io/hspec-expectations-lens/ license: BSD3 license-file: LICENSE author: Matvey Aksenov@@ -22,7 +22,7 @@ source-repository this type: git- tag: 0.1.0.0+ tag: 0.2.0.0 location: https://github.com/supki/hspec-expectations-lens library@@ -30,10 +30,13 @@ hs-source-dirs: src build-depends: base == 4.*- , hspec-expectations+ , hspec-expectations >= 0.5+ , hspec >= 1.8+ , lens >= 3.9 , HUnit exposed-modules: Test.Hspec.Expectations.Lens+ Test.Hspec.Lens test-suite spec type: exitcode-stdio-1.0
src/Test/Hspec/Expectations/Lens.hs view
@@ -1,104 +1,135 @@-{-# LANGUAGE Rank2Types #-} -- | Hspec expectations for the lens stuff module Test.Hspec.Expectations.Lens ( -- * Expectations- shouldHave, shouldNotHave+ shouldHave+ , shouldNotHave , shouldView , shouldPreview , shouldList+ , shouldThrow , through ) where -import Control.Applicative (Const(..))-import Data.Monoid (Any(..), First(..), Endo(..))+import Control.Lens+import Control.Exception (SomeException)+import Control.Exception.Lens+import Data.Monoid (Any(..), All(..), First(..), Endo(..)) import Test.Hspec.Expectations (Expectation)-import Test.HUnit (assertBool)--{-# ANN module "HLint: Use camelCase" #-}+import Test.HUnit (assertBool, assertFailure)+import Text.Printf (printf) -infixl 1 `shouldHave`, `shouldNotHave`, `shouldView`, `shouldPreview`, `shouldList`, `through`+infixl 1 `shouldHave`, `shouldNotHave`, `shouldView`, `shouldPreview`, `shouldList`, `shouldThrow`, `through` -- | @x \`shouldHave\` l@ sets the expectation that 'Fold' @l@ has -- non-zero number of targets in @x@-shouldHave- :: Show s- => s- -> ((a -> Const Any a) -> s -> Const Any s)- -> Expectation-x `shouldHave` f = assertBool errorMsg (has f x)- where- errorMsg = unwords ["Supplied Fold has zero targets for", show x]+--+-- @+-- shouldHave :: 'Show' s => s -> 'Getter' s a -> 'Expectation'+-- shouldHave :: 'Show' s => s -> 'Fold' s a -> 'Expectation'+-- shouldHave :: 'Show' s => s -> 'Iso'' s a -> 'Expectation'+-- shouldHave :: 'Show' s => s -> 'Lens'' s a -> 'Expectation'+-- shouldHave :: 'Show' s => s -> 'Traversal'' s a -> 'Expectation'+-- shouldHave :: 'Show' s => s -> 'Prism'' s a -> 'Expectation'+-- @+shouldHave :: Show s => s -> Getting Any s a -> Expectation+x `shouldHave` f = assertBool msg (has f x)+ where+ msg = printf "Supplied Fold has zero targets for %s" (show x) -- | @x \`shouldNotHave\` l@ sets the expectation that 'Fold' @l@ -- has zero targets in @x@-shouldNotHave- :: Show s- => s- -> ((a -> Const Any a) -> s -> Const Any s)- -> Expectation-x `shouldNotHave` f = assertBool errorMsg (hasn't f x)- where- errorMsg = unwords ["Supplied Fold has targets for", show x]+--+-- @+-- shouldNotHave :: 'Show' s => s -> 'Getter' s a -> 'Expectation'+-- shouldNotHave :: 'Show' s => s -> 'Fold' s a -> 'Expectation'+-- shouldNotHave :: 'Show' s => s -> 'Iso'' s a -> 'Expectation'+-- shouldNotHave :: 'Show' s => s -> 'Lens'' s a -> 'Expectation'+-- shouldNotHave :: 'Show' s => s -> 'Traversal'' s a -> 'Expectation'+-- shouldNotHave :: 'Show' s => s -> 'Prism'' s a -> 'Expectation'+-- @+shouldNotHave :: Show s => s -> Getting All s a -> Expectation+x `shouldNotHave` f = assertBool msg (hasn't f x)+ where+ msg = printf "Supplied Fold has non-zero targets for %s" (show x) -- | @x \`shouldView\` y \`through\` l@ sets the expectation that -- you can see @y@ in @x@ though a 'Getter' @l@-shouldView- :: (Show s, Show a, Eq a)- => s- -> a- -> ((a -> Const a a) -> s -> Const a s)- -> Expectation-(x `shouldView` y) l = assertBool errorMsg (view l x == y)- where- errorMsg = unwords ["Can't view", show y, "from", show x, "through supplied Getter"]+--+-- @+-- shouldView :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Getter' s a -> 'Expectation'+-- shouldView :: ('Data.Monoid.Monoid' m, 'Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Fold' s m -> 'Expectation'+-- shouldView :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Iso'' s a -> 'Expectation'+-- shouldView :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Lens'' s a -> 'Expectation'+-- shouldView :: ('Data.Monoid.Monoid' m, 'Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Traversal'' s m -> 'Expectation'+-- shouldView :: ('Data.Monoid.Monoid' m, 'Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Prism'' s m -> 'Expectation'+-- @+shouldView :: (Show s, Show a, Eq a) => s -> a -> Getting a s a -> Expectation+(x `shouldView` y) l = assertBool msg (view l x == y)+ where+ msg = printf "Can't view %s from %s through supplied Getter" (show y) (show x) -- | @x \`shouldPreview\` y \`through\` l@ sets the expectation that -- you can list @y@ in @x@ first though a 'Fold' @l@-shouldPreview- :: (Show s, Show a, Eq a)- => s- -> a- -> ((a -> Const (First a) a) -> s -> Const (First a) s)- -> Expectation-(x `shouldPreview` y) l = assertBool errorMsg (preview l x == Just y)- where- errorMsg = unwords ["Can't preview", show y, "from", show x, "through supplied Fold"]+--+-- @+-- shouldPreview :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Getter' s a -> 'Expectation'+-- shouldPreview :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Fold' s a -> 'Expectation'+-- shouldPreview :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Lens'' s a -> 'Expectation'+-- shouldPreview :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Iso'' s a -> 'Expectation'+-- shouldPreview :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Traversal'' s a -> 'Expectation'+-- shouldPreview :: ('Show' s, 'Show' a, 'Eq' a) => s -> a -> 'Prism'' s a -> 'Expectation'+-- @+shouldPreview :: (Show s, Show a, Eq a) => s -> a -> Getting (First a) s a -> Expectation+(x `shouldPreview` y) l = assertBool msg (preview l x == Just y)+ where+ msg = printf "Can't preview %s from %s through supplied Fold" (show y) (show x) -- | @x \`shouldList\` ys \`through\` l@ sets the expectation that -- you can list @ys@ in @x@ though a 'Fold' @l@-shouldList- :: (Show s, Show a, Eq a)- => s- -> [a]- -> ((a -> Const (Endo [a]) a) -> s -> Const (Endo [a]) s)- -> Expectation-(x `shouldList` y) l = assertBool errorMsg (toListOf l x == y)- where- errorMsg = unwords ["Can't list", show y, "from", show x, "through supplied Fold"]+--+-- @+-- shouldList :: ('Show' s, 'Show' a, 'Eq' a) => s -> [a] -> 'Getter' s a -> 'Expectation'+-- shouldList :: ('Show' s, 'Show' a, 'Eq' a) => s -> [a] -> 'Fold' s a -> 'Expectation'+-- shouldList :: ('Show' s, 'Show' a, 'Eq' a) => s -> [a] -> 'Lens'' s a -> 'Expectation'+-- shouldList :: ('Show' s, 'Show' a, 'Eq' a) => s -> [a] -> 'Iso'' s a -> 'Expectation'+-- shouldList :: ('Show' s, 'Show' a, 'Eq' a) => s -> [a] -> 'Traversal'' s a -> 'Expectation'+-- shouldList :: ('Show' s, 'Show' a, 'Eq' a) => s -> [a] -> 'Prism'' s a -> 'Expectation'+-- @+shouldList :: (Show s, Show a, Eq a) => s -> [a] -> Getting (Endo [a]) s a -> Expectation+(x `shouldList` y) l = assertBool msg (toListOf l x == y)+ where+ msg = printf "Can't list %s from %s through supplied Fold" (show y) (show x) +-- | @x \`shouldthrow\` l@ sets the expectation that+-- @x@ throws an exception catchable with a 'Fold' @l@+--+-- /Note:/ name conflicts with 'Test.Hspec.Expectations.shouldThrow'+--+-- @+-- shouldThrow :: 'IO' a -> -> 'Getter' s b -> 'Expectation'+-- shouldThrow :: 'IO' a -> -> 'Fold' s b -> 'Expectation'+-- shouldThrow :: 'IO' a -> -> 'Lens'' s b -> 'Expectation'+-- shouldThrow :: 'IO' a -> -> 'Iso'' s b -> 'Expectation'+-- shouldThrow :: 'IO' a -> -> 'Traversal'' s b -> 'Expectation'+-- shouldThrow :: 'IO' a -> -> 'Prism'' s b -> 'Expectation'+-- @+shouldThrow :: IO a -> Getting (First b) SomeException b -> Expectation+x `shouldThrow` l = do+ r <- trying l x+ case r of+ Left _ -> return ()+ Right _ -> assertFailure "Couldn't catch any exceptions with the supplied Fold"+ -- | A helper to fight parentheses -- -- @ -- through ≡ id -- @+--+-- @+-- through :: 'Int' -> 'Int'+-- through :: 'Char' -> 'Char'+-- @ through :: a -> a through = id--has :: ((a -> Const Any b) -> s -> Const Any t) -> s -> Bool-has l = getAny . foldMapOf l (\_ -> Any True)--hasn't :: ((a -> Const Any b) -> s -> Const Any t) -> s -> Bool-hasn't l = not . has l--view :: ((a -> Const a a) -> s -> Const a s) -> s -> a-view l = foldMapOf l id--preview :: ((a -> Const (First a) a) -> s -> Const (First a) s) -> s -> Maybe a-preview l s = getFirst (foldMapOf l (First . Just) s)--toListOf :: ((a -> Const (Endo [a]) a) -> s -> Const (Endo [a]) s) -> s -> [a]-toListOf l s = appEndo (foldMapOf l (\x -> Endo (x :)) s) []--foldMapOf :: ((a -> Const m b) -> s -> Const n t) -> (a -> m) -> s -> n-foldMapOf l f = getConst . l (Const . f)
+ src/Test/Hspec/Lens.hs view
@@ -0,0 +1,28 @@+-- | Module is designed to be conveniently imported instead of "Test.Hspec"+--+-- It reexports "Test.Hspec" without expectations (except for `shouldBe`)+-- and "Test.Hspec.Expectations.Lens" expectations+module Test.Hspec.Lens+ ( -- * Types+ Spec+ , Example+ -- * Setting expectations+ , shouldBe+ , module Test.Hspec.Expectations.Lens+ -- * Defining a spec+ , describe+ , context+ , it+ , example+ , pending+ , pendingWith+ , before+ , after+ , around+ , parallel+ -- * Running a spec+ , hspec+ ) where++import Test.Hspec+import Test.Hspec.Expectations.Lens