From d86284f09ef62eed05f5feeb651454f1636fbc91 Mon Sep 17 00:00:00 2001 From: vi Date: Mon, 11 Aug 2014 00:12:36 +0800 Subject: [PATCH] Unit tests for Data.Text.ICU.Extras. --- https-everywhere-rules.cabal | 29 ++++++++++++++++++++++++++ src/Data/Text/ICU/Extras.hs | 10 +++++++-- test/Data/Text/ICU/ExtrasSpec.hs | 35 ++++++++++++++++++++++++++++++++ test/Spec.hs | 1 + 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 test/Data/Text/ICU/ExtrasSpec.hs create mode 100644 test/Spec.hs diff --git a/https-everywhere-rules.cabal b/https-everywhere-rules.cabal index 4e974c9..e8155ab 100644 --- a/https-everywhere-rules.cabal +++ b/https-everywhere-rules.cabal @@ -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 diff --git a/src/Data/Text/ICU/Extras.hs b/src/Data/Text/ICU/Extras.hs index fa281a7..5e9b99b 100644 --- a/src/Data/Text/ICU/Extras.hs +++ b/src/Data/Text/ICU/Extras.hs @@ -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 diff --git a/test/Data/Text/ICU/ExtrasSpec.hs b/test/Data/Text/ICU/ExtrasSpec.hs new file mode 100644 index 0000000..a6184d2 --- /dev/null +++ b/test/Data/Text/ICU/ExtrasSpec.hs @@ -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 diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-}