From a2a5831e0bb3ae0b3276d0539e821b44f863026f Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Wed, 15 Jan 2020 16:16:40 +0100 Subject: [PATCH] Explain in which module we are getting an unexpected test result --- src/Review/Test.elm | 57 ++++++++++++++++---------------- src/Review/Test/ErrorMessage.elm | 18 +++++----- tests/ErrorMessageTest.elm | 16 ++++----- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/Review/Test.elm b/src/Review/Test.elm index 964a2195..a774747b 100644 --- a/src/Review/Test.elm +++ b/src/Review/Test.elm @@ -126,13 +126,14 @@ import Set exposing (Set) -} type ReviewResult = FailedRun String - | SuccessfulRun - (List - { moduleName : String - , inspector : CodeInspector - , errors : List Error - } - ) + | SuccessfulRun (List SuccessfulRunResult) + + +type alias SuccessfulRunResult = + { moduleName : String + , inspector : CodeInspector + , errors : List Error + } type alias CodeInspector = @@ -500,13 +501,13 @@ expectNoErrors reviewResult = Expect.fail errorMessage SuccessfulRun runResults -> - let - errors : List Error - errors = - List.concatMap .errors runResults - in - List.isEmpty errors - |> Expect.true (ErrorMessage.didNotExpectErrors errors) + runResults + |> List.map + (\{ errors, moduleName } () -> + List.isEmpty errors + |> Expect.true (ErrorMessage.didNotExpectErrors moduleName errors) + ) + |> (\expectations -> Expect.all expectations ()) {-| Assert that the rule reported some errors, by specifying which one. @@ -548,8 +549,8 @@ expectErrors expectedErrors reviewResult = FailedRun errorMessage -> Expect.fail errorMessage - SuccessfulRun ({ inspector, errors, moduleName } :: []) -> - checkAllErrorsMatch inspector expectedErrors errors + SuccessfulRun (runResult :: []) -> + checkAllErrorsMatch runResult expectedErrors SuccessfulRun _ -> Expect.fail ErrorMessage.needToUsedExpectErrorsForModules @@ -611,16 +612,16 @@ expectErrorsForModules expectedErrorsList reviewResult = -- TODO Fail if there are some unknown modules in expectedErrorsList runResults |> List.map - (\{ inspector, errors, moduleName } -> + (\runResult -> let expectedErrors : List ExpectedError expectedErrors = expectedErrorsList - |> ListExtra.find (\( moduleName_, _ ) -> moduleName_ == moduleName) + |> ListExtra.find (\( moduleName_, _ ) -> moduleName_ == runResult.moduleName) |> Maybe.map Tuple.second |> Maybe.withDefault [] in - \() -> checkAllErrorsMatch inspector expectedErrors errors + \() -> checkAllErrorsMatch runResult expectedErrors ) |> (\expectations -> Expect.all expectations ()) @@ -801,28 +802,28 @@ checkIfLocationIsAmbiguousInSourceCode sourceCode error_ under = -- RUNNING THE CHECKS -checkAllErrorsMatch : CodeInspector -> List ExpectedError -> List Error -> Expectation -checkAllErrorsMatch codeInspector expectedErrors errors = - checkErrorsMatch codeInspector expectedErrors errors +checkAllErrorsMatch : SuccessfulRunResult -> List ExpectedError -> Expectation +checkAllErrorsMatch runResult expectedErrors = + checkErrorsMatch runResult expectedErrors runResult.errors |> List.reverse |> (\expectations -> Expect.all expectations ()) -checkErrorsMatch : CodeInspector -> List ExpectedError -> List Error -> List (() -> Expectation) -checkErrorsMatch codeInspector expectedErrors errors = +checkErrorsMatch : SuccessfulRunResult -> List ExpectedError -> List Error -> List (() -> Expectation) +checkErrorsMatch runResult expectedErrors errors = case ( expectedErrors, errors ) of ( [], [] ) -> [ always Expect.pass ] ( expected :: restOfExpectedErrors, error_ :: restOfErrors ) -> - checkErrorMatch codeInspector expected error_ - :: checkErrorsMatch codeInspector restOfExpectedErrors restOfErrors + checkErrorMatch runResult.inspector expected error_ + :: checkErrorsMatch runResult restOfExpectedErrors restOfErrors ( expected :: restOfExpectedErrors, [] ) -> - [ always <| Expect.fail <| ErrorMessage.expectedMoreErrors <| List.map extractExpectedErrorData (expected :: restOfExpectedErrors) ] + [ always <| Expect.fail <| ErrorMessage.expectedMoreErrors runResult.moduleName <| List.map extractExpectedErrorData (expected :: restOfExpectedErrors) ] ( [], error_ :: restOfErrors ) -> - [ always <| Expect.fail <| ErrorMessage.tooManyErrors (error_ :: restOfErrors) ] + [ always <| Expect.fail <| ErrorMessage.tooManyErrors runResult.moduleName (error_ :: restOfErrors) ] checkErrorMatch : CodeInspector -> ExpectedError -> Error -> (() -> Expectation) diff --git a/src/Review/Test/ErrorMessage.elm b/src/Review/Test/ErrorMessage.elm index 8f8c2d68..93770c77 100644 --- a/src/Review/Test/ErrorMessage.elm +++ b/src/Review/Test/ErrorMessage.elm @@ -43,11 +43,11 @@ type alias SourceCode = -- ERROR MESSAGES -didNotExpectErrors : List Error -> String -didNotExpectErrors errors = +didNotExpectErrors : String -> List Error -> String +didNotExpectErrors moduleName errors = """DID NOT EXPECT ERRORS -I expected no errors but found: +I expected no errors for module `""" ++ moduleName ++ """` but found: """ ++ listErrorMessagesAndPositions errors @@ -187,8 +187,8 @@ but I found it at: """ ++ rangeAsString (Rule.errorRange error) -expectedMoreErrors : List ExpectedErrorData -> String -expectedMoreErrors missingExpectedErrors = +expectedMoreErrors : String -> List ExpectedErrorData -> String +expectedMoreErrors moduleName missingExpectedErrors = let numberOfErrors : Int numberOfErrors = @@ -197,15 +197,15 @@ expectedMoreErrors missingExpectedErrors = """RULE REPORTED LESS ERRORS THAN EXPECTED I expected to see """ - ++ (String.fromInt numberOfErrors ++ " more " ++ pluralizeErrors numberOfErrors ++ ":\n\n") + ++ (String.fromInt numberOfErrors ++ " more " ++ pluralizeErrors numberOfErrors ++ " for module `" ++ moduleName ++ "`:\n\n") ++ (missingExpectedErrors |> List.map expectedErrorToString |> String.join "\n" ) -tooManyErrors : List Error -> String -tooManyErrors extraErrors = +tooManyErrors : String -> List Error -> String +tooManyErrors moduleName extraErrors = let numberOfErrors : Int numberOfErrors = @@ -214,7 +214,7 @@ tooManyErrors extraErrors = """RULE REPORTED MORE ERRORS THAN EXPECTED I found """ - ++ (String.fromInt numberOfErrors ++ " " ++ pluralizeErrors numberOfErrors ++ " too many:\n\n") + ++ (String.fromInt numberOfErrors ++ " " ++ pluralizeErrors numberOfErrors ++ " too many for module `" ++ moduleName ++ "`:\n\n") ++ listErrorMessagesAndPositions extraErrors diff --git a/tests/ErrorMessageTest.elm b/tests/ErrorMessageTest.elm index 01b2a862..b0867728 100644 --- a/tests/ErrorMessageTest.elm +++ b/tests/ErrorMessageTest.elm @@ -101,11 +101,11 @@ didNotExpectErrorsTest = dummyRange ] in - ErrorMessage.didNotExpectErrors errors + ErrorMessage.didNotExpectErrors "ModuleName" errors |> expectMessageEqual """ DID NOT EXPECT ERRORS -I expected no errors but found: +I expected no errors for module `ModuleName` but found: - `Some error` at { start = { row = 2, column = 1 }, end = { row = 2, column = 5 } } @@ -443,11 +443,11 @@ expectedMoreErrorsTest = } ] in - ErrorMessage.expectedMoreErrors missingErrors + ErrorMessage.expectedMoreErrors "MyModule" missingErrors |> expectMessageEqual """ RULE REPORTED LESS ERRORS THAN EXPECTED -I expected to see 2 more errors: +I expected to see 2 more errors for module `MyModule`: - `Remove the use of `Debug` before shipping to production` - `Remove the use of `Debug` before shipping to production` @@ -469,11 +469,11 @@ tooManyErrorsTest = { start = { row = 2, column = 1 }, end = { row = 2, column = 5 } } ] in - ErrorMessage.tooManyErrors extraErrors + ErrorMessage.tooManyErrors "MyModule" extraErrors |> expectMessageEqual """ RULE REPORTED MORE ERRORS THAN EXPECTED -I found 1 error too many: +I found 1 error too many for module `MyModule`: - `Remove the use of `Debug` before shipping to production` at { start = { row = 2, column = 1 }, end = { row = 2, column = 5 } } @@ -495,11 +495,11 @@ I found 1 error too many: { start = { row = 3, column = 1 }, end = { row = 3, column = 5 } } ] in - ErrorMessage.tooManyErrors extraErrors + ErrorMessage.tooManyErrors "MyOtherModule" extraErrors |> expectMessageEqual """ RULE REPORTED MORE ERRORS THAN EXPECTED -I found 2 errors too many: +I found 2 errors too many for module `MyOtherModule`: - `Remove the use of `Debug` before shipping to production` at { start = { row = 2, column = 1 }, end = { row = 2, column = 5 } }