Skip to content

Commit

Permalink
Better labels in Haskell/Diagnostics.hs noci
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed May 6, 2024
1 parent 9f48208 commit 5fce42f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
82 changes: 42 additions & 40 deletions tests/app/Spec/Tests/Haskell/Diagnostics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,48 @@ import TestLib.NixEnvironmentContext

diagnosticsTests :: (LspContext context m) => Text -> SpecFree context m ()
diagnosticsTests lsName = describe "Diagnostics" $ do
testDiagnostics lsName "Foo.hs" Nothing [__i|module Foo where
foo = bar
|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 1 6) (Position 1 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnostics lsName "Foo.hs" Nothing etaExpandCode $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 6 0) (Position 6 14), Just (InR "refact:Eta reduce"))]

testDiagnostics lsName "main.ipynb" Nothing [__i|-- A comment
foo = bar

putStrLn "HI"
|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 1 6) (Position 1 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnostics lsName "main.ipynb" Nothing [__i|import Data.Aeson.TH
{-\# LANGUAGE TemplateHaskell \#-}
foo = bar -- This should be the only diagnostic we get
data Foo = Bar | Baz
$(deriveJSON defaultOptions ''Foo)|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 2 6) (Position 2 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnostics lsName "main.ipynb" Nothing [__i|import Data.Aeson as A
import Data.Aeson.TH
:set -XTemplateHaskell
foo = bar -- This should be the only diagnostic we get
data Foo = Bar | Baz
$(deriveJSON defaultOptions ''Foo)
import Data.ByteString.Lazy.Char8 as BL
Prelude.putStrLn $ BL.unpack $ A.encode Bar|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 3 6) (Position 3 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnostics lsName "main.ipynb" Nothing [__i|-- Some comment
import Data.ByteString.Lazy.Char8 as BL
foo = bar

putStrLn "HI"
|] $ \diagnostics -> case [(x ^. range, x ^. message) | x <- diagnostics] of
[(Range (Position 4 0) (Position 4 8), x)] | containsAll x ["Ambiguous occurrence", "putStrLn"] -> return ()
xs -> expectationFailure [i|Unexpected diagnostics: #{xs}|]
describe "Foo.hs" $ do
testDiagnosticsLabel "Out of scope variable" lsName "Foo.hs" Nothing [__i|module Foo where
foo = bar
|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 1 6) (Position 1 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnosticsLabel "Eta reduce" lsName "Foo.hs" Nothing etaExpandCode $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 6 0) (Position 6 14), Just (InR "refact:Eta reduce"))]

describe "main.ipynb" $ do
testDiagnosticsLabel "Top-level putStrLn" lsName "main.ipynb" Nothing [__i|-- A comment
foo = bar

putStrLn "HI"
|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 1 6) (Position 1 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnosticsLabel "Top-level putStrLn with diagnostic" lsName "main.ipynb" Nothing [__i|-- Some comment
import Data.ByteString.Lazy.Char8 as BL
foo = bar

putStrLn "HI"
|] $ \diagnostics -> case [(x ^. range, x ^. message) | x <- diagnostics] of
[(Range (Position 4 0) (Position 4 8), x)] | containsAll x ["Ambiguous occurrence", "putStrLn"] -> return ()
xs -> expectationFailure [i|Unexpected diagnostics: #{xs}|]

testDiagnosticsLabel "Reordering" lsName "main.ipynb" Nothing [__i|import Data.Aeson.TH
{-\# LANGUAGE TemplateHaskell \#-}
foo = bar -- This should be the only diagnostic we get
data Foo = Bar | Baz
$(deriveJSON defaultOptions ''Foo)|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 2 6) (Position 2 9), Just (InR "-Wdeferred-out-of-scope-variables"))]

testDiagnosticsLabel "Complicated reordering" lsName "main.ipynb" Nothing [__i|import Data.Aeson as A
import Data.Aeson.TH
:set -XTemplateHaskell
foo = bar -- This should be the only diagnostic we get
data Foo = Bar | Baz
$(deriveJSON defaultOptions ''Foo)
import Data.ByteString.Lazy.Char8 as BL
Prelude.putStrLn $ BL.unpack $ A.encode Bar|] $ \diagnostics -> do
assertDiagnosticRanges diagnostics [(Range (Position 3 6) (Position 3 9), Just (InR "-Wdeferred-out-of-scope-variables"))]


etaExpandCode :: Text
Expand Down
5 changes: 5 additions & 0 deletions tests/src/TestLib/LSP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ testDiagnostics' :: (
) => Text -> FilePath -> Maybe Text -> Text -> [(FilePath, B.ByteString)] -> ([Diagnostic] -> ExampleT ctx m ()) -> SpecFree ctx m ()
testDiagnostics' name filename maybeLanguageId codeToTest = testDiagnostics'' [i|#{name}, #{filename} with #{show codeToTest} (diagnostics)|] name filename maybeLanguageId codeToTest

testDiagnosticsLabel :: (
LspContext ctx m
) => String -> Text -> FilePath -> Maybe Text -> Text -> ([Diagnostic] -> ExampleT ctx m ()) -> SpecFree ctx m ()
testDiagnosticsLabel label name filename maybeLanguageId codeToTest = testDiagnostics'' label name filename maybeLanguageId codeToTest []

testDiagnostics'' :: (
LspContext ctx m
) => String -> Text -> FilePath -> Maybe Text -> Text -> [(FilePath, B.ByteString)] -> ([Diagnostic] -> ExampleT ctx m ()) -> SpecFree ctx m ()
Expand Down

0 comments on commit 5fce42f

Please sign in to comment.