packages feed

hspec-parsec (empty) → 0

raw patch · 6 files changed

+200/−0 lines, 6 filesdep +basedep +hspecdep +hspec-expectationssetup-changed

Dependencies added: base, hspec, hspec-expectations, hspec-parsec, parsec

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Simon Jakobi (c) 2019++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 Simon Jakobi 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,69 @@+# hspec-parsec++[![License BSD3](https://img.shields.io/badge/license-BSD3-brightgreen.svg)](http://opensource.org/licenses/BSD-3-Clause)+[![Hackage](https://img.shields.io/hackage/v/hspec-parsec.svg?style=flat)](https://hackage.haskell.org/package/hspec-parsec)++This package provides handy [Hspec](http://hspec.github.io/) expectations for testing+[Parsec](http://hackage.haskell.org/package/parsec) parsers.++## Usage++Add `hspec-parsec` to your test suite's dependencies:++```+  build-depends:       base+                     , hspec+                     , hspec-parsec+                     , parsec+```++… write some tests:++```haskell+import Test.Hspec+import Test.Hspec.Parsec+import Text.Parsec+import Text.Parsec.String (Parser)++main :: IO ()+main = hspec $ do+  describe "yamlBool" $ do+    let yamlBool' = parse yamlBool ""++    it "correctly parses \"True\"" $ do+      yamlBool' "True" `shouldParse` True++    it "doesn't parse \"yes\"" $ do+      yamlBool' `shouldFailOn` "yes"++yamlBool :: Parser Bool+yamlBool = +        (choice (map string false) *> pure False)+    <|> (choice (map string true) *> pure True)+  where+    false = ["false", "False", "FALSE"]+    true = ["true", "True", "TRUE"]+```++… and run them:++```shell+$ stack test+hspec-parsec> test (suite: spec)+++yamlBool+  correctly parses "True"+  doesn't parse "yes"++Finished in 0.0001 seconds+2 examples, 0 failures++hspec-parsec> Test suite spec passed+```++## Thanks++… to [Mark Karpov](https://github.com/mrkkrp), whose package+[`hspec-megaparsec`](https://hackage.haskell.org/package/hspec-megaparsec)+much inspired `hspec-parsec`!
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hspec-parsec.cabal view
@@ -0,0 +1,36 @@+name:                hspec-parsec+version:             0+synopsis:            Hspec expectations for testing Parsec parsers+homepage:            https://github.com/sjakobi/hspec-parsec#readme+bug-reports:         https://github.com/sjakobi/hspec-parsec/issues+license:             BSD3+license-file:        LICENSE+author:              Simon Jakobi, Mark Karpov+maintainer:          simon.jakobi@gmail.com+copyright:           Copyright (c) 2019 Simon Jakobi+category:            Testing, Parsing+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Test.Hspec.Parsec+  build-depends:       base >= 4.7 && < 5+                     , hspec-expectations < 0.9+                     , parsec < 3.2+  default-language:    Haskell2010++test-suite spec+  hs-source-dirs:      tests+  main-is:             Main.hs+  build-depends:       base+                     , hspec+                     , hspec-parsec+                     , parsec+  type:                exitcode-stdio-1.0+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/sjakobi/hspec-parsec
+ src/Test/Hspec/Parsec.hs view
@@ -0,0 +1,40 @@+-- | Utility functions for testing Parsec parsers with Hspec.+module Test.Hspec.Parsec+    ( shouldParse+    , shouldFailOn+    ) where++import GHC.Stack (HasCallStack)+import Text.Parsec+import Test.Hspec.Expectations++-- | Create an expectation by saying what the result should be.+--+-- @+-- 'parse' 'letter' "" "x" ``shouldParse`` \'x\'+-- @+shouldParse+  :: (HasCallStack, Show a, Eq a)+  => Either ParseError a -- ^ Result of parsing as returned by function like 'parse'+  -> a                   -- ^ Desired result+  -> Expectation+r `shouldParse` v = case r of+  Left e -> expectationFailure $ "expected: " ++ show v +++    "\nbut parsing failed with error:\n" ++ show e+  Right x -> x `shouldBe` v++-- | Check that a parser fails on a given input.+--+-- @+-- 'parse' ('char' \'x\') "" ``shouldFailOn`` "a"+-- @+shouldFailOn+  :: (HasCallStack, Show a)+  => (s -> Either ParseError a)+     -- ^ Parser that takes stream and produces result or error message+  -> s                 -- ^ Input that the parser should fail on+  -> Expectation+p `shouldFailOn` s = case p s of+  Left _ -> return ()+  Right v -> expectationFailure $+    "the parser is expected to fail, but it parsed: " ++ show v
+ tests/Main.hs view
@@ -0,0 +1,23 @@+import Test.Hspec+import Test.Hspec.Parsec+import Text.Parsec+import Text.Parsec.String (Parser)++main :: IO ()+main = hspec $ do+  describe "yamlBool" $ do+    let yamlBool' = parse yamlBool ""++    it "correctly parses \"True\"" $ do+      yamlBool' "True" `shouldParse` True++    it "doesn't parse \"yes\"" $ do+      yamlBool' `shouldFailOn` "yes"++yamlBool :: Parser Bool+yamlBool = +        (choice (map string false) *> pure False)+    <|> (choice (map string true) *> pure True)+  where+    false = ["false", "False", "FALSE"]+    true = ["true", "True", "TRUE"]