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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
{-# LANGUAGE OverloadedStrings #-}
import Test.Hspec
import Database.Persist.Quasi
import Database.Persist.Types
main :: IO ()
main = hspec $ do
describe "tokenization" $ do
it "handles normal words" $
tokenize " foo bar baz" `shouldBe`
[ Spaces 1
, Token "foo"
, Spaces 3
, Token "bar"
, Spaces 2
, Token "baz"
]
it "handles quotes" $
tokenize " \"foo bar\" \"baz\"" `shouldBe`
[ Spaces 2
, Token "foo bar"
, Spaces 2
, Token "baz"
]
it "handles quotes mid-token" $
tokenize " x=\"foo bar\" \"baz\"" `shouldBe`
[ Spaces 2
, Token "x=foo bar"
, Spaces 2
, Token "baz"
]
it "handles escaped quote mid-token" $
tokenize " x=\\\"foo bar\" \"baz\"" `shouldBe`
[ Spaces 2
, Token "x=\\\"foo"
, Spaces 1
, Token "bar\""
, Spaces 2
, Token "baz"
]
it "handles unnested parantheses" $
tokenize " (foo bar) (baz)" `shouldBe`
[ Spaces 2
, Token "foo bar"
, Spaces 2
, Token "baz"
]
it "handles unnested parantheses mid-token" $
tokenize " x=(foo bar) (baz)" `shouldBe`
[ Spaces 2
, Token "x=foo bar"
, Spaces 2
, Token "baz"
]
it "handles nested parantheses" $
tokenize " (foo (bar)) (baz)" `shouldBe`
[ Spaces 2
, Token "foo (bar)"
, Spaces 2
, Token "baz"
]
it "escaping" $
tokenize " (foo \\(bar) y=\"baz\\\"\"" `shouldBe`
[ Spaces 2
, Token "foo (bar"
, Spaces 2
, Token "y=baz\""
]
it "mid-token quote in later token" $
tokenize "foo bar baz=(bin\")" `shouldBe`
[ Token "foo"
, Spaces 1
, Token "bar"
, Spaces 1
, Token "baz=bin\""
]
describe "parseFieldType" $ do
it "simple types" $
parseFieldType "FooBar" `shouldBe` Just (FTTypeCon Nothing "FooBar")
it "module types" $
parseFieldType "Data.Map.FooBar" `shouldBe` Just (FTTypeCon (Just "Data.Map") "FooBar")
it "application" $
parseFieldType "Foo Bar" `shouldBe` Just (
FTTypeCon Nothing "Foo" `FTApp` FTTypeCon Nothing "Bar")
it "application multiple" $
parseFieldType "Foo Bar Baz" `shouldBe` 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)" `shouldBe` 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" `shouldBe` Just (
foo `FTApp` bars `FTApp` baz)
describe "stripId" $ do
it "works" $
(parseFieldType "FooId" >>= stripId) `shouldBe` Just "Foo"
|