lentil-0.1.4.0: test/Lentil/Parse/SourceSpec.hs
module Lentil.Parse.SourceSpec where
import Test.Hspec
import Text.Parsec (runParser)
import Text.Parsec.Char
import Text.Parsec.Combinator
import Lentil.Types
import Lentil.Parse.Source
import Prelude -- 7.8 hack
-- Parsing tests
-- simple parser
sp :: ParSource a -> String -> Maybe a
sp p cs = either (const Nothing) Just
(runParser p () fp cs)
where fp = "<f>"
-- short comment constructors
sl, ml :: Row -> String -> CommentString
sl r t = SingleLine r t
ml r t = MultiLine r t
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "manyTill1" $ do
it "behaves like manyTill on non-empty string" $
sp (manyTill1 anyChar newline) "foo\n" `shouldBe`
sp (manyTill anyChar newline) "foo\n"
it "fails on empty string (while manyTill does not)" $
(sp (manyTill1 anyChar newline) "\n",
sp (manyTill anyChar newline) "\n") `shouldBe` (Nothing,
Just "")
it "has the same behaviour as manyTill on 1 char w/o end char" $
sp (manyTill1 anyChar newline) "f" `shouldBe`
sp (manyTill anyChar newline) "f"
describe "lineComment" $ do
it "parses a line comment" $
sp (lineComment "//") "// Test\n" `shouldBe` Just (sl 1 " Test")
it "fails without trailing \\n" $
sp (lineComment "//") "// Test" `shouldBe` Nothing
describe "blockComment" $ do
it "parses a block comment" $
sp (blockComment ("/*","*/")) "/* Test\n2 */" `shouldBe`
Just (ml 1 " Test\n2 ")
describe "litString" $ do
it "parses code string" $
sp (litString ClangLike '"') "\"palla\"" `shouldBe` Just "palla"
it "parses code string with escaped \" inside" $
sp (litString ClangLike '"') "\"pal\\\"la\"" `shouldBe` Just "pal\"la"
it "parses code string with comments symbols inside" $
sp (litString ClangLike '"') "\"pal#la\"" `shouldBe` Just "pal#la"
it "parses escape character (Pascal, SQL)" $
sp (litString SQLLike '\'') "\'\\\'" `shouldBe` Just "\\"
describe "litChar" $ do
it "parses a string literal character inside" $
sp (litChar '\'') "'a'" `shouldBe` Just 'a'
it "parses escaped characters too" $
sp (litChar '\'') "'\"'" `shouldBe` Just '\"'
let hss = ParSyntax ["--"] [("{-", "-}")] ClangLike ['"'] ['\'']
describe "program" $ do
it "parses program instructions till eof" $
sp (program hss) "prova " `shouldBe` Just "prova "
it "stops at single-line comment" $
sp (program hss) "prova -- " `shouldBe` Just "prova "
it "stops at blockcomment" $
sp (program hss) "prova {- " `shouldBe` Just "prova "
it "stops at literal string" $
sp (program hss) "prova \"babby " `shouldBe` Just "prova "
it "stops at literal char" $
sp (program hss) "prova ' '" `shouldBe` Just "prova "
it "stops at ' which is not a literal char" $
sp (program hss) "prova' " `shouldBe` Just "prova' "
let rbs = ParSyntax ["#"] [] ClangLike ['"', '\''] []
describe "source" $ do
it "parses one piece of source (line-comment)" $
sp (source hss) "-- hey\n my " `shouldBe` Just [sl 1 " hey"]
it "parses one piece of source (block-comment)" $
sp (source hss) "{-hey-}\n my " `shouldBe` Just [ml 1 "hey"]
it "parses one piece of source (string-literal)" $
sp (source hss) "\"hey\"" `shouldBe` Just []
it "parses a string for language with ' and \" available" $
sp (source rbs) "\"he#y\"" `shouldBe` Just []
it "parses one piece of source (char-literal)" $
sp (source hss) "\'h\'" `shouldBe` Just []
it "parses one piece of source (program instructions)" $
sp (source hss) "prime'" `shouldBe` Just []
it "should not choke on en empty file" $
sp (source hss) "" `shouldBe` Just []
describe "groupLineComms" $ do
it "groups line comments" $
groupLineComms [SingleLine 1 "a",
SingleLine 2 "b",
SingleLine 4 "c"] `shouldBe` [(1, "a\nb\n"),
(4, "c\n" )]
describe "comms2Tuple" $ do
it "groups comments" $
comms2Tuple [SingleLine 1 "a",
SingleLine 2 "b",
MultiLine 5 "c",
SingleLine 6 "d"] `shouldBe` [(1, "a\nb\n"),
(5, "c" ),
(6, "d\n" )]