Skip to content

Commit

Permalink
Combine checkFixesMatch and checkGlobalErrorFixesMatch into one
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Jan 22, 2025
1 parent 1e7b0e5 commit df88044
Showing 1 changed file with 12 additions and 98 deletions.
110 changes: 12 additions & 98 deletions src/Review/Test.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ checkGlobalErrorsMatch project originalNumberOfExpectedErrors params =
Expect.fail (FailureMessage.emptyDetails head.message)

else
case checkGlobalErrorFixesMatch project matchedError head.fixes (ErrorFixes.toList fixes) of
case checkFixesMatch project FailureMessage.Global matchedError head.fixes (ErrorFixes.toList fixes) of
Err failure ->
Expect.fail failure

Expand Down Expand Up @@ -1920,7 +1920,7 @@ checkFixesAreCorrect project moduleName ((ReviewError err) as error_) expectedEr
ComesFromWhenFixed fixedSource ->
checkFixesMatch
project
moduleName
(FailureMessage.Module moduleName)
error_
(Dict.singleton err.filePath (ExpectEdited fixedSource))
errorFixes
Expand All @@ -1929,7 +1929,7 @@ checkFixesAreCorrect project moduleName ((ReviewError err) as error_) expectedEr
ComesFromShouldFixFiles expectedFixes ->
checkFixesMatch
project
moduleName
(FailureMessage.Module moduleName)
error_
expectedFixes
errorFixes
Expand All @@ -1946,16 +1946,16 @@ resultToFailure result =
Expect.fail failure


checkFixesMatch : ProjectInternals -> String -> ReviewError -> Dict String ExpectedFix -> List ( FileTarget, ErrorFixes.FixKind ) -> Result String ()
checkFixesMatch project moduleName error_ expectedFixed fixes =
checkFixesMatch : ProjectInternals -> FailureMessage.Target -> ReviewError -> Dict String ExpectedFix -> List ( FileTarget, ErrorFixes.FixKind ) -> Result String ()
checkFixesMatch project target error_ expectedFixed fixes =
case fixes of
[] ->
if Dict.isEmpty expectedFixed then
Ok ()

else
FailureMessage.missingFixes
{ target = FailureMessage.Module moduleName
{ target = target
, message = Rule.errorMessage error_
, expectedFixedModules = Dict.keys expectedFixed
}
Expand All @@ -1973,14 +1973,14 @@ checkFixesMatch project moduleName error_ expectedFixed fixes =
Ok () ->
checkFixesMatch
project
moduleName
target
error_
(Dict.remove key expectedFixed)
rest

Just ( key, ExpectRemoved ) ->
FailureMessage.fileWasEditedInsteadOfRemoved
{ target = FailureMessage.Module moduleName
{ target = target
, message = Rule.errorMessage error_
, nameOfFixedFile = key
, fixedSource = targetInformation.source
Expand All @@ -1989,7 +1989,7 @@ checkFixesMatch project moduleName error_ expectedFixed fixes =

Nothing ->
FailureMessage.unexpectedAdditionalFixes
{ target = FailureMessage.Module moduleName
{ target = target
, message = Rule.errorMessage error_
, nameOfFixedFile = FileTarget.filePath fixTarget
, fixedSource = targetInformation.source
Expand All @@ -2007,22 +2007,22 @@ checkFixesMatch project moduleName error_ expectedFixed fixes =
Just ( key, ExpectRemoved ) ->
checkFixesMatch
project
moduleName
target
error_
(Dict.remove key expectedFixed)
rest

Just ( key, ExpectEdited _ ) ->
FailureMessage.fileWasRemovedInsteadOfEdited
{ target = FailureMessage.Module moduleName
{ target = target
, message = Rule.errorMessage error_
, nameOfFixedFile = key
}
|> Err

Nothing ->
FailureMessage.unexpectedAdditionalFixes
{ target = FailureMessage.Module moduleName
{ target = target
, message = Rule.errorMessage error_
, nameOfFixedFile = FileTarget.filePath fixTarget
, fixedSource = targetInformation.source
Expand All @@ -2034,92 +2034,6 @@ checkFixesMatch project moduleName error_ expectedFixed fixes =
|> Err


checkGlobalErrorFixesMatch : ProjectInternals -> ReviewError -> Dict String ExpectedFix -> List ( FileTarget, ErrorFixes.FixKind ) -> Result String ()
checkGlobalErrorFixesMatch project error_ expectedFixed fixes =
case fixes of
[] ->
if Dict.isEmpty expectedFixed then
Ok ()

else
FailureMessage.missingFixes
{ target = FailureMessage.Global
, message = Rule.errorMessage error_
, expectedFixedModules = Dict.keys expectedFixed
}
|> Err

( target, ErrorFixes.Edit fileFixes ) :: rest ->
case getTargetFileFromProject target project of
Just targetInformation ->
case getExpectedFixedCodeThroughFilePathOrModuleName (FileTarget.filePath target) targetInformation.moduleName expectedFixed of
Just ( key, ExpectEdited expectedFix ) ->
case fixOneError target fileFixes targetInformation.source expectedFix error_ of
Err failureMessage ->
Err failureMessage

Ok () ->
checkGlobalErrorFixesMatch
project
error_
(Dict.remove key expectedFixed)
rest

Just ( key, ExpectRemoved ) ->
FailureMessage.fileWasEditedInsteadOfRemoved
{ target = FailureMessage.Global
, message = Rule.errorMessage error_
, nameOfFixedFile = key
, fixedSource = targetInformation.source
}
|> Err

Nothing ->
FailureMessage.unexpectedAdditionalFixes
{ target = FailureMessage.Global
, message = Rule.errorMessage error_
, nameOfFixedFile = FileTarget.filePath target
, fixedSource = targetInformation.source
}
|> Err

Nothing ->
FailureMessage.fixForUnknownFile (FileTarget.filePath target)
|> Err

( target, ErrorFixes.Remove ) :: rest ->
case getTargetFileFromProject target project of
Just targetInformation ->
case getExpectedFixedCodeThroughFilePathOrModuleName (FileTarget.filePath target) targetInformation.moduleName expectedFixed of
Just ( key, ExpectRemoved ) ->
checkGlobalErrorFixesMatch
project
error_
(Dict.remove key expectedFixed)
rest

Just ( key, ExpectEdited _ ) ->
FailureMessage.fileWasRemovedInsteadOfEdited
{ target = FailureMessage.Global
, message = Rule.errorMessage error_
, nameOfFixedFile = key
}
|> Err

Nothing ->
FailureMessage.unexpectedAdditionalFixes
{ target = FailureMessage.Global
, message = Rule.errorMessage error_
, nameOfFixedFile = FileTarget.filePath target
, fixedSource = targetInformation.source
}
|> Err

Nothing ->
FailureMessage.fixForUnknownFile (FileTarget.filePath target)
|> Err


getExpectedFixedCodeThroughFilePathOrModuleName : String -> Maybe String -> Dict String ExpectedFix -> Maybe ( String, ExpectedFix )
getExpectedFixedCodeThroughFilePathOrModuleName filePath moduleName expectedFixed =
case Dict.get filePath expectedFixed of
Expand Down

0 comments on commit df88044

Please sign in to comment.