elm-review/tests/NoDebugTest.elm

154 lines
5.3 KiB
Elm
Raw Normal View History

2017-06-18 15:00:48 +03:00
module NoDebugTest exposing (all)
2018-11-06 20:46:46 +03:00
import Elm.Syntax.Range exposing (Location, Range)
2018-11-11 01:37:18 +03:00
import Lint exposing (Rule)
import Lint.Error as Error exposing (Error)
2018-11-11 01:43:58 +03:00
import Lint.Rule exposing (LintResult)
import Lint.Rule.NoDebug exposing (rule)
2018-11-22 21:19:19 +03:00
import Test exposing (Test, describe, test)
2019-06-16 15:53:07 +03:00
import TestUtil
2017-06-18 14:43:37 +03:00
testRule : String -> LintResult
2018-11-06 20:46:46 +03:00
testRule string =
"module A exposing (..)\n\n"
++ string
2019-06-16 15:53:07 +03:00
|> TestUtil.ruleTester rule
error : Range -> Error
error range =
Error.create "Forbidden use of Debug" range
tests : List Test
tests =
[ test "should not report normal function calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = foo n
b = bar.foo n
c = debug
e = debug.log n
d = debug.log n
"""
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors []
, test "should report Debug.log use" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = Debug.log"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 5 ) ( 3, 14 )) ]
, test "should report Debug.log calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = Debug.log z"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 5 ) ( 3, 14 )) ]
, test "should report multiple Debug.log calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = Debug.log z
b = Debug.log z
"""
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 4, 5 ) ( 4, 14 ))
, error (TestUtil.location ( 5, 5 ) ( 5, 14 ))
]
, test "should report Debug.crash calls" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = Debug.crash 1"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 5 ) ( 3, 16 )) ]
, test "should report any Debug method" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = Debug.foo 1"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 5 ) ( 3, 14 )) ]
, test "should report Debug in a binary expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = (Debug.log z) + 2"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 6 ) ( 3, 15 )) ]
, test "should report Debug in a << binary expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = fn << Debug.log"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 11 ) ( 3, 20 )) ]
, test "should report Debug in a pipe expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = fn |> Debug.log z"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 11 ) ( 3, 20 )) ]
, test "should report Debug in an list expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = [Debug.log z y]"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 6 ) ( 3, 15 )) ]
, test "should report Debug in an record expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = { foo = Debug.log z y }"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 13 ) ( 3, 22 )) ]
2017-06-18 15:06:36 +03:00
, test "should report Debug in an record update expression" <|
\() ->
testRule "a = { model | foo = Debug.log z y }"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 21 ) ( 3, 30 )) ]
, test "should report Debug in an lambda expression" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = (\\foo -> Debug.log z foo)"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 14 ) ( 3, 23 )) ]
, test "should report Debug in an if expression condition" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = if Debug.log a b then True else False"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 8 ) ( 3, 17 )) ]
2018-11-06 20:46:46 +03:00
, test "should report Debug in an if expression then branch" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = if True then Debug.log a b else False"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 18 ) ( 3, 27 )) ]
2018-11-06 20:46:46 +03:00
, test "should report Debug in an if expression else branch" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule "a = if True then True else Debug.log a b"
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 3, 28 ) ( 3, 37 )) ]
, test "should report Debug in a case value" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = case Debug.log a b of
_ -> []
"""
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 4, 10 ) ( 4, 19 )) ]
, test "should report Debug in a case body" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = case a of
_ -> Debug.log a b
"""
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 5, 8 ) ( 5, 17 )) ]
, test "should report Debug in let declaration section" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = let b = Debug.log a b
in b
"""
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 4, 13 ) ( 4, 22 )) ]
, test "should report Debug in let body" <|
\() ->
2017-06-18 14:43:37 +03:00
testRule """
2018-11-06 20:46:46 +03:00
a = let b = c
in Debug.log a b
"""
2019-06-16 15:53:07 +03:00
|> TestUtil.expectErrors
[ error (TestUtil.location ( 5, 8 ) ( 5, 17 )) ]
]
all : Test
all =
describe "NoDebug" tests