diff --git a/default.nix b/default.nix index b4e7e2b..2c6dfe3 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,7 @@ { mkDerivation, aeson, base, bytestring, containers , directory, deepseq, effectful, filemanip, filepath , optparse-applicative, parsec, pretty, signal, lib, uu-parsinglib -, uuagc, uuagc-cabal, tasty, tasty-golden +, uuagc, uuagc-cabal, tasty, tasty-golden, tasty-hunit }: let # Clean up the source of the derivation to prevent rebuilds @@ -51,6 +51,7 @@ in mkDerivation { uuagc-cabal tasty tasty-golden + tasty-hunit ]; executableHaskellDepends = [ aeson diff --git a/glualint.cabal b/glualint.cabal index 194495d..0dd7c55 100644 --- a/glualint.cabal +++ b/glualint.cabal @@ -79,6 +79,20 @@ test-suite golden default-language: Haskell2010 +test-suite linttest + type: exitcode-stdio-1.0 + main-is: Spec.hs + hs-source-dirs: tests/linttest + build-depends: + , base + , bytestring + , glualint + , tasty >=1.4 && <2 + , tasty-hunit >=0.10 && <0.12 + , uu-parsinglib + + default-language: Haskell2010 + executable glualint -- Disable the executable for ghcjs and older versions of ghc. The executable -- uses functions only available in ghc 8+ diff --git a/tests/linttest/Spec.hs b/tests/linttest/Spec.hs new file mode 100644 index 0000000..ae9a286 --- /dev/null +++ b/tests/linttest/Spec.hs @@ -0,0 +1,46 @@ +import qualified Data.ByteString.Lazy as LBS +import qualified Data.ByteString.Lazy.Char8 as LBS8 +import GLua.AG.Token (Region (..)) +import qualified GLuaFixer.Interface as GLua +import GLuaFixer.LintMessage ( + Issue (..), + LintMessage (..), + Severity (..), + ) +import GLuaFixer.LintSettings (LintSettings, defaultLintSettings) +import Test.Tasty (TestTree, defaultMain, testGroup) +import Test.Tasty.HUnit (testCase, (@=?)) +import Text.ParserCombinators.UU.BasicInstances (LineColPos (..)) + +main :: IO () +main = defaultMain tests + +tests :: TestTree +tests = testGroup "Unit tests" [testQuotedStringWarningPosition] + +-- | Regression test for https://github.com/FPtje/GLuaFixer/issues/169 +testQuotedStringWarningPosition :: TestTree +testQuotedStringWarningPosition = + testCase "Assert that the warning is thrown and in the right region" $ + let + input = "bar = a or b\nfoo = \"\" and \"\" and \"dddd\" || \"[]\"" + expectedRegion = Region (LineColPos 1 27 40) (LineColPos 1 29 42) + warning = SyntaxInconsistency "||" "or" + msg = LintMessage LintWarning expectedRegion warning testFilePath + in + lintString input @=? [msg] + +-- | Helper to lint a string +lintString :: String -> [LintMessage] +lintString input = + let + settings = defaultLintSettings + in + case GLua.lex settings testFilePath input of + Left errs -> errs + Right mTokens -> case GLua.parse settings testFilePath mTokens of + Left errs -> errs + Right ast -> GLua.lexiconLint testFilePath settings mTokens <> GLua.astLint testFilePath settings ast + +testFilePath :: String +testFilePath = "test input"