Commit 9771c366 authored by Oleg Grenrus's avatar Oleg Grenrus
Browse files

Add nbsp warning test

Note: we notify only about nbsp at the beginning of the line
nbsp inside fields get just parsed atm
parent 4a4cea68
......@@ -304,6 +304,7 @@ extra-source-files:
tests/PackageTests/UniqueIPID/P2/my.cabal
tests/PackageTests/multInst/my.cabal
tests/ParserTests/warnings/bom.cabal
tests/ParserTests/warnings/nbsp.cabal
tests/Setup.hs
tests/hackage/check.sh
tests/hackage/download.sh
......
......@@ -76,7 +76,7 @@ tokens :-
}
<bol_section, bol_field_layout, bol_field_braces> {
$nbspspacetab* @nl { \_ _ inp -> checkWhitespace inp >> adjustPos retPos >> lexToken }
$nbspspacetab* @nl { \_pos len inp -> checkWhitespace len inp >> adjustPos retPos >> lexToken }
-- no @nl here to allow for comments on last line of the file with no trailing \n
$spacetab* "--" $comment* ; -- TODO: check the lack of @nl works here
-- including counting line numbers
......@@ -84,7 +84,7 @@ tokens :-
<bol_section> {
$nbspspacetab* --TODO prevent or record leading tabs
{ \pos len inp -> checkWhitespace inp >>
{ \pos len inp -> checkWhitespace len inp >>
if B.length inp == len
then return (L pos EOF)
else setStartCode in_section
......@@ -162,13 +162,11 @@ toki t pos len input = return $! L pos (t (B.take len input))
tok :: Monad m => Token -> Position -> t -> t1 -> m LToken
tok t pos _len _input = return $! L pos t
checkWhitespace :: ByteString -> Lex ()
checkWhitespace bs
| B.any (== 194) bs = addWarning LexWarningNBSP "Non-breaking space found"
| otherwise = return ()
whitespace :: Position -> Int -> ByteString -> Lex ()
whitespace _ _ = checkWhitespace
checkWhitespace :: Int -> ByteString -> Lex ()
checkWhitespace len bs
| B.any (== 194) (B.take len bs) =
addWarning LexWarningNBSP "Non-breaking space found"
| otherwise = return ()
-- -----------------------------------------------------------------------------
-- The input type
......
......@@ -23,6 +23,7 @@ tests = testGroup "parsec tests"
warningTests :: TestTree
warningTests = testGroup "warnings triggered"
[ warningTest PWTLexBOM "bom.cabal"
, warningTest PWTLexNBSP "nbsp.cabal"
]
warningTest :: PWarnType -> FilePath -> TestTree
......@@ -30,7 +31,7 @@ warningTest wt fp = testCase (show wt) $ do
contents <- BS.readFile $ "tests" </> "ParserTests" </> "warnings" </> fp
let res = parseGenericPackageDescription contents
let (warns, errs, x) = runParseResult res
assertBool "parses successfully" $ isJust x
assertBool "parses without errors" $ null errs
......
name: nbsp
version: 1
cabal-version: >= 1.6
library
  build-depends: base >= 4.9 && <4.10
hs-source-dirs: .
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment