Replace expectErrorsForFiles by expectErrorsForModules

This commit is contained in:
Jeroen Engels 2020-01-05 18:44:16 +01:00
parent c388770501
commit 3bd94e4127
5 changed files with 113 additions and 58 deletions

View File

@ -1,4 +1,4 @@
module ListExtra exposing (last, uniquePairs) module ListExtra exposing (find, last, uniquePairs)
{-| Functions taken from elm-community/list-extra. {-| Functions taken from elm-community/list-extra.
@ -8,7 +8,7 @@ elm-community/list-extra would release a new major version.
# List functions # List functions
@docs last, uniquePairs @docs find, last, uniquePairs
# Original Copyright notice # Original Copyright notice
@ -38,6 +38,24 @@ SOFTWARE.
-} -}
{-| Find the first element that satisfies a predicate and return
Just that element. If none match, return Nothing.
find (\\num -> num > 5) [2, 4, 6, 8] == Just 6
-}
find : (a -> Bool) -> List a -> Maybe a
find predicate list =
case list of
[] ->
Nothing
first :: rest ->
if predicate first then
Just first
else
find predicate rest
last : List a -> Maybe a last : List a -> Maybe a
last items = last items =
case items of case items of

View File

@ -1,6 +1,6 @@
module Review.Test exposing module Review.Test exposing
( ReviewResult, run, runWithProjectData, runMulti, runMultiWithProjectData ( ReviewResult, run, runWithProjectData, runMulti, runMultiWithProjectData
, ExpectedError, expectErrors, expectErrorsForFiles, expectNoErrors, error, atExactly, whenFixed , ExpectedError, expectErrors, expectErrorsForModules, expectNoErrors, error, atExactly, whenFixed
) )
{-| Module that helps you test your rules, using [`elm-test`](https://package.elm-lang.org/packages/elm-explorations/test/latest/). {-| Module that helps you test your rules, using [`elm-test`](https://package.elm-lang.org/packages/elm-explorations/test/latest/).
@ -100,7 +100,7 @@ for this module.
# Making assertions # Making assertions
@docs ExpectedError, expectErrors, expectErrorsForFiles, expectNoErrors, error, atExactly, whenFixed @docs ExpectedError, expectErrors, expectErrorsForModules, expectNoErrors, error, atExactly, whenFixed
-} -}
@ -109,6 +109,7 @@ import Elm.Syntax.Module as Module
import Elm.Syntax.Node as Node import Elm.Syntax.Node as Node
import Elm.Syntax.Range exposing (Range) import Elm.Syntax.Range exposing (Range)
import Expect exposing (Expectation) import Expect exposing (Expectation)
import ListExtra
import Review import Review
import Review.File as File import Review.File as File
import Review.Fix as Fix import Review.Fix as Fix
@ -126,7 +127,13 @@ import Set exposing (Set)
-} -}
type ReviewResult type ReviewResult
= FailedRun String = FailedRun String
| SuccessfulRun (List { inspector : CodeInspector, errors : List Error }) | SuccessfulRun
(List
{ moduleName : String
, inspector : CodeInspector
, errors : List Error
}
)
type alias CodeInspector = type alias CodeInspector =
@ -261,7 +268,12 @@ runMultiWithProjectData project rule sources =
in in
List.map List.map
(\parsedFile -> (\parsedFile ->
{ inspector = codeInspectorForSource parsedFile { moduleName =
parsedFile.ast.moduleDefinition
|> Node.value
|> Module.moduleName
|> String.join "."
, inspector = codeInspectorForSource parsedFile
, errors = , errors =
errors errors
|> List.filter (\error_ -> Rule.errorFilePath error_ == parsedFile.path) |> List.filter (\error_ -> Rule.errorFilePath error_ == parsedFile.path)
@ -438,28 +450,39 @@ an error at the end of the source code.
-} -}
expectErrors : List ExpectedError -> ReviewResult -> Expectation expectErrors : List ExpectedError -> ReviewResult -> Expectation
expectErrors expectedErrors reviewResult = expectErrors expectedErrors reviewResult =
expectErrorsForFiles [ expectedErrors ] reviewResult case reviewResult of
FailedRun errorMessage ->
Expect.fail errorMessage
SuccessfulRun ({ inspector, errors, moduleName } :: []) ->
checkAllErrorsMatch inspector expectedErrors errors
SuccessfulRun _ ->
Expect.fail ErrorMessage.needToUsedExpectErrorsForModules
{-| TODO Documentation {-| TODO Documentation
-} -}
expectErrorsForFiles : List (List ExpectedError) -> ReviewResult -> Expectation expectErrorsForModules : List ( String, List ExpectedError ) -> ReviewResult -> Expectation
expectErrorsForFiles expectedErrorsList reviewResult = expectErrorsForModules expectedErrorsList reviewResult =
case reviewResult of case reviewResult of
FailedRun errorMessage -> FailedRun errorMessage ->
Expect.fail errorMessage Expect.fail errorMessage
SuccessfulRun runResults -> SuccessfulRun runResults ->
if List.length runResults /= List.length expectedErrorsList then
Expect.fail <| ErrorMessage.errorListLengthMismatch (List.length runResults) (List.length expectedErrorsList)
else
List.map2
(\{ inspector, errors } expectedErrors () ->
checkAllErrorsMatch inspector expectedErrors errors
)
runResults runResults
|> List.map
(\{ inspector, errors, moduleName } ->
let
expectedErrors : List ExpectedError
expectedErrors =
expectedErrorsList expectedErrorsList
|> ListExtra.find (\( moduleName_, _ ) -> moduleName_ == moduleName)
|> Maybe.map Tuple.second
|> Maybe.withDefault []
in
\() -> checkAllErrorsMatch inspector expectedErrors errors
)
|> (\expectations -> Expect.all expectations ()) |> (\expectations -> Expect.all expectations ())

View File

@ -2,7 +2,7 @@ module Review.Test.ErrorMessage exposing
( ExpectedErrorData ( ExpectedErrorData
, parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors , parsingFailure, messageMismatch, emptyDetails, unexpectedDetails, wrongLocation, didNotExpectErrors
, underMismatch, expectedMoreErrors, tooManyErrors, locationIsAmbiguousInSourceCode , underMismatch, expectedMoreErrors, tooManyErrors, locationIsAmbiguousInSourceCode
, errorListLengthMismatch, duplicateModuleName , needToUsedExpectErrorsForModules, duplicateModuleName
, missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges , missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges
, impossibleState , impossibleState
) )
@ -15,7 +15,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, locationIsAmbiguousInSourceCode @docs underMismatch, expectedMoreErrors, tooManyErrors, locationIsAmbiguousInSourceCode
@docs errorListLengthMismatch, duplicateModuleName @docs needToUsedExpectErrorsForModules, duplicateModuleName
@docs missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges @docs missingFixes, unexpectedFixes, fixedCodeMismatch, unchangedSourceAfterFix, invalidSourceAfterFix, hasCollisionsInFixRanges
@docs impossibleState @docs impossibleState
@ -239,26 +239,27 @@ Tip: I found them at:
""" ++ listOccurrencesAsLocations sourceCode under occurrencesInSourceCode """ ++ listOccurrencesAsLocations sourceCode under occurrencesInSourceCode
errorListLengthMismatch : Int -> Int -> String needToUsedExpectErrorsForModules : String
errorListLengthMismatch expectedListLength actualListLength = needToUsedExpectErrorsForModules =
"""MISMATCH BETWEEN NUMBER OF MODULES AND NUMBER OF LISTS OF ERRORS """AMBIGUOUS MODULE FOR ERROR
You passed a list of """ ++ String.fromInt expectedListLength ++ """ modules to this test, but a list of """ ++ String.fromInt actualListLength ++ """ lists You gave me several modules, and you expect some errors. I need to know for
of errors. which module you expect these errors to be reported.
I expect each item in the list of expected errors to correspond to the You should use `expectErrorsForModules` to do this:
module at the same position in the module list. Since the two lists have
different sizes, I'm not sure how to associate the last modules or errors.
If you expect no errors to be reported for a module, use an empty list:
test "..." <| test "..." <|
\\() -> \\() ->
[ sourceCode1, sourceCode2 ] [ \"\"\"
module A exposing (..)
-- someCode
\"\"\", \"\"\"
module B exposing (..)
-- someCode
\"\"\" ]
|> Review.Test.runMulti rule |> Review.Test.runMulti rule
|> Review.Test.expectErrorsForFiles |> Review.Test.expectErrorsForModules
[ [] -- Expect no errors reported in `sourceCode1` [ ( "B", [ Review.Test.error someError ] )
, [ Review.Test.error theErrorForSourceCode2 ]
]""" ]"""

View File

@ -21,7 +21,7 @@ all =
, expectedMoreErrorsTest , expectedMoreErrorsTest
, tooManyErrorsTest , tooManyErrorsTest
, locationIsAmbiguousInSourceCodeTest , locationIsAmbiguousInSourceCodeTest
, errorListLengthMismatchTest , needToUsedExpectErrorsForModulesTest
, duplicateModuleNameTest , duplicateModuleNameTest
, missingFixesTest , missingFixesTest
, unexpectedFixesTest , unexpectedFixesTest
@ -607,32 +607,32 @@ Tip: I found them at:
] ]
errorListLengthMismatchTest : Test needToUsedExpectErrorsForModulesTest : Test
errorListLengthMismatchTest = needToUsedExpectErrorsForModulesTest =
test "errorListLengthMismatch" <| test "needToUsedExpectErrorsForModules" <|
\() -> \() ->
ErrorMessage.errorListLengthMismatch 1417 1418 ErrorMessage.needToUsedExpectErrorsForModules
|> expectMessageEqual """ |> expectMessageEqual """
MISMATCH BETWEEN NUMBER OF MODULES AND NUMBER OF LISTS OF ERRORS AMBIGUOUS MODULE FOR ERROR
You passed a list of 1417 modules to this test, but a list of 1418 lists You gave me several modules, and you expect some errors. I need to know for
of errors. which module you expect these errors to be reported.
I expect each item in the list of expected errors to correspond to the You should use `expectErrorsForModules` to do this:
module at the same position in the module list. Since the two lists have
different sizes, I'm not sure how to associate the last modules or errors.
If you expect no errors to be reported for a module, use an empty list:
test "..." <| test "..." <|
\\() -> \\() ->
[ sourceCode1, sourceCode2 ] [ \"\"\"
module A exposing (..)
-- someCode
\"\"\", \"\"\"
module B exposing (..)
-- someCode
\"\"\" ]
|> Review.Test.runMulti rule |> Review.Test.runMulti rule
|> Review.Test.expectErrorsForFiles |> Review.Test.expectErrorsForModules
[ [] -- Expect no errors reported in `sourceCode1` [ ( "B", [ Review.Test.error someError ] )
, [ Review.Test.error theErrorForSourceCode2 ] ]"""
]
"""
duplicateModuleNameTest : Test duplicateModuleNameTest : Test

View File

@ -89,17 +89,30 @@ main = text ""
, """ , """
module Reported exposing (..) module Reported exposing (..)
a = 1 a = 1
"""
, """
module Other.Reported exposing (..)
a = 1
""" """
] ]
|> Review.Test.runMultiWithProjectData application rule |> Review.Test.runMultiWithProjectData application rule
|> Review.Test.expectErrorsForFiles |> Review.Test.expectErrorsForModules
[ [] [ ( "Reported"
, [ Review.Test.error , [ Review.Test.error
{ message = "Module `Reported` is never used." { message = "Module `Reported` is never used."
, details = details , details = details
, under = "Reported" , under = "Reported"
} }
] ]
)
, ( "Other.Reported"
, [ Review.Test.error
{ message = "Module `Other.Reported` is never used."
, details = details
, under = "Other.Reported"
}
]
)
] ]
, test "should report a module even if it is the only module in the project" <| , test "should report a module even if it is the only module in the project" <|
\() -> \() ->