/
space_around_operators_test.exs
328 lines (288 loc) · 7.44 KB
/
space_around_operators_test.exs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do
use Credo.TestHelper
@described_check Credo.Check.Consistency.SpaceAroundOperators
@without_spaces """
defmodule Credo.Sample1 do
@spec f(<<_::16, _::_*8>>) :: binary
defmodule InlineModule do
@min -1
@max +1
@type config_or_func :: Config.t() | (-> Config.t())
def foobar do
1+2
|> test1()
|> test2()
|> test3()
|> test4()
end
end
end
"""
@without_spaces2 """
defmodule OtherModule3 do
defmacro foo do
3+4
end
defp bar do
6*7
end
end
"""
@without_spaces3 """
defmodule OtherModule4 do
defp dictionary_changeset() do
fields = ~W(
a
b
)a
end
end
"""
@with_spaces """
defmodule Credo.Sample2 do
defmodule F do
def f(), do: 1 + 2
def g(), do: 3 + 1
def l(), do: [&+/2, &-/2, &*/2, &//2]
def x do
entries
|> Stream.map(&json_library().encode!/1)
|> Enum.join(".")
end
end
defmodule InlineModule do
@type config_or_func :: Config.t() | (-> Config.t())
# Fine
defp format_value("NPC_", "NPDT", <<skills::binary-27>>) do
{time, r} = :timer.tc(&unquote(module).unquote(part)/0)
end
# Gives warning
defp format_value("NPC_", "NPDT", <<stuff::integer, other_stuff::integer, even_more_stuff::integer,
skills::binary-27>>) do
skip = (SourceFile.column(source_file, line_no, text) || -1) + name_size
end
defp parse_image_stats(<< @gif_89_signature,
width::little-integer-size(16),
height::little-integer-size(16),
_remainder::binary >>) do
%{ filetype: :gif,
width: width,
height: height }
map_fn = &(enum.__struct__).map/2
end
def foo do
<<_, unquoted::binary-size(size), _>> = quoted
<<data::size(len)-binary, _::binary>>
<<102::integer-native, rest::binary>>
<<102::native-integer, rest::binary>>
<<102::unsigned-big-integer, rest::binary>>
<<102::unsigned-big-integer-size(8), rest::binary>>
<<102::unsigned-big-integer-8, rest::binary>>
<<102::signed-little-float-8, rest::binary>>
<<102::8-integer-big-unsigned, rest::binary>>
<<102, rest::binary>>
<< valsize :: 32-unsigned, rest::binary >>
end
def error(err_no) do
case err_no do
-1 -> :unknown_error
+2 -> :known_error
_ -> @error_map[err_no] || err_no
end
end
test "date_add with negative interval" do
dec = Decimal.new(-1)
assert [{2013, 1, 1}] = TestRepo.all(from p in Post, select: date_add(p.posted, ^-1, "year"))
assert [{2013, 1, 1}] = TestRepo.all(from p in Post, select: date_add(p.posted, ^-1.0, "year"))
assert [{2013, 1, 1}] = TestRepo.all(from p in Post, select: date_add(p.posted, ^dec, "year"))
end
def parse_response(<< _correlation_id :: 32-signed, error_code :: 16-signed, generation_id :: 32-signed,
protocol_len :: 16-signed, _protocol :: size(protocol_len)-binary,
leader_len :: 16-signed, leader :: size(leader_len)-binary,
member_id_len :: 16-signed, member_id :: size(member_id_len)-binary,
members_size :: 32-signed, rest :: binary >>) do
members = parse_members(members_size, rest, [])
%Response{error_code: KafkaEx.Protocol.error(error_code), generation_id: generation_id,
leader_id: leader, member_id: member_id, members: members}
end
defp int(<< value :: size(valsize)-binary >>, x) do
case x |> Integer.parse do
:error -> -1
{a, _} -> a
end
end
def bar do
c = n * -1
c = n + -1
c = n / -1
c = n - -1
[(3 * 4) + (2 / 2) - (-1 * 4) / 1 - 4]
[(3 * 4) + (2 / 2) - (-1 * 4) / 1 - 4]
[(3 * 4) + (2 / 2) - (-1 * 4) / 1 - 4]
|> my_func(&Some.Deep.Module.is_something/1)
end
end
end
"""
@with_spaces2 """
defmodule OtherModule3 do
defmacro foo do
1 && 2
end
defp bar do
:ok
end
end
"""
@with_spaces3 """
defmodule OtherModule3 do
defmacro foo do
case foo do
{line_no, line} -> nil
{line_no, line} ->
nil
end
end
end
"""
@with_spaces4 """
defmodule OtherModule3 do
@min -1
@max 2 + 1
@base_priority_map %{low: -10, normal: 1, higher: +20}
def foo(prio) when prio in -999..-1 do
end
for prio < -999..0 do
# something
end
end
"""
@with_spaces5 """
defmodule CredoTest do
@moduledoc ""
def test do
&String.capitalize/1
&String.downcase/1
&String.reverse/1
[&String.capitalize/1, &String.downcase/1, &String.reverse/1]
"foo" <> " Add more."
1 + 1 = 2
end
end
"""
if Version.match?(System.version(), ">= 1.6.0-rc") do
@with_spaces6 """
assert -24 == MyModule.fun
assert MyModule.fun != -24
ExUnit.assert -12 == MyApp.fun_that_should_return_a_negative
"""
else
@with_spaces6 """
"""
end
@with_and_without_spaces """
defmodule OtherModule3 do
defmacro foo do
3+4
end
defp bar do
6 *7
end
end
"""
@with_and_without_spaces2 """
defmodule CredoTests do
def bar do
2+3
4 + 5
end
end
"""
#
# cases NOT raising issues
#
test "it should not report issues when used with sigil" do
[
@without_spaces3
]
|> to_source_files()
|> refute_issues(@described_check)
end
test "it should not report issues if spaces are used everywhere" do
[
@with_spaces,
@with_spaces2,
@with_spaces3,
@with_spaces4
]
|> to_source_files()
|> refute_issues(@described_check)
end
test "it should not report issues if spaces are used everywhere in a single file" do
[
@with_spaces5
]
|> to_source_files()
|> refute_issues(@described_check)
end
test "it should not report issues if spaces are used everywhere in two files" do
[
@with_spaces5,
@with_spaces6
]
|> to_source_files()
|> refute_issues(@described_check)
end
test "it should not report issues if spaces are omitted everywhere" do
[
@without_spaces,
@without_spaces2
]
|> to_source_files()
|> refute_issues(@described_check)
end
#
# cases raising issues
#
test "it should report an issue for mixed styles /1" do
[
@without_spaces,
@with_spaces,
@with_spaces2,
@with_spaces3
]
|> to_source_files()
|> assert_issue(@described_check)
end
test "it should report an issue for mixed styles /2" do
[
@without_spaces,
@with_spaces2,
@with_spaces2
]
|> to_source_files()
|> assert_issue(@described_check)
end
test "it should report the correct result /4" do
[
@with_and_without_spaces
]
|> to_source_files()
|> assert_issue(@described_check)
end
test "it should report the correct result /5" do
[
@with_and_without_spaces2
]
|> to_source_files()
|> assert_issue(@described_check)
end
test "it should report the correct result /6" do
[
@with_and_without_spaces,
@with_and_without_spaces2
]
|> to_source_files()
|> assert_issues(@described_check)
end
end