-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNoUnsafeRegexFromLiteralTest.elm
123 lines (120 loc) · 5.87 KB
/
NoUnsafeRegexFromLiteralTest.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module NoUnsafeRegexFromLiteralTest exposing (all)
import Elm.Project
import Expect exposing (Expectation)
import Json.Decode as Decode
import NoUnsafeRegexFromLiteral exposing (rule)
import Review.Project as Project exposing (Project)
import Review.Test
import Test exposing (Test, describe, test)
all : Test
all =
describe "NoUnsafeRegexFromLiteral"
[ test "should not report calls to Helpers.Regex.fromLiteral with a valid literal regex" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex
a = Helpers.Regex.fromLiteral "^abc$"
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
, test "should report calls to Helpers.Regex.fromLiteral with an invalid literal regex" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex
a = Helpers.Regex.fromLiteral "^ab($cd"
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Helpers.Regex.fromLiteral needs to be called with a valid regex."
, details =
[ "The regex you passed does not evaluate to a valid regex. Please fix it or use `Regex.fromString`."
]
, under = """Helpers.Regex.fromLiteral "^ab($cd\""""
}
]
, test "should report calls to Helpers.Regex.fromLiteral with a non-literal value" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex
a = Helpers.Regex.fromLiteral dynamicValue
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Helpers.Regex.fromLiteral needs to be called with a static string literal."
, details =
[ "This function serves to give you more guarantees about creating regular expressions, but if the argument is dynamic or too complex, I won't be able to tell you."
, "Either make the argument static or use Regex.fromString."
]
, under = "Helpers.Regex.fromLiteral dynamicValue"
}
]
, test "should not report calls to Helpers.Regex.fromLiteral with an valid literal regex containing back-slashes" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex
a = Helpers.Regex.fromLiteral "\\\\s"
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
, test "should report invalid calls if the function is called through a module alias" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex as R
a = R.fromLiteral dynamicValue
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Helpers.Regex.fromLiteral needs to be called with a static string literal."
, details =
[ "This function serves to give you more guarantees about creating regular expressions, but if the argument is dynamic or too complex, I won't be able to tell you."
, "Either make the argument static or use Regex.fromString."
]
, under = "R.fromLiteral dynamicValue"
}
]
, test "should report invalid calls if the function is called through a direct import" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex exposing (fromLiteral)
a = fromLiteral dynamicValue
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Helpers.Regex.fromLiteral needs to be called with a static string literal."
, details =
[ "This function serves to give you more guarantees about creating regular expressions, but if the argument is dynamic or too complex, I won't be able to tell you."
, "Either make the argument static or use Regex.fromString."
]
, under = "fromLiteral dynamicValue"
}
]
, test "should report invalid calls if the function is called through an import that exposes all" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex exposing (..)
a = fromLiteral dynamicValue
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Helpers.Regex.fromLiteral needs to be called with a static string literal."
, details =
[ "This function serves to give you more guarantees about creating regular expressions, but if the argument is dynamic or too complex, I won't be able to tell you."
, "Either make the argument static or use Regex.fromString."
]
, under = "fromLiteral dynamicValue"
}
]
, test "should not report invalid calls if the function was not imported" <|
\_ ->
"""module A exposing (..)
import Helpers.Regex
a = fromLiteral dynamicValue
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
]