Fix Review.Test not working correctly with multilines

This commit is contained in:
Jeroen Engels 2020-05-14 18:24:08 +02:00
parent d2c37dd489
commit d0bd0579f3
4 changed files with 76 additions and 2 deletions

View File

@ -911,7 +911,7 @@ getCodeAtLocationInSourceCode sourceCode =
lastLine : Maybe String
lastLine =
Array.get (end.row - 1) lines
|> Maybe.map (String.dropRight end.column)
|> Maybe.map (String.left end.column)
in
[ [ firstLine ]
, Array.slice start.row (end.row - 1) lines

View File

@ -0,0 +1,43 @@
module MiscRules.NoBooleanCase exposing (rule)
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Pattern as Pattern
import Elm.Syntax.Range exposing (Range)
import Review.Rule as Rule exposing (Error, Rule)
rule : Rule
rule =
Rule.newModuleRuleSchema "NoBooleanCase" ()
|> Rule.withSimpleExpressionVisitor expressionVisitor
|> Rule.fromModuleRuleSchema
expressionVisitor : Node Expression -> List (Error {})
expressionVisitor (Node.Node range expression) =
case expression of
Expression.CaseExpression caseBlock ->
errorsForCaseBlock range caseBlock
_ ->
[]
errorsForCaseBlock : Range -> Expression.CaseBlock -> List (Error {})
errorsForCaseBlock ({ end } as range) { expression, cases } =
case cases of
[ ( Node.Node _ (Pattern.NamedPattern { moduleName, name } []), _ ), _ ] ->
if moduleName == [] && (name == "True" || name == "False") then
[ Rule.error
{ message = "Matching boolean values in a case .. of expression"
, details = [ "It's quite silly" ]
}
range
]
else
[]
_ ->
[]

View File

@ -0,0 +1,31 @@
module MiscRules.NoBooleanCaseTest exposing (all)
import MiscRules.NoBooleanCase as NoBooleanCase
import Review.Test
import Test exposing (Test, describe, test)
all : Test
all =
describe "noBooleanCase"
[ test "Simple one" <|
\_ ->
"""module A exposing (..)
a =
case expr of
True -> True
False -> False
"""
|> Review.Test.run NoBooleanCase.rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Matching boolean values in a case .. of expression"
, details =
[ "It's quite silly"
]
, under = """case expr of
True -> True
False -> False"""
}
]
]

View File

@ -555,7 +555,7 @@ import Foo
[ Review.Test.error
{ message = "Imported type `C` is not used"
, details = details
, under = "C\n "
, under = "C\n ,"
}
|> Review.Test.whenFixed """module SomeModule exposing (d)
import Foo