Skip to content

Commit

Permalink
Add tests for diagnostics handler
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-tielen committed Sep 17, 2023
1 parent cc858af commit e2a3298
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/Eclair/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ posToSourcePos (Pos l c) =

-- Actual location in the code (a range).
-- Contains the file, start and end of the position.
-- TODO make this the leading location type instead of SourceSpan
data Location
= Location
{ locationFile :: FilePath
Expand Down
2 changes: 1 addition & 1 deletion lib/Eclair/LSP/Handlers/Diagnostics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ diagnosticsHandler path = do
mFileContents <- lift $ vfsLookupFile path
case mFileContents of
Nothing ->
pure $ DiagnosticsError path Nothing "Failed to read file from VFS!"
pure $ DiagnosticsError path Nothing "File not found in VFS!"
Just fileContents -> do
errs <- liftLSP $ emitDiagnostics params path
diagnostics <- mconcat <$> traverse (errorToDiagnostics fileContents) errs
Expand Down
29 changes: 26 additions & 3 deletions tests/eclair/Test/Eclair/LSP/HandlersSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ spec :: Spec
spec = describe "LSP handlers" $ parallel $ do
hoverSpec
documentHighlightSpec

describe "diagnostics" $ parallel $ do
it "" pending
diagnosticsSpec

hoverSpec :: Spec
hoverSpec = describe "Hover action" $ do
Expand Down Expand Up @@ -98,6 +96,31 @@ documentHighlightSpec = describe "Document highlight action" $ do
result <- withLSP (Just file) $ documentHighlightHandler file srcPos
result `shouldBe` DocHLError file srcPos "Failed to get highlight information!"

diagnosticsSpec :: Spec
diagnosticsSpec = describe "Diagnostics action" $ parallel $ do
it "reports nothing if file is OK" pending

it "reports invalid syntax" $ do
let file = fixture "invalid_syntax.eclair"
DiagnosticsOk diags <- withLSP (Just file) $ diagnosticsHandler file
length diags `shouldBe` 2

it "returns an error if file not found in vfs" $ do
let file = "not_found.eclair"
result <- withLSP Nothing $ diagnosticsHandler file
result `shouldBe` DiagnosticsError file Nothing "File not found in VFS!"

it "reports semantic errors" $ do
let file = fixture "semantic_errors.eclair"
DiagnosticsOk [diag] <- withLSP (Just file) $ diagnosticsHandler file
let (Diagnostic _ _ _ msg) = diag
toString msg `shouldContain` "Wildcard in top level fact"

it "reports type errors" $ do
let file = fixture "type_errors.eclair"
DiagnosticsOk (_:_:diag:_) <- withLSP (Just file) $ diagnosticsHandler file
let (Diagnostic _ _ _ msg) = diag
toString msg `shouldContain` "Type mismatch"

fixture :: FilePath -> FilePath
fixture file =
Expand Down
5 changes: 5 additions & 0 deletions tests/eclair/fixtures/lsp/invalid_syntax.eclair
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@def number(u32) output.

number(123, ).
number(456).
number(789, ).
3 changes: 3 additions & 0 deletions tests/eclair/fixtures/lsp/semantic_errors.eclair
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@def wildcard_in_fact(u32) output.

wildcard_in_fact(_).

0 comments on commit e2a3298

Please sign in to comment.