2019-11-23 01:52:51 +03:00
|
|
|
module Review.RuleVisitorsOrderTest exposing (all)
|
|
|
|
|
|
|
|
import Elm.Syntax.Declaration exposing (Declaration)
|
|
|
|
import Elm.Syntax.Expression exposing (Expression)
|
|
|
|
import Elm.Syntax.Node exposing (Node)
|
2020-03-25 20:02:37 +03:00
|
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
2020-02-15 02:02:34 +03:00
|
|
|
import Review.Test
|
2019-11-23 01:52:51 +03:00
|
|
|
import Test exposing (Test, test)
|
|
|
|
|
|
|
|
|
|
|
|
type alias Context =
|
|
|
|
String
|
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
|
|
|
Test.describe "Visitor order"
|
2019-11-23 02:51:03 +03:00
|
|
|
[ test "should call visitors in a given order" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-02-14 20:20:45 +03:00
|
|
|
Rule.newModuleRuleSchema "TestRule" "\n0 - initial context"
|
2020-03-03 00:51:25 +03:00
|
|
|
|> Rule.withElmJsonModuleVisitor (\_ context -> context ++ "\n1 - withElmJsonModuleVisitor")
|
2020-03-03 00:53:37 +03:00
|
|
|
|> Rule.withDependenciesModuleVisitor (\_ context -> context ++ "\n2 - withDependenciesModuleVisitor")
|
2019-11-23 02:51:03 +03:00
|
|
|
|> Rule.withModuleDefinitionVisitor (\_ context -> ( [], context ++ "\n3 - withModuleDefinitionVisitor" ))
|
|
|
|
|> Rule.withImportVisitor (\_ context -> ( [], context ++ "\n4 - withImportVisitor" ))
|
|
|
|
|> Rule.withDeclarationListVisitor (\_ context -> ( [], context ++ "\n5 - withDeclarationListVisitor" ))
|
|
|
|
|> Rule.withDeclarationVisitor
|
|
|
|
(\node direction context ->
|
|
|
|
case direction of
|
|
|
|
Rule.OnEnter ->
|
|
|
|
( [], context ++ "\n6 - withDeclarationVisitor (Enter)" )
|
|
|
|
|
|
|
|
Rule.OnExit ->
|
|
|
|
( [], context ++ "\n9 - withDeclarationVisitor (Exit)" )
|
|
|
|
)
|
|
|
|
|> Rule.withExpressionVisitor
|
|
|
|
(\node direction context ->
|
|
|
|
case direction of
|
|
|
|
Rule.OnEnter ->
|
|
|
|
( [], context ++ "\n7 - withExpressionVisitor (Enter)" )
|
|
|
|
|
|
|
|
Rule.OnExit ->
|
|
|
|
( [], context ++ "\n8 - withExpressionVisitor (Exit)" )
|
|
|
|
)
|
2020-01-26 15:28:57 +03:00
|
|
|
|> Rule.withFinalModuleEvaluation finalEvaluation
|
2020-01-19 22:37:19 +03:00
|
|
|
|> Rule.fromModuleRuleSchema
|
2019-11-23 02:51:03 +03:00
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
finalEvaluation : Context -> List (Error {})
|
2019-11-23 02:51:03 +03:00
|
|
|
finalEvaluation context =
|
|
|
|
[ Rule.error { message = context, details = [ "details" ] }
|
|
|
|
{ start = { row = 1, column = 1 }
|
|
|
|
, end = { row = 1, column = 7 }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
in
|
2020-02-14 20:20:45 +03:00
|
|
|
"""module A exposing (..)
|
|
|
|
import B
|
2019-11-23 02:51:03 +03:00
|
|
|
a = 1
|
|
|
|
"""
|
2020-02-14 20:20:45 +03:00
|
|
|
|> Review.Test.run rule
|
2019-11-23 02:51:03 +03:00
|
|
|
|> Review.Test.expectErrors
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = """
|
2020-02-14 20:20:45 +03:00
|
|
|
0 - initial context
|
2020-03-03 00:51:25 +03:00
|
|
|
1 - withElmJsonModuleVisitor
|
2020-03-03 00:53:37 +03:00
|
|
|
2 - withDependenciesModuleVisitor
|
2019-11-23 02:51:03 +03:00
|
|
|
3 - withModuleDefinitionVisitor
|
|
|
|
4 - withImportVisitor
|
|
|
|
5 - withDeclarationListVisitor
|
|
|
|
6 - withDeclarationVisitor (Enter)
|
|
|
|
7 - withExpressionVisitor (Enter)
|
|
|
|
8 - withExpressionVisitor (Exit)
|
|
|
|
9 - withDeclarationVisitor (Exit)"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, test "should call the same type of visitors in order of call on enter, and reverse order on exit (expression)" <|
|
2019-11-23 01:52:51 +03:00
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-01-19 22:37:19 +03:00
|
|
|
Rule.newModuleRuleSchema "TestRule" ""
|
2019-11-23 01:52:51 +03:00
|
|
|
|> Rule.withExpressionVisitor (declarationVisitor "A")
|
|
|
|
|> Rule.withExpressionVisitor (declarationVisitor "B")
|
|
|
|
|> Rule.withExpressionVisitor (declarationVisitor "C")
|
2020-01-26 15:28:57 +03:00
|
|
|
|> Rule.withFinalModuleEvaluation finalEvaluation
|
2020-01-19 22:37:19 +03:00
|
|
|
|> Rule.fromModuleRuleSchema
|
2019-11-23 01:52:51 +03:00
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
declarationVisitor : String -> Node Expression -> Rule.Direction -> Context -> ( List (Error {}), Context )
|
2019-11-23 01:52:51 +03:00
|
|
|
declarationVisitor text node direction context =
|
|
|
|
case direction of
|
|
|
|
Rule.OnEnter ->
|
|
|
|
( [], context ++ "\nEnter " ++ text )
|
|
|
|
|
|
|
|
Rule.OnExit ->
|
|
|
|
( [], context ++ "\nExit " ++ text )
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
finalEvaluation : Context -> List (Error {})
|
2019-11-23 01:52:51 +03:00
|
|
|
finalEvaluation context =
|
|
|
|
[ Rule.error { message = context, details = [ "details" ] }
|
|
|
|
{ start = { row = 1, column = 1 }
|
|
|
|
, end = { row = 1, column = 7 }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
in
|
2020-02-14 20:20:45 +03:00
|
|
|
Review.Test.run rule """module A exposing (..)
|
2019-11-23 01:52:51 +03:00
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|> Review.Test.expectErrors
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = """
|
|
|
|
Enter A
|
|
|
|
Enter B
|
|
|
|
Enter C
|
|
|
|
Exit C
|
|
|
|
Exit B
|
|
|
|
Exit A"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
, test "should call the same type of visitors in order of call on enter, and reverse order on exit (declaration)" <|
|
|
|
|
\() ->
|
|
|
|
let
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-01-19 22:37:19 +03:00
|
|
|
Rule.newModuleRuleSchema "TestRule" ""
|
2019-11-23 01:52:51 +03:00
|
|
|
|> Rule.withDeclarationVisitor (declarationVisitor "A")
|
|
|
|
|> Rule.withDeclarationVisitor (declarationVisitor "B")
|
|
|
|
|> Rule.withDeclarationVisitor (declarationVisitor "C")
|
2020-01-26 15:28:57 +03:00
|
|
|
|> Rule.withFinalModuleEvaluation finalEvaluation
|
2020-01-19 22:37:19 +03:00
|
|
|
|> Rule.fromModuleRuleSchema
|
2019-11-23 01:52:51 +03:00
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
declarationVisitor : String -> Node Declaration -> Rule.Direction -> Context -> ( List (Error {}), Context )
|
2019-11-23 01:52:51 +03:00
|
|
|
declarationVisitor text node direction context =
|
|
|
|
case direction of
|
|
|
|
Rule.OnEnter ->
|
|
|
|
( [], context ++ "\nEnter " ++ text )
|
|
|
|
|
|
|
|
Rule.OnExit ->
|
|
|
|
( [], context ++ "\nExit " ++ text )
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
finalEvaluation : Context -> List (Error {})
|
2019-11-23 01:52:51 +03:00
|
|
|
finalEvaluation context =
|
|
|
|
[ Rule.error { message = context, details = [ "details" ] }
|
|
|
|
{ start = { row = 1, column = 1 }
|
|
|
|
, end = { row = 1, column = 7 }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
in
|
2020-02-14 20:20:45 +03:00
|
|
|
Review.Test.run rule """module A exposing (..)
|
2019-11-23 01:52:51 +03:00
|
|
|
a = 1
|
|
|
|
"""
|
|
|
|
|> Review.Test.expectErrors
|
|
|
|
[ Review.Test.error
|
|
|
|
{ message = """
|
|
|
|
Enter A
|
|
|
|
Enter B
|
|
|
|
Enter C
|
|
|
|
Exit C
|
|
|
|
Exit B
|
|
|
|
Exit A"""
|
|
|
|
, details = [ "details" ]
|
|
|
|
, under = "module"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|