git.iko.soy/typeable/inflections-hs
on master help sign in
Better define what a word is, tests for ‘Types.hs’
commit2348bb717c36a9c668b9b3780fbc6c6f19213830 authormrkkrp <markkarpov@opmbx.org> dateWed 21 Dec 2016, 20:01 (10 years ago) parent4d3bb48
4 files changed+69−14
Text/Inflections/Types.hs+14−12 inflections.cabal+1 test/Text/Inflections/HumanizeSpec.hs+3−2 test/Text/Inflections/TypesSpec.hs+51
Text/Inflections/Types.hs+14−12
@@ -31,7 +31,7 @@ module Text.Inflections.Types
3131where
3232
3333import Control.Monad.Catch
34−import Data.Char (isSpace)
34+import Data.Char (isAlphaNum)
3535import Data.Data (Data)
3636import Data.Text (Text)
3737import Data.Typeable (Typeable)
@@ -43,28 +43,30 @@ import qualified Data.Text as T
4343import Prelude hiding (Word)
4444#endif
4545
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)
4748-- 'InflectionInvalidWord' will be thrown.
4849--
4950-- /since 0.3.0.0/
5051
5152mkWord :: MonadThrow m => Text -> m (Word 'Normal)
5253mkWord txt =
53− if T.any isSpace txt
54− then throwM (InflectionInvalidWord txt)
55− else return (Word txt)
56−
57−-- | Create an acronym from given 'Text'. The input should not contain
58−-- spaces or 'InflectionInvalidAcronym' will be thrown. Acronym is different
59−-- from normal word by that it may not be transformed by inflections.
54+ if T.all isAlphaNum txt
55+ then return (Word txt)
56+ else throwM (InflectionInvalidWord txt)
57+
58+-- | Create an acronym from given 'Text'. The input should consist of only
59+-- alpha-numeric characters 'InflectionInvalidAcronym' will be thrown.
60+-- Acronym is different from normal word by that it may not be transformed
61+-- by inflections (also see 'unSomeWord').
6062--
6163-- /since 0.3.0.0/
6264
6365mkAcronym :: MonadThrow m => Text -> m (Word 'Acronym)
6466mkAcronym txt =
65− if T.any isSpace txt
66− then throwM (InflectionInvalidAcronym txt)
67− else return (Word txt)
67+ if T.all isAlphaNum txt
68+ then return (Word txt)
69+ else throwM (InflectionInvalidAcronym txt)
6870
6971-- | A 'Text' value that should be kept whole through applied inflections.
7072
inflections.cabal+1
@@ -78,5 +78,6 @@ test-suite test
7878 , Text.Inflections.Parse.SnakeCaseSpec
7979 , Text.Inflections.TitleizeSpec
8080 , Text.Inflections.TransliterateSpec
81+ , Text.Inflections.TypesSpec
8182 , Text.Inflections.UnderscoreSpec
8283 , Text.InflectionsSpec
test/Text/Inflections/HumanizeSpec.hs+3−2
@@ -19,8 +19,9 @@ spec = describe "humazine" $ do
1919 humanize [employee,salary] `shouldBe` "Employee salary"
2020 it "turns underscores into spaces" $ do
2121 employee <- SomeWord <$> mkWord "employee"
22− hasSalary <- SomeWord <$> mkWord "has_salary"
23− humanize [employee, hasSalary] `shouldBe` "Employee has salary"
22+ has <- SomeWord <$> mkWord "has"
23+ salary <- SomeWord <$> mkWord "salary"
24+ humanize [employee, has, salary] `shouldBe` "Employee has salary"
2425 it "capitalizes the first word of a sentence" $ do
2526 underground <- SomeWord <$> mkWord "underground"
2627 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"