elm-review/tests/NoHtmlButtonTest.elm

238 lines
7.9 KiB
Elm
Raw Normal View History

module NoHtmlButtonTest exposing (all)
2019-11-23 23:43:18 +03:00
import Dependencies
import NoHtmlButton exposing (rule)
2019-11-20 13:16:37 +03:00
import Review.Project as Project exposing (Project)
import Review.Test exposing (ReviewResult)
import Test exposing (Test, describe, test)
testRule : String -> ReviewResult
testRule string =
"module A exposing (..)\n\n"
++ string
|> Review.Test.run rule
2019-11-20 13:16:37 +03:00
projectWithHtmlDependency : Project
projectWithHtmlDependency =
Project.new
2019-11-23 23:43:18 +03:00
|> Project.withDependency Dependencies.elmHtml
2019-11-20 13:16:37 +03:00
testRuleWithHtmlDependency : String -> ReviewResult
testRuleWithHtmlDependency string =
"module A exposing (..)\n\n"
++ string
|> Review.Test.runWithProjectData projectWithHtmlDependency rule
message : String
message =
"Do not use `Html.button` directly"
details : List String
details =
[ "At fruits.com, we've built a nice `Button` module that suits our needs better. Using this module instead of `Html.button` ensures we have a consistent button experience across the website."
]
tests : List Test
tests =
[ test "should not report the use of non-`Html.button` function or values" <|
\() ->
testRule """
a = bar 1
b = Foo.bar 1
c = baz
"""
|> Review.Test.expectNoErrors
, test "should report the use of `Html.button` as an expression" <|
\() ->
testRule """
import Html
a = Html.button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "Html.button"
}
]
, test "should report the use of `Html.button` even if it is not imported" <|
\() ->
testRule """
a = Html.button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "Html.button"
}
]
, test "should not report the use of `H.button` when H is not an alias for Html" <|
\() ->
testRule """
a = H.button
"""
|> Review.Test.expectNoErrors
, test "should report the use of `H.button` when H is an alias for Html" <|
\() ->
testRule """
import Html as H
a = H.button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "H.button"
}
]
, test "should report the use of `button` when it has been imported explicitly" <|
\() ->
testRule """
import Html exposing (button)
a = button
2019-11-20 13:16:37 +03:00
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "button"
}
|> Review.Test.atExactly { start = { row = 5, column = 5 }, end = { row = 5, column = 11 } }
]
, test "should not report the use of `button` when it has been imported using `exposing (..)` and the dependency is not known" <|
2019-11-20 13:16:37 +03:00
\() ->
testRule """
import Html exposing (..)
a = button
"""
|> Review.Test.expectNoErrors
, test "should report the use of `button` when it has been imported using `exposing (..)` and the dependency is known" <|
2019-11-20 13:16:37 +03:00
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a = button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "button"
}
|> Review.Test.atExactly { start = { row = 5, column = 5 }, end = { row = 5, column = 11 } }
]
2019-12-07 17:29:20 +03:00
, test "should not report the use of `button` when it has been imported using `exposing (..)` and the dependency is known, but it has been redefined at the top-level as a function" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a = button
button = 1
"""
|> Review.Test.expectNoErrors
, test "should not report the use of `button` when it has been imported using `exposing (..)` and the dependency is known, but it has been redefined in an accessible let..in declaration" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a = let
button = 1
in button
2019-12-07 17:29:20 +03:00
"""
|> Review.Test.expectNoErrors
, test "should not report the use of `button` when it has been imported using `exposing (..)` and the dependency is known, but it has been redefined at the top-level as a port" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a = button
port button : (() -> msg) -> Sub msg
"""
|> Review.Test.expectNoErrors
, test "should not report the use of `button` when it has been imported using `exposing (button)`, but it has been redefined in an accessible let..in declaration" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a = let
button = 1
in button
"""
|> Review.Test.expectNoErrors
, test "should report the use of `button` when it has been imported using `exposing (..)` and the dependency is known, and it has been redefined in an out-of-scope let..in declaration" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a = let
button = 1
in 2
b = button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "button"
}
|> Review.Test.atExactly { start = { row = 8, column = 5 }, end = { row = 8, column = 11 } }
]
2019-11-27 02:17:14 +03:00
, test "should not report the use of `button` if it is shadowed by an accessible parameter" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a button =
button
"""
|> Review.Test.expectNoErrors
, test "should report the use of `button` even if it was defined as a parameter somewhere else" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a button =
button
b =
button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "button"
}
|> Review.Test.atExactly { start = { row = 8, column = 3 }, end = { row = 8, column = 9 } }
]
, test "should not report the use of `button` if it is shadowed by a name while pattern matching" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a =
case c of
Foo button -> button
"""
|> Review.Test.expectNoErrors
, test "should report the use of `button` even if a different pattern matching has a pattern that shadows it" <|
\() ->
testRuleWithHtmlDependency """
import Html exposing (..)
a =
case c of
Foo button -> button
Bar -> button
"""
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "button"
}
|> Review.Test.atExactly { start = { row = 8, column = 12 }, end = { row = 8, column = 18 } }
]
]
all : Test
all =
describe "NoHtmlButton" tests