Give a nicer error message when under is passed empty

This commit is contained in:
Jeroen Engels 2020-02-11 18:47:28 +01:00
parent b72326085b
commit f28b7698d7
3 changed files with 77 additions and 24 deletions

View File

@ -841,16 +841,16 @@ checkErrorsMatch runResult expectedErrors errors =
checkErrorMatch : CodeInspector -> ExpectedError -> Error -> (() -> Expectation)
checkErrorMatch codeInspector ((ExpectedError expectedError_) as expectedError) error_ =
Expect.all
[ (expectedError_.message == Rule.errorMessage error_)
|> Expect.true
(ErrorMessage.messageMismatch
(extractExpectedErrorData expectedError)
error_
)
|> always
[ \() ->
(expectedError_.message == Rule.errorMessage error_)
|> Expect.true
(ErrorMessage.messageMismatch
(extractExpectedErrorData expectedError)
error_
)
, checkMessageAppearsUnder codeInspector error_ expectedError
, checkDetailsAreCorrect error_ expectedError
, always <| checkFixesAreCorrect codeInspector error_ expectedError
, \() -> checkFixesAreCorrect codeInspector error_ expectedError
]
@ -861,27 +861,37 @@ checkMessageAppearsUnder codeInspector error_ (ExpectedError expectedError) =
case expectedError.under of
Under under ->
Expect.all
[ (codeAtLocation == under)
|> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation })
|> always
, codeInspector.checkIfLocationIsAmbiguous error_ under
|> always
[ \() ->
case under of
"" ->
ErrorMessage.underMayNotBeEmpty
{ message = expectedError.message
, codeAtLocation = codeAtLocation
}
|> Expect.fail
_ ->
Expect.pass
, \() ->
(codeAtLocation == under)
|> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation })
, \() -> codeInspector.checkIfLocationIsAmbiguous error_ under
]
UnderExactly under range ->
Expect.all
[ (codeAtLocation == under)
|> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation })
|> always
, (Rule.errorRange error_ == range)
|> Expect.true (ErrorMessage.wrongLocation error_ range under)
|> always
[ \() ->
(codeAtLocation == under)
|> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation })
, \() ->
(Rule.errorRange error_ == range)
|> Expect.true (ErrorMessage.wrongLocation error_ range under)
]
Nothing ->
ErrorMessage.locationNotFound error_
|> Expect.fail
|> always
\() ->
ErrorMessage.locationNotFound error_
|> Expect.fail
checkDetailsAreCorrect : Error -> ExpectedError -> (() -> Expectation)

View File

@ -1,7 +1,7 @@
module Review.Test.ErrorMessage exposing
( ExpectedErrorData
, parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors
, underMismatch, expectedMoreErrors, tooManyErrors, locationNotFound, locationIsAmbiguousInSourceCode
, underMismatch, expectedMoreErrors, tooManyErrors, locationNotFound, underMayNotBeEmpty, locationIsAmbiguousInSourceCode
, needToUsedExpectErrorsForModules, duplicateModuleName, unknownModulesInExpectedErrors
, missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges
)
@ -13,7 +13,7 @@ module Review.Test.ErrorMessage exposing
@docs ExpectedErrorData
@docs parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors
@docs underMismatch, expectedMoreErrors, tooManyErrors, locationNotFound, locationIsAmbiguousInSourceCode
@docs underMismatch, expectedMoreErrors, tooManyErrors, locationNotFound, underMayNotBeEmpty, locationIsAmbiguousInSourceCode
@docs needToUsedExpectErrorsForModules, duplicateModuleName, unknownModulesInExpectedErrors
@docs missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges
@ -232,6 +232,23 @@ Please try to have the error under the smallest region that makes sense.
This will be the most helpful for the person who reads the error message."""
underMayNotBeEmpty : { message : String, codeAtLocation : String } -> String
underMayNotBeEmpty { message, codeAtLocation } =
"""COULD NOT FIND LOCATION FOR ERROR
I was looking for the error with the following message:
""" ++ wrapInQuotes message ++ """
and I found it, but the expected error has an empty string for `under`. I
need to point somewhere, so as to best help the people who encounter this
error.
If this helps, this is where I found the error:
""" ++ formatSourceCode codeAtLocation
locationIsAmbiguousInSourceCode : SourceCode -> Error -> String -> List Int -> String
locationIsAmbiguousInSourceCode sourceCode error under occurrencesInSourceCode =
"""AMBIGUOUS ERROR LOCATION

View File

@ -18,6 +18,7 @@ all =
, unexpectedDetailsTest
, emptyDetailsTest
, wrongLocationTest
, underMayNotBeEmptyTest
, locationNotFoundTest
, expectedMoreErrorsTest
, tooManyErrorsTest
@ -458,6 +459,31 @@ This will be the most helpful for the person who reads the error message.
"""
underMayNotBeEmptyTest : Test
underMayNotBeEmptyTest =
test "underMayNotBeEmpty" <|
\() ->
ErrorMessage.underMayNotBeEmpty
{ message = "Some error"
, codeAtLocation = "abcd = 1"
}
|> expectMessageEqual """
COULD NOT FIND LOCATION FOR ERROR
I was looking for the error with the following message:
`Some error`
and I found it, but the expected error has an empty string for `under`. I
need to point somewhere, so as to best help the people who encounter this
error.
If this helps, this is where I found the error:
`abcd = 1`
"""
expectedMoreErrorsTest : Test
expectedMoreErrorsTest =
test "expectedMoreErrors" <|