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 -> Error -> (() -> Expectation)
checkErrorMatch codeInspector ((ExpectedError expectedError_) as expectedError) error_ = checkErrorMatch codeInspector ((ExpectedError expectedError_) as expectedError) error_ =
Expect.all Expect.all
[ (expectedError_.message == Rule.errorMessage error_) [ \() ->
(expectedError_.message == Rule.errorMessage error_)
|> Expect.true |> Expect.true
(ErrorMessage.messageMismatch (ErrorMessage.messageMismatch
(extractExpectedErrorData expectedError) (extractExpectedErrorData expectedError)
error_ error_
) )
|> always
, checkMessageAppearsUnder codeInspector error_ expectedError , checkMessageAppearsUnder codeInspector error_ expectedError
, checkDetailsAreCorrect 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 case expectedError.under of
Under under -> Under under ->
Expect.all Expect.all
[ (codeAtLocation == under) [ \() ->
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 }) |> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation })
|> always , \() -> codeInspector.checkIfLocationIsAmbiguous error_ under
, codeInspector.checkIfLocationIsAmbiguous error_ under
|> always
] ]
UnderExactly under range -> UnderExactly under range ->
Expect.all Expect.all
[ (codeAtLocation == under) [ \() ->
(codeAtLocation == under)
|> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation }) |> Expect.true (ErrorMessage.underMismatch error_ { under = under, codeAtLocation = codeAtLocation })
|> always , \() ->
, (Rule.errorRange error_ == range) (Rule.errorRange error_ == range)
|> Expect.true (ErrorMessage.wrongLocation error_ range under) |> Expect.true (ErrorMessage.wrongLocation error_ range under)
|> always
] ]
Nothing -> Nothing ->
\() ->
ErrorMessage.locationNotFound error_ ErrorMessage.locationNotFound error_
|> Expect.fail |> Expect.fail
|> always
checkDetailsAreCorrect : Error -> ExpectedError -> (() -> Expectation) checkDetailsAreCorrect : Error -> ExpectedError -> (() -> Expectation)

View File

@ -1,7 +1,7 @@
module Review.Test.ErrorMessage exposing module Review.Test.ErrorMessage exposing
( ExpectedErrorData ( ExpectedErrorData
, parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors , parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors
, underMismatch, expectedMoreErrors, tooManyErrors, locationNotFound, locationIsAmbiguousInSourceCode , underMismatch, expectedMoreErrors, tooManyErrors, locationNotFound, underMayNotBeEmpty, locationIsAmbiguousInSourceCode
, needToUsedExpectErrorsForModules, duplicateModuleName, unknownModulesInExpectedErrors , needToUsedExpectErrorsForModules, duplicateModuleName, unknownModulesInExpectedErrors
, missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges , missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges
) )
@ -13,7 +13,7 @@ module Review.Test.ErrorMessage exposing
@docs ExpectedErrorData @docs ExpectedErrorData
@docs parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors @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 needToUsedExpectErrorsForModules, duplicateModuleName, unknownModulesInExpectedErrors
@docs missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges @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.""" 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 -> String -> List Int -> String
locationIsAmbiguousInSourceCode sourceCode error under occurrencesInSourceCode = locationIsAmbiguousInSourceCode sourceCode error under occurrencesInSourceCode =
"""AMBIGUOUS ERROR LOCATION """AMBIGUOUS ERROR LOCATION

View File

@ -18,6 +18,7 @@ all =
, unexpectedDetailsTest , unexpectedDetailsTest
, emptyDetailsTest , emptyDetailsTest
, wrongLocationTest , wrongLocationTest
, underMayNotBeEmptyTest
, locationNotFoundTest , locationNotFoundTest
, expectedMoreErrorsTest , expectedMoreErrorsTest
, tooManyErrorsTest , 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
expectedMoreErrorsTest = expectedMoreErrorsTest =
test "expectedMoreErrors" <| test "expectedMoreErrors" <|