packages feed

substring-parser-0.3.0.0: test/Spec.hs

import           Test.Hspec
import           Test.Hspec.QuickCheck

import           Text.Parser.Substring

import           Control.Applicative ((*>))
import           Data.Attoparsec.Text
import           Data.Monoid ((<>))
import qualified Data.Set as Set
import           Data.Set ((\\))
import qualified Data.Text as Text


main :: IO ()
main = hspec $ do
  describe "replaceWithParser" $ do
    it "replace first matching text by parser" $
      replaceOnceWithParser (string "abc" *> pure "def") "--abc-abc-"
        `shouldBe` "--def-abc-"

    it "returns the source text if parser doesn't match" $ do
      let src = "--abc-abc-"
      replaceOnceWithParser mempty src `shouldBe` src

  describe "takeMatch" $
    prop "take any string before the given parser and parse the data with the parser." $
      \(cs1, i, cs2) -> do
        let s1 = Text.pack $ Set.toList $ cs1 \\ Set.fromList ['0'..'9']
            s2 = Text.pack $ Set.toList $ cs2 \\ Set.fromList ['0'..'9']
            ds = show (abs i :: Integer)
            input = s1 <> Text.pack ds <> s2
        case feed (parse (takeMatch $ many1 digit) input) "" of
             Done left result -> do
               result `shouldBe` (s1, ds)
               left `shouldBe` s2
             other ->
               fail $ "Not done: " <> show other