Skip to content

Commit 82367ec

Browse files
author
Jaro Reinders
committed
Resolve some of soulomoon's feedback
1 parent 79341ed commit 82367ec

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

hls-test-utils/src/Development/IDE/Test.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ import System.FilePath (equalFilePath)
6363
import System.Time.Extra
6464
import Test.Tasty.HUnit
6565

66+
expectedDiagnosticWithNothing :: ExpectedDiagnostic -> ExpectedDiagnosticWithTag
67+
expectedDiagnosticWithNothing (ds, c, t, code) = (ds, c, t, code, Nothing)
68+
6669
requireDiagnosticM
6770
:: (Foldable f, Show (f Diagnostic), HasCallStack)
6871
=> f Diagnostic
69-
-> (DiagnosticSeverity, Cursor, T.Text, Maybe T.Text, Maybe DiagnosticTag)
72+
-> ExpectedDiagnosticWithTag
7073
-> Assertion
7174
requireDiagnosticM actuals expected = case requireDiagnostic actuals expected of
7275
Nothing -> pure ()
@@ -114,15 +117,15 @@ flushMessages = do
114117
--
115118
-- Rather than trying to assert the absence of diagnostics, introduce an
116119
-- expected diagnostic (e.g. a redundant import) and assert the singleton diagnostic.
117-
expectDiagnostics :: HasCallStack => [(FilePath, [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text)])] -> Session ()
120+
expectDiagnostics :: HasCallStack => [(FilePath, [ExpectedDiagnostic])] -> Session ()
118121
expectDiagnostics
119122
= expectDiagnosticsWithTags
120-
. map (second (map (\(ds, c, t, code) -> (ds, c, t, code, Nothing))))
123+
. map (second (map expectedDiagnosticWithNothing))
121124

122125
unwrapDiagnostic :: TServerMessage Method_TextDocumentPublishDiagnostics -> (Uri, [Diagnostic])
123126
unwrapDiagnostic diagsNot = (diagsNot^. L.params . L.uri, diagsNot^. L.params . L.diagnostics)
124127

125-
expectDiagnosticsWithTags :: HasCallStack => [(String, [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text, Maybe DiagnosticTag)])] -> Session ()
128+
expectDiagnosticsWithTags :: HasCallStack => [(String, [ExpectedDiagnosticWithTag])] -> Session ()
126129
expectDiagnosticsWithTags expected = do
127130
let toSessionPath = getDocUri >=> liftIO . canonicalizeUri >=> pure . toNormalizedUri
128131
next = unwrapDiagnostic <$> skipManyTill anyMessage diagnostic
@@ -132,7 +135,7 @@ expectDiagnosticsWithTags expected = do
132135
expectDiagnosticsWithTags' ::
133136
(HasCallStack, MonadIO m) =>
134137
m (Uri, [Diagnostic]) ->
135-
Map.Map NormalizedUri [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text, Maybe DiagnosticTag)] ->
138+
Map.Map NormalizedUri [ExpectedDiagnosticWithTag] ->
136139
m ()
137140
expectDiagnosticsWithTags' next m | null m = do
138141
(_,actual) <- next
@@ -170,14 +173,14 @@ expectDiagnosticsWithTags' next expected = go expected
170173
<> show actual
171174
go $ Map.delete canonUri m
172175

173-
expectCurrentDiagnostics :: HasCallStack => TextDocumentIdentifier -> [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text)] -> Session ()
176+
expectCurrentDiagnostics :: HasCallStack => TextDocumentIdentifier -> [ExpectedDiagnostic] -> Session ()
174177
expectCurrentDiagnostics doc expected = do
175178
diags <- getCurrentDiagnostics doc
176179
checkDiagnosticsForDoc doc expected diags
177180

