1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
{-# LANGUAGE OverloadedStrings #-}
import Test.Hspec.Monadic
import Test.Hspec.HUnit ()
import Test.HUnit
import Database.Persist.Quasi
import Database.Persist.EntityDef
main :: IO ()
main = hspecX $ do
describe "tokenization" $ do
it "handles normal words" $
tokenize " foo bar baz" @?=
[ Spaces 1
, Token "foo"
, Spaces 3
, Token "bar"
, Spaces 2
, Token "baz"
]
it "handles quotes" $
tokenize " \"foo bar\" \"baz\"" @?=
[ Spaces 2
, Token "foo bar"
, Spaces 2
, Token "baz"
]
it "handles unnested parantheses" $
tokenize " (foo bar) (baz)" @?=
[ Spaces 2
, Token "foo bar"
, Spaces 2
, Token "baz"
]
it "handles nested parantheses" $
tokenize " (foo (bar)) (baz)" @?=
[ Spaces 2
, Token "foo (bar)"
, Spaces 2
, Token "baz"
]
it "escaping " $
tokenize " (foo \\(bar) \"baz\\\"\"" @?=
[ Spaces 2
, Token "foo (bar"
, Spaces 2
, Token "baz\""
]
describe "parseFieldType" $ do
it "simple types" $
parseFieldType "FooBar" @?= Just (FTTypeCon Nothing "FooBar")
it "module types" $
parseFieldType "Data.Map.FooBar" @?= Just (FTTypeCon (Just "Data.Map") "FooBar")
it "application" $
parseFieldType "Foo Bar" @?= Just (
FTTypeCon Nothing "Foo" `FTApp` FTTypeCon Nothing "Bar")
it "application multiple" $
parseFieldType "Foo Bar Baz" @?= Just (
(FTTypeCon Nothing "Foo" `FTApp` FTTypeCon Nothing "Bar")
`FTApp` FTTypeCon Nothing "Baz"
)
it "parens" $ do
let foo = FTTypeCon Nothing "Foo"
bar = FTTypeCon Nothing "Bar"
baz = FTTypeCon Nothing "Baz"
parseFieldType "Foo (Bar Baz)" @?= Just (
foo `FTApp` (bar `FTApp` baz))
it "lists" $ do
let foo = FTTypeCon Nothing "Foo"
bar = FTTypeCon Nothing "Bar"
bars = FTList bar
baz = FTTypeCon Nothing "Baz"
parseFieldType "Foo [Bar] Baz" @?= Just (
foo `FTApp` bars `FTApp` baz)
describe "stripId" $ do
it "works" $
(parseFieldType "FooId" >>= stripId) @?= Just "Foo"
|