elm-review/tests/NoConstantConditionTest.elm

179 lines
6.0 KiB
Elm
Raw Normal View History

2017-06-18 15:00:48 +03:00
module NoConstantConditionTest exposing (all)
2017-01-21 17:38:56 +03:00
2017-01-29 22:03:31 +03:00
import Lint.Rules.NoConstantCondition exposing (rule)
2018-11-05 17:40:41 +03:00
import Lint.Types exposing (LintError, LintResult, LintRule)
import Test exposing (Test, describe, test)
import TestUtil exposing (expectErrors, ruleTester)
2017-06-18 14:43:37 +03:00
testRule : String -> LintResult
testRule =
ruleTester rule
2017-01-21 17:38:56 +03:00
2017-06-12 21:21:27 +03:00
error : LintError
2017-01-21 17:38:56 +03:00
error =
2017-06-12 21:21:27 +03:00
LintError "NoConstantCondition" "Useless condition: It will always evaluate to the same value"
2017-01-21 17:38:56 +03:00
condition : String -> String
condition expr =
"a = if " ++ expr ++ " then 1 else 0"
comparisonOperators : List String
comparisonOperators =
[ "==", "/=", "<", "<=", ">", ">=" ]
createComparisonSource : String -> String
createComparisonSource op =
String.join "\n"
[ condition ("b " ++ op ++ " c")
, condition ("b " ++ op ++ " 0")
, condition ("0 " ++ op ++ " b")
]
validComparisonTests : List Test
validComparisonTests =
List.map
(\op ->
test ("should not report a condition that compares a variable (" ++ op ++ ")") <|
\() ->
2017-06-18 14:43:37 +03:00
testRule (createComparisonSource op)
|> expectErrors []
2017-01-21 17:38:56 +03:00
)
comparisonOperators
tests : List Test
tests =
2017-01-23 22:09:37 +03:00
[ test "should not report a condition that accesses a variable" <|
2017-01-21 17:38:56 +03:00
\() ->
condition "b"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors []
2017-01-21 17:38:56 +03:00
, test "should not report a condition that calls a function" <|
\() ->
condition "b c"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors []
2017-01-21 17:38:56 +03:00
, test "should not report a condition that compares different lists" <|
\() ->
condition "[a] == [b]"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors []
2017-01-21 17:38:56 +03:00
, test "should not report a condition that compares different records" <|
\() ->
condition "{a = 1} == {a = 2}"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors []
2017-01-21 17:38:56 +03:00
, test "should report condition that only has True" <|
\() ->
condition "True"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that only has False" <|
\() ->
condition "False"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares True to False (==)" <|
\() ->
condition "True == False"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares True to False (/=)" <|
\() ->
condition "True /= False"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares False to True (==)" <|
\() ->
condition "False == True"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares False to True (/=)" <|
\() ->
condition "False /= True"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two integers (==)" <|
\() ->
condition "1 == 1"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two integers (/=)" <|
\() ->
condition "1 /= 1"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two floats (==)" <|
\() ->
condition "1.1 == 1.1"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two floats (/=)" <|
\() ->
condition "1.1 /= 1.1"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two strings (==)" <|
\() ->
condition "\"foo\" == \"foo\""
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two strings (/=)" <|
\() ->
condition "\"foo\" /= \"foo\""
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two similar Lists (==)" <|
\() ->
condition "[1, 2] == [1, 2]"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two similar Lists (/=)" <|
\() ->
condition "[1, 2] /= [1, 2]"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two similar Lists, even with variables" <|
\() ->
condition "[1, a] == [1, a]"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two similar records" <|
\() ->
condition "{ a = 2 } == { a = 2 }"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two identical elements (==)" <|
\() ->
condition "b == b"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two identical elements (/=)" <|
\() ->
condition "b /= b"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two identical complex elements (==)" <|
\() ->
condition "((foo bar) ++ List.map a <| List.reduce b c d) == ((foo bar) ++ List.map a <| List.reduce b c d)"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
, test "should report condition that compares two identical complex elements (/=)" <|
\() ->
condition "((foo bar) ++ List.map a <| List.reduce b c d) /= ((foo bar) ++ List.map a <| List.reduce b c d)"
2017-06-18 14:43:37 +03:00
|> testRule
|> expectErrors [ error ]
2017-01-21 17:38:56 +03:00
]
++ validComparisonTests
all : Test
all =
describe "NoConstantCondition" tests