Unit tests for Data.Text.ICU.Extras.

master
vi 2014-08-11 00:12:36 +08:00
parent 2c0a18a6f0
commit d86284f09e
4 changed files with 73 additions and 2 deletions

View File

@ -50,3 +50,32 @@ library
Haskell2010
ghc-options:
-Wall
test-suite spec
type:
exitcode-stdio-1.0
build-depends:
base >= 4.7 && < 4.8,
attoparsec >= 0.12 && < 0.13,
http-client >= 0.3 && < 0.4,
text >= 1.1 && < 1.2,
pipes >= 4.1 && < 4.2,
errors >= 1.4 && < 1.5,
lens >= 4.3 && < 4.4,
functor-infix >= 0.0 && < 0.1,
string-conversions >= 0.3 && < 0.4,
text-icu >= 0.6 && < 0.7,
taggy-lens >= 0.1 && < 0.2,
hspec >= 1.10 && < 1.11,
https-everywhere-rules-raw
main-is:
Spec.hs
hs-source-dirs:
src,
test
default-language:
Haskell2010
cpp-options:
-DTEST
ghc-options:
-Wall

View File

@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
@ -5,7 +6,11 @@
module Data.Text.ICU.Extras (
match,
findAndReplace
findAndReplace,
#ifdef TEST
Segment(..),
parseReplacement
#endif
) where
import Control.Applicative ((<$>), (*>), (<|>))
@ -29,7 +34,8 @@ findAndReplace pattern replacement = do
type Replacement = [Segment]
data Segment = Reference Int | Literal Text deriving (Show)
data Segment = Reference Int | Literal Text
deriving (Show, Eq)
parseReference :: Parser Segment
parseReference = char '$' *> digit <&> Reference . read . return

View File

@ -0,0 +1,35 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.Text.ICU.ExtrasSpec (spec) where
import Control.Applicative ((<$>))
import Control.Lens ((&))
import Data.Text (Text)
import Data.Text.ICU.Extras (match, findAndReplace, Segment(..), parseReplacement)
import Test.Hspec (Spec, describe, it, shouldBe)
spec :: Spec
spec = do
describe "match" $ do
it "Should be Nothing if provided an invalid regular expression." $ do
match "(" `shouldBe` Nothing
it "Should yield a match function if provided a regular expression." $ do
("xa" &) <$> match "x" `shouldBe` Just True
("xa" &) <$> match "y" `shouldBe` Just False
describe "parseReplacement" $ do
it "Should decompose a replacement string into a sequence [Segment]." $ do
parseReplacement "foo$1bar$4$1" `shouldBe` Just [Literal "foo", Reference 1, Literal "bar", Reference 4, Reference 1]
it "Should correctly parse successive '$'s" $ do
parseReplacement "$$1" `shouldBe` Just [Literal "$", Reference 1]
parseReplacement "$$" `shouldBe` Just [Literal "$", Literal "$"]
describe "findAndReplace" $ do
it "Should find and replace based upon a regular expression and pattern." $ do
("barbaz" &) <$> findAndReplace "(.*)" "$1qux" `shouldBe` Just (Just "barbazqux")
instance Show (Text -> Bool) where
show _ = "Text -> Bool"
instance Eq (Text -> Bool) where
_ == _ = False

1
test/Spec.hs Normal file
View File

@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}