178-
checkDiagnosticsForDoc :: HasCallStack => TextDocumentIdentifier -> [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text)] -> [Diagnostic] -> Session ()
181+
checkDiagnosticsForDoc :: HasCallStack => TextDocumentIdentifier -> [ExpectedDiagnostic] -> [Diagnostic] -> Session ()
179182
checkDiagnosticsForDoc TextDocumentIdentifier {_uri} expected obtained = do
180-
let expected' = Map.singleton nuri (map (\(ds, c, t, code) -> (ds, c, t, code, Nothing)) expected)
183+
let expected' = Map.singleton nuri (map expectedDiagnosticWithNothing expected)
181184
nuri = toNormalizedUri _uri
182185
expectDiagnosticsWithTags' (return (_uri, obtained)) expected'
183186

hls-test-utils/src/Development/IDE/Test/Diagnostic.hs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,39 @@ cursorPosition (line, col) = Position line col
1616

1717
type ErrorMsg = String
1818

19+
20+
-- | Expected diagnostics have the following components:
21+
--
22+
-- 1. severity
23+
-- 2. cursor (line and column numbers)
24+
-- 3. infix of the message
25+
-- 4. code (e.g. GHC-87543)
26+
type ExpectedDiagnostic =
27+
( DiagnosticSeverity
28+
, Cursor
29+
, T.Text
30+
, Maybe T.Text
31+
)
32+
33+
-- | Expected diagnostics with a tag have the following components:
34+
--
35+
-- 1. severity
36+
-- 2. cursor (line and column numbers)
37+
-- 3. infix of the message
38+
-- 4. code (e.g. GHC-87543)
39+
-- 5. tag (unnecessary or deprecated)
40+
type ExpectedDiagnosticWithTag =
41+
( DiagnosticSeverity
42+
, Cursor
43+
, T.Text
44+
, Maybe T.Text
45+
, Maybe DiagnosticTag
46+
)
47+
1948
requireDiagnostic
2049
:: (Foldable f, Show (f Diagnostic), HasCallStack)
2150
=> f Diagnostic
22-
-> (DiagnosticSeverity, Cursor, T.Text, Maybe T.Text, Maybe DiagnosticTag)
51+
-> ExpectedDiagnosticWithTag
2352
-> Maybe ErrorMsg
2453
requireDiagnostic actuals expected@(severity, cursor, expectedMsg, mbExpectedCode, expectedTag)
2554
| any match actuals = Nothing

plugins/hls-refactor-plugin/test/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ suggestImportDisambiguationTests = testGroup "suggest import disambiguation acti
19951995
compareHideFunctionTo = compareTwo "HideFunction.hs"
19961996
withTarget file locs k = runWithExtraFiles "hiding" $ \dir -> do
19971997
doc <- openDoc file "haskell"
1998-
void $ expectDiagnostics [(file, [(DiagnosticSeverity_Error, loc, "Ambiguous occurrence", Nothing) | loc <- locs])] -- TODO: Give this a proper error
1998+
void $ expectDiagnostics [(file, [(DiagnosticSeverity_Error, loc, "Ambiguous occurrence", Just "GHC-87543") | loc <- locs])]
19991999
actions <- getAllCodeActions doc
20002000
k dir doc actions
20012001
withHideFunction = withTarget ("HideFunction" <.> "hs")

test/functional/Config.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type instance RuleResult GetTestDiagnostics = ()
110110

111111
expectDiagnosticsFail
112112
:: HasCallStack
113-
=> ExpectBroken 'Ideal [(FilePath, [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text)])]
114-
-> ExpectBroken 'Current [(FilePath, [(DiagnosticSeverity, Cursor, T.Text, Maybe T.Text)])]
113+
=> ExpectBroken 'Ideal [(FilePath, [ExpectedDiagnostic])]
114+
-> ExpectBroken 'Current [(FilePath, [ExpectedDiagnostic])]
115115
-> Session ()
116116
expectDiagnosticsFail _ = expectDiagnostics . unCurrent

0 commit comments

Comments
 (0)