summaryrefslogtreecommitdiff
path: root/tests/Test.hs
blob: c0d6932240a1bb587481c6effc91e9bffdbf6e86 (plain)
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
{-# LANGUAGE QuasiQuotes, ExtendedDefaultRules, OverloadedStrings, IncoherentInstances #-}

module Main where

import Data.ByteString.Char8 as BS(ByteString, pack)
import Data.Text as T(Text, pack)
import GHC.Exts(fromString)
import Test.HUnit
import Text.InterpolatedString.QQ2

data Foo = Foo Int String deriving Show

t1 :: String
t1 = "字元"

testEmpty       = assertBool "" ([qc||] == "")
testCharLiteral = assertBool "" ([qc|#{1+2}|] == "3")
testString      = assertBool "" ([qc|a string #{t1} is here|] == "a string 字元 is here")
testEscape      = assertBool "" ([qc|\#{ }|] == "#{ }")
testComplex     = assertBool "" ([qc|
        \ok
#{Foo 4 "Great!" : [Foo 3 "Scott!"]}
        then
|] == ("\n" ++
    "        \\ok\n" ++
    "[Foo 4 \"Great!\",Foo 3 \"Scott!\"]\n" ++
    "        then\n"))
testConvert = assertBool ""
              (([qc|#{fromString "a"::Text} #{fromString "b"::ByteString}|] :: String)
               == "a b")

tests = TestList
    [ TestLabel "Empty String"       $ TestCase testEmpty
    , TestLabel "Character Literal"  $ TestCase testCharLiteral
    , TestLabel "String Variable"    $ TestCase testString
    , TestLabel "Escape Sequences"   $ TestCase testEscape
    , TestLabel "Complex Expression" $ TestCase testComplex
    , TestLabel "String Conversion"  $ TestCase testConvert
    , TestLabel "ByteString Test"    $ TestCase testByteString
    , TestLabel "Text Test"          $ TestCase testText
    ]

main = runTestTT tests


-- the primary purpose of these tests is to ensure that
-- the Text and ByteString rewrite rules are firing, to avoid
-- needlessly converting string types
testByteString = assertBool "" $ [qc|#{"a" :: ByteString} #{"b" :: ByteString}|]
                 == BS.pack ("a b")
testText = assertBool "" $ [qc|#{"a" :: Text} #{"b" :: Text}|]
           == T.pack ("a b")