git.iko.soy/typeable/inflections-hs
on master help sign in
Better define what a word is, tests for ‘Types.hs’
2348bb7mrkkrp
Text/Inflections/Types.hs+14−12
@@ -31,7 +31,7 @@ module Text.Inflections.Types
31where
31where
32
32
33import Control.Monad.Catch
33import Control.Monad.Catch
34−import Data.Char (isSpace)
34+import Data.Char (isAlphaNum)
35import Data.Data (Data)
35import Data.Data (Data)
36import Data.Text (Text)
36import Data.Text (Text)
37import Data.Typeable (Typeable)
37import Data.Typeable (Typeable)
@@ -43,28 +43,30 @@ import qualified Data.Text as T
43import Prelude hiding (Word)
43import Prelude hiding (Word)
44#endif
44#endif
45
45
46−-- | Create a word from given 'Text'. The input should not contain spaces or
46+-- | Create a word from given 'Text'. The input should consist of only
47+-- alpha-numeric characters (no white spaces or punctuation)
47-- 'InflectionInvalidWord' will be thrown.
48-- 'InflectionInvalidWord' will be thrown.
48--
49--
49-- /since 0.3.0.0/
50-- /since 0.3.0.0/
50
51
51mkWord :: MonadThrow m => Text -> m (Word 'Normal)
52mkWord :: MonadThrow m => Text -> m (Word 'Normal)
52mkWord txt =
53mkWord txt =
53− if T.any isSpace txt
54+ if T.all isAlphaNum txt
55+ then return (Word txt)
54− then throwM (InflectionInvalidWord txt)
56+ else throwM (InflectionInvalidWord txt)
55− else return (Word txt)
57+
56−
57−-- | Create an acronym from given 'Text'. The input should not contain
58+-- | Create an acronym from given 'Text'. The input should consist of only
58−-- spaces or 'InflectionInvalidAcronym' will be thrown. Acronym is different
59+-- alpha-numeric characters 'InflectionInvalidAcronym' will be thrown.
59−-- from normal word by that it may not be transformed by inflections.
60+-- Acronym is different from normal word by that it may not be transformed
61+-- by inflections (also see 'unSomeWord').
60--
62--
61-- /since 0.3.0.0/
63-- /since 0.3.0.0/
62
64
63mkAcronym :: MonadThrow m => Text -> m (Word 'Acronym)
65mkAcronym :: MonadThrow m => Text -> m (Word 'Acronym)
64mkAcronym txt =
66mkAcronym txt =
65− if T.any isSpace txt
67+ if T.all isAlphaNum txt
68+ then return (Word txt)
66− then throwM (InflectionInvalidAcronym txt)
69+ else throwM (InflectionInvalidAcronym txt)
67− else return (Word txt)
68
70
69-- | A 'Text' value that should be kept whole through applied inflections.
71-- | A 'Text' value that should be kept whole through applied inflections.
70
72
inflections.cabal+1
@@ -78,5 +78,6 @@ test-suite test
78 , Text.Inflections.Parse.SnakeCaseSpec
78 , Text.Inflections.Parse.SnakeCaseSpec
79 , Text.Inflections.TitleizeSpec
79 , Text.Inflections.TitleizeSpec
80 , Text.Inflections.TransliterateSpec
80 , Text.Inflections.TransliterateSpec
81+ , Text.Inflections.TypesSpec
81 , Text.Inflections.UnderscoreSpec
82 , Text.Inflections.UnderscoreSpec
82 , Text.InflectionsSpec
83 , Text.InflectionsSpec
test/Text/Inflections/HumanizeSpec.hs+3−2
@@ -19,8 +19,9 @@ spec = describe "humazine" $ do
19 humanize [employee,salary] `shouldBe` "Employee salary"
19 humanize [employee,salary] `shouldBe` "Employee salary"
20 it "turns underscores into spaces" $ do
20 it "turns underscores into spaces" $ do
21 employee <- SomeWord <$> mkWord "employee"
21 employee <- SomeWord <$> mkWord "employee"
22+ has <- SomeWord <$> mkWord "has"
22− hasSalary <- SomeWord <$> mkWord "has_salary"
23+ salary <- SomeWord <$> mkWord "salary"
23− humanize [employee, hasSalary] `shouldBe` "Employee has salary"
24+ humanize [employee, has, salary] `shouldBe` "Employee has salary"
24 it "capitalizes the first word of a sentence" $ do
25 it "capitalizes the first word of a sentence" $ do
25 underground <- SomeWord <$> mkWord "underground"
26 underground <- SomeWord <$> mkWord "underground"
26 humanize [underground] `shouldBe` "Underground"
27 humanize [underground] `shouldBe` "Underground"
test/Text/Inflections/TypesSpec.hsnew file+51
@@ -0,0 +1,51 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+
4+module Text.Inflections.TypesSpec
5+ ( spec )
6+where
7+
8+import Test.Hspec
9+import Text.Inflections
10+import qualified Data.Text as T
11+
12+#if !MIN_VERSION_base(4,8,0)
13+import Control.Applicative
14+#endif
15+
16+spec :: Spec
17+spec = do
18+ describe "mkWord" $ do
19+ context "when provided a correct Text value" $
20+ it "creates a normal word" $ do
21+ x <- mkWord "foo"
22+ unWord x `shouldBe` "foo"
23+ context "when provided an incorrect Text value" $
24+ it "throws the correct exception" $ do
25+ mkWord "foo bar" `shouldThrow` (== InflectionInvalidWord "foo bar")
26+ mkWord "foo_" `shouldThrow` (== InflectionInvalidWord "foo_")
27+ mkWord "&$?%" `shouldThrow` (== InflectionInvalidWord "&$?%")
28+ describe "mkAcronym" $ do
29+ context "when provided a correct Text value" $
30+ it "creates an acronym" $ do
31+ x <- mkAcronym "foo"
32+ unWord x `shouldBe` "foo"
33+ context "when providde an incorrect Text value" $
34+ it "throws the correct exception" $ do
35+ mkAcronym "foo bar" `shouldThrow` (== InflectionInvalidAcronym "foo bar")
36+ mkAcronym "foo_" `shouldThrow` (== InflectionInvalidAcronym "foo_")
37+ mkAcronym "&$?%" `shouldThrow` (== InflectionInvalidAcronym "&$?%")
38+ describe "unWord" $
39+ it "extracts the inner Text value" $ do
40+ (unWord <$> mkWord "foo") `shouldReturn` "foo"
41+ (unWord <$> mkWord "bar") `shouldReturn` "bar"
42+ (unWord <$> mkAcronym "baz") `shouldReturn` "baz"
43+ describe "unSomeWord" $ do
44+ context "when inner value is a normal word" $
45+ it "Text is extracted and the given function applied" $ do
46+ x <- SomeWord <$> mkWord "word"
47+ unSomeWord T.toUpper x `shouldBe` "WORD"
48+ context "when inner value is an acronym" $
49+ it "Text is extracted, but the function is not applied" $ do
50+ x <- SomeWord <$> mkAcronym "acronym"
51+ unSomeWord T.toUpper x `shouldBe` "acronym"