on master help sign in
Fix multiline string handling and add more tests.
commit6b255fed5989f52c94c66241944b69bc66345fb0 authorMax New <maxsnew@gmail.com> dateTue 7 Jan 2014, 16:57 (13 years ago) parentcca69cf
20 files changed+52−18
compiler/Parse/Helpers.hs+18−7 tests/Tests/Compiler.hs+18−11 tests/data/bad/BBTArgs → tests/test-files/bad/BBTArgs tests/data/bad/InfiniteType.elm → tests/test-files/bad/InfiniteType.elm tests/data/bad/NonElementMain.elm → tests/test-files/bad/NonElementMain.elm tests/test-files/bad/Strings/ExtraClose.elm+4 tests/data/good/AliasSubstitution.elm → tests/test-files/good/AliasSubstitution.elm tests/data/good/NoExpressions.elm → tests/test-files/good/NoExpressions.elm tests/data/good/Otherwise.elm → tests/test-files/good/Otherwise.elm tests/data/good/QuotesAndComments.elm → tests/test-files/good/QuotesAndComments.elm tests/data/good/Soundness/Apply.elm → tests/test-files/good/Soundness/Apply.elm tests/data/good/Soundness/ApplyAnnotated.elm → tests/test-files/good/Soundness/ApplyAnnotated.elm tests/data/good/Soundness/Id.elm → tests/test-files/good/Soundness/Id.elm tests/data/good/Soundness/IdAnnotated.elm → tests/test-files/good/Soundness/IdAnnotated.elm tests/data/good/Soundness/TrickyId.elm → tests/test-files/good/Soundness/TrickyId.elm tests/data/good/Soundness/TrickyIdAnnotated.elm → tests/test-files/good/Soundness/TrickyIdAnnotated.elm tests/test-files/good/Strings/Multiline.elm+6 tests/test-files/good/Strings/MultilineNormal.elm+6 tests/data/good/Unify/LockedVars.elm → tests/test-files/good/Unify/LockedVars.elm tests/data/good/Unify/NonHomogeneousRecords.elm → tests/test-files/good/Unify/NonHomogeneousRecords.elm
compiler/Parse/Helpers.hs+18−7
@@ -312,9 +312,9 @@ markdown interpolation = try (string "[markdown|") >> closeMarkdown "" []
312312 ]
313313
314314str :: IParser String
315−str =
316− choice [ quote >> manyTill stringChar quote
317− , expecting "string" . betwixt '"' '"' . many $ stringChar
315+str = expecting "String" $
316+ choice [ quote >> manyTill multilineStringChar quote
317+ , char '"' >> manyTill stringChar (char '"')
318318 ]
319319 >>= parseStringLiteral . wrapQuotes '\"' . join
320320 where quote = try (string "\"\"\"")
@@ -323,9 +323,19 @@ wrapQuotes :: Char -> String -> String
323323wrapQuotes delim s = (delim:s ++ [delim])
324324
325325stringChar :: IParser String
326−stringChar = newline <|> escaped '\"' <|> (pure <$> satisfy (/= '\"'))
327− where newline = ['\\', 'n'] <$ char '\n'
328− <|> ['\\', 'r'] <$ char '\r'
326+stringChar = newlineChar <|> escaped '\"' <|> (pure <$> satisfy (/= '\"'))
327+
328+multilineStringChar :: IParser String
329+multilineStringChar = noEnd
330+ >> (newlineChar <|> escaped '\"' <|> expandQuote <$> anyChar)
331+ where noEnd = notFollowedBy (string "\"\"\"")
332+ expandQuote c = if c == '\"'
333+ then "\\\""
334+ else [c]
335+
336+newlineChar :: IParser String
337+newlineChar = ['\\', 'n'] <$ char '\n'
338+ <|> ['\\', 'r'] <$ char '\r'
329339
330340escaped :: Char -> IParser String
331341escaped delim = try $ do
@@ -334,7 +344,8 @@ escaped delim = try $ do
334344 return ['\\', c]
335345
336346chr :: IParser Char
337−chr = (betwixt '\'' '\'' (many1 (escaped '\'' <|> (pure <$> satisfy (/='\''))))
347+chr = (betwixt '\'' '\''
348+ (many1 (escaped '\'' <|> (pure <$> satisfy (/='\''))))
338349 >>= parseCharLiteral . wrapQuotes '\'' . join
339350 )
340351 <?> "character"
tests/Tests/Compiler.hs+18−11
@@ -7,14 +7,15 @@ import System.FilePath ((</>))
77import System.FilePath.Find (find, (==?), extension)
88import Test.Framework
99import Test.Framework.Providers.HUnit (testCase)
10−import Test.HUnit ((@=?), Assertion)
10+import Test.HUnit (Assertion, assertFailure, assertBool)
11+import Text.Parsec (ParseError)
1112
1213import Elm.Internal.Utils as Elm
1314
1415compilerTests :: Test
1516compilerTests = buildTest $ do
16− goods <- mkTests True =<< getElms "good"
17− bads <- mkTests False =<< getElms "bad"
17+ goods <- mkTests goodCompile =<< getElms "good"
18+ bads <- mkTests badCompile =<< getElms "bad"
1819 return $ testGroup "Compile Tests"
1920 [
2021 testGroup "Good Tests" goods
@@ -24,15 +25,21 @@ compilerTests = buildTest $ do
2425 where getElms :: FilePath -> IO [FilePath]
2526 getElms fname = find (return True) (extension ==? ".elm") (testsDir </> fname)
2627
27− mkTests :: Bool -> [FilePath] -> IO [Test]
28− mkTests b = traverse setupTest
29− where setupTest f = testCase f . mkCompileTest b <$> readFile f
28+ mkTests :: (Either String String -> Assertion) -> [FilePath] -> IO [Test]
29+ mkTests h = traverse setupTest
30+ where setupTest f = testCase f . mkCompileTest h <$> readFile f
3031
31− testsDir = "tests" </> "data"
32+ testsDir = "tests" </> "test-files"
3233
33−mkCompileTest :: Bool -- ^ Expect success?
34+goodCompile :: Either String String -> Assertion
35+goodCompile (Left err) = assertFailure err
36+goodCompile (Right _) = assertBool "" True
37+
38+badCompile :: Either String String -> Assertion
39+badCompile (Left _) = assertBool "" True
40+badCompile (Right _) = assertFailure "Compilation succeeded but should have failed"
41+
42+mkCompileTest :: ((Either String String) -> Assertion) -- ^ Handler
3443 -> String -- ^ File Contents
3544 -> Assertion
36−mkCompileTest succ modul = noCompileErr @=? succ
37− where noCompileErr = either (const False) (const True) . Elm.compile $ modul
38− expectation = "Compile " ++ if succ then "Success" else "Error"
45+mkCompileTest handle = handle . Elm.compile
tests/data/bad/BBTArgs → tests/test-files/bad/BBTArgsrenamed

The content is the same.

tests/data/bad/InfiniteType.elm → tests/test-files/bad/InfiniteType.elmrenamed

The content is the same.

tests/data/bad/NonElementMain.elm → tests/test-files/bad/NonElementMain.elmrenamed

The content is the same.

tests/test-files/bad/Strings/ExtraClose.elmnew file+4
@@ -0,0 +1,4 @@
1+
2+s = " " "
3+
4+main = plainText s
tests/data/good/AliasSubstitution.elm → tests/test-files/good/AliasSubstitution.elmrenamed

The content is the same.

tests/data/good/NoExpressions.elm → tests/test-files/good/NoExpressions.elmrenamed

The content is the same.

tests/data/good/Otherwise.elm → tests/test-files/good/Otherwise.elmrenamed

The content is the same.

tests/data/good/QuotesAndComments.elm → tests/test-files/good/QuotesAndComments.elmrenamed

The content is the same.

tests/data/good/Soundness/Apply.elm → tests/test-files/good/Soundness/Apply.elmrenamed

The content is the same.

tests/data/good/Soundness/ApplyAnnotated.elm → tests/test-files/good/Soundness/ApplyAnnotated.elmrenamed

The content is the same.

tests/data/good/Soundness/Id.elm → tests/test-files/good/Soundness/Id.elmrenamed

The content is the same.

tests/data/good/Soundness/IdAnnotated.elm → tests/test-files/good/Soundness/IdAnnotated.elmrenamed

The content is the same.

tests/data/good/Soundness/TrickyId.elm → tests/test-files/good/Soundness/TrickyId.elmrenamed

The content is the same.

tests/data/good/Soundness/TrickyIdAnnotated.elm → tests/test-files/good/Soundness/TrickyIdAnnotated.elmrenamed

The content is the same.

tests/test-files/good/Strings/Multiline.elmnew file+6
@@ -0,0 +1,6 @@
1+s = """
2+here's a quote: "
3+
4+"""
5+
6+main = plainText s
tests/test-files/good/Strings/MultilineNormal.elmnew file+6
@@ -0,0 +1,6 @@
1+s = "
2+here's a quote: \"
3+
4+"
5+
6+main = plainText s
tests/data/good/Unify/LockedVars.elm → tests/test-files/good/Unify/LockedVars.elmrenamed

The content is the same.

tests/data/good/Unify/NonHomogeneousRecords.elm → tests/test-files/good/Unify/NonHomogeneousRecords.elmrenamed

The content is the same.