-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
MagicNumberSpec.kt
1025 lines (861 loc) · 32.1 KB
/
MagicNumberSpec.kt
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
private const val IGNORE_NUMBERS = "ignoreNumbers"
private const val IGNORE_HASH_CODE = "ignoreHashCodeFunction"
private const val IGNORE_PROPERTY_DECLARATION = "ignorePropertyDeclaration"
private const val IGNORE_LOCAL_VARIABLES = "ignoreLocalVariableDeclaration"
private const val IGNORE_CONSTANT_DECLARATION = "ignoreConstantDeclaration"
private const val IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION = "ignoreCompanionObjectPropertyDeclaration"
private const val IGNORE_ANNOTATION = "ignoreAnnotation"
private const val IGNORE_NAMED_ARGUMENT = "ignoreNamedArgument"
private const val IGNORE_ENUMS = "ignoreEnums"
private const val IGNORE_RANGES = "ignoreRanges"
private const val IGNORE_EXTENSION_FUNCTIONS = "ignoreExtensionFunctions"
class MagicNumberSpec {
@Nested
inner class `a float of 1` {
val code = "val myFloat = 1.0f"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).hasSourceLocation(1, 15)
}
}
@Nested
inner class `a const float of 1` {
val code = "const val MY_FLOAT = 1.0f"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `an integer of 1` {
val code = "val myInt = 1"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).hasSourceLocation(1, 13)
}
}
@Nested
inner class `a const integer of 1` {
val code = "const val MY_INT = 1"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `a long of 1` {
val code = "val myLong = 1L"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).hasSourceLocation(1, 14)
}
}
@Nested
inner class `a long of -1` {
val code = "val myLong = -1L"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).hasSourceLocation(1, 15)
}
}
@Nested
inner class `a long of -2` {
val code = "val myLong = -2L"
@Test
fun `should be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).hasSourceLocation(1, 15)
}
@Test
fun `should be ignored when ignoredNumbers contains it verbatim`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("-2L")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be ignored when ignoredNumbers contains it as floating point`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("-2f")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be ignored when ignoredNumbers contains 2 but not -2`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("1", "2", "3", "-1", "0"))))
.lint(code)
assertThat(findings).hasSourceLocation(1, 15)
}
@Test
fun `should not be ignored when ignoredNumbers contains 2 but not -2 config with string`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to "1,2,3,-1,0")))
.lint(code)
assertThat(findings).hasSourceLocation(1, 15)
}
}
@Nested
inner class `a const long of 1` {
val code = "const val MY_LONG = 1L"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `a double of 1` {
val code = "val myDouble = 1.0"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).hasSourceLocation(1, 16)
}
}
@Nested
inner class `a const double of 1` {
val code = "const val MY_DOUBLE = 1.0"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `a hex of 1` {
val code = "val myHex = 0x1"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).hasSourceLocation(1, 13)
}
}
@Nested
inner class `a const hex of 1` {
val code = "const val MY_HEX = 0x1"
@Test
fun `should not be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignoredNumbers is empty`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>()))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `an integer of 300` {
val code = "val myInt = 300"
@Test
fun `should not be reported when ignoredNumbers contains 300`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("300")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignoredNumbers contains a floating point 300`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("300.0")))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `a binary literal` {
val code = "val myBinary = 0b01001"
@Test
fun `should not be reported`() {
val findings = MagicNumber().lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `should not be reported when ignoredNumbers contains a binary literal 0b01001`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("0b01001")))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `an integer literal with underscores` {
val code = "val myInt = 100_000"
@Test
fun `should be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).hasSourceLocation(1, 13)
}
@Test
fun `should not be reported when ignored verbatim`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("100_000")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignored with different underscores`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("10_00_00")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not be reported when ignored without underscores`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("100000")))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `an if statement with magic numbers` {
val code = "val myInt = if (5 < 6) 7 else 8"
@Test
fun `should be reported`() {
val findings = MagicNumber().lint(code)
assertThat(findings)
.hasStartSourceLocations(
SourceLocation(1, 17),
SourceLocation(1, 21),
SourceLocation(1, 24),
SourceLocation(1, 31)
)
}
}
@Nested
inner class `a when statement with magic numbers` {
val code = """
fun test(x: Int) {
when (x) {
5 -> return 5
4 -> return 4
3 -> return 3
}
}
"""
@Test
fun `should be reported`() {
val findings = MagicNumber().lint(code)
assertThat(findings).hasStartSourceLocations(
SourceLocation(3, 9),
SourceLocation(3, 21),
SourceLocation(4, 9),
SourceLocation(4, 21),
SourceLocation(5, 9),
SourceLocation(5, 21)
)
}
}
@Nested
inner class `a method containing variables with magic numbers` {
val code = """
fun test(x: Int) {
val i = 5
}
"""
@Test
fun `should be reported`() {
val findings = MagicNumber().lint(code)
assertThat(findings).hasSize(1)
}
}
@Nested
inner class `a boolean value` {
val code = """
fun test() : Boolean {
return true;
}
"""
@Test
fun `should not be reported`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `a non-numeric constant expression` {
val code = "val surprise = true"
@Test
fun `should not be reported`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
@DisplayName("a float of 0.5")
inner class Float {
val code = "val test = 0.5f"
@Test
fun `should be reported by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).hasSourceLocation(1, 12)
}
@Test
fun `should not be reported when ignoredNumbers contains it`() {
val findings = MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf(".5")))).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `a magic number number in a constructor call` {
@Test
fun `should report`() {
val code = "val file = Array<String?>(42) { null }"
val findings = MagicNumber().compileAndLint(code)
assertThat(findings).hasSize(1)
}
}
@Nested
inner class `an invalid ignoredNumber` {
@Test
fun `throws a NumberFormatException`() {
assertThatExceptionOfType(NumberFormatException::class.java).isThrownBy {
MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to listOf("banana")))).compileAndLint("val i = 0")
}
}
}
@Nested
inner class `an empty ignoredNumber` {
@Test
fun `doesn't throw an exception`() {
MagicNumber(TestConfig(mapOf(IGNORE_NUMBERS to emptyList<String>())))
}
}
@Nested
inner class `ignoring properties` {
val code = """
@Magic(number = 69)
class A {
val boringNumber = 42
const val BORING_CONSTANT = 93871
val duration = Duration.seconds(10)
val durationWithStdlibFunction = 10.toDuration(DurationUnit.MILLISECONDS)
override fun hashCode(): Int {
val iAmSoMagic = 7328672
}
companion object {
val anotherBoringNumber = 43
const val anotherBoringConstant = 93872
val color = Color(0x66000000)
val colorWithExplicitParameter = Color(color = 0x66000000)
}
}
data class Color(val color: Int)
"""
@Test
fun `should report all without ignore flags`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "false",
IGNORE_ANNOTATION to "false",
IGNORE_NAMED_ARGUMENT to "false",
IGNORE_HASH_CODE to "false",
IGNORE_CONSTANT_DECLARATION to "false",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "false"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings)
.hasStartSourceLocations(
SourceLocation(1, 17),
SourceLocation(3, 24),
SourceLocation(4, 33),
SourceLocation(5, 37),
SourceLocation(9, 26),
SourceLocation(13, 35),
SourceLocation(14, 43),
SourceLocation(15, 27),
SourceLocation(16, 56)
)
}
@Test
fun `should not report any issues with all ignore flags`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "true",
IGNORE_ANNOTATION to "true",
IGNORE_HASH_CODE to "true",
IGNORE_CONSTANT_DECLARATION to "true",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "true"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `magic numbers in companion object property assignments` {
val code = """
class A {
companion object {
val anotherBoringNumber = 43
const val anotherBoringConstant = 93872
}
}
"""
@Test
fun `should not report any issues by default`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report any issues when ignoring properties but not constants nor companion objects`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "true",
IGNORE_CONSTANT_DECLARATION to "false",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "false"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report any issues when ignoring properties and constants but not companion objects`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "true",
IGNORE_CONSTANT_DECLARATION to "true",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "false"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report any issues when ignoring properties, constants and companion objects`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "true",
IGNORE_CONSTANT_DECLARATION to "true",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "true"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report any issues when ignoring companion objects but not properties and constants`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "false",
IGNORE_CONSTANT_DECLARATION to "false",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "true"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should report property when ignoring constants but not properties and companion objects`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "false",
IGNORE_CONSTANT_DECLARATION to "true",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "false"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings).hasSourceLocation(4, 35)
}
@Test
fun `should report property and constant when not ignoring properties, constants nor companion objects`() {
val config = TestConfig(
mapOf(
IGNORE_PROPERTY_DECLARATION to "false",
IGNORE_CONSTANT_DECLARATION to "false",
IGNORE_COMPANION_OBJECT_PROPERTY_DECLARATION to "false"
)
)
val findings = MagicNumber(config).lint(code)
assertThat(findings)
.hasStartSourceLocations(
SourceLocation(4, 35),
SourceLocation(5, 43)
)
}
}
@Nested
inner class `a property without number` {
val code = "private var pair: Pair<String, Int>? = null"
@Test
fun `should not lead to a crash #276`() {
val findings = MagicNumber().lint(code)
assertThat(findings).isEmpty()
}
}
@Nested
inner class `ignoring named arguments` {
@Nested
inner class `in constructor invocation` {
private fun code(numberString: String) = """
data class Model(
val someVal: Int,
val other: String = "default"
)
var model = Model(someVal = $numberString)
"""
@Test
fun `should not ignore int`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false")))
assertThat(rule.lint(code("53"))).hasSize(1)
}
@Test
fun `should not ignore float`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false")))
assertThat(rule.lint(code("53f"))).hasSize(1)
}
@Test
fun `should not ignore binary`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false")))
assertThat(rule.lint(code("0b01001"))).hasSize(1)
}
@Test
fun `should ignore integer with underscores`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false")))
assertThat(rule.lint(code("101_000"))).hasSize(1)
}
@Test
fun `should ignore numbers by default`() {
assertThat(MagicNumber().lint(code("53"))).isEmpty()
}
@Test
fun `should ignore negative numbers by default`() {
assertThat(MagicNumber().lint(code("-53"))).isEmpty()
}
@Test
fun `should ignore named arguments in inheritance - #992`() {
val code = """
abstract class A(n: Int)
object B : A(n = 5)
"""
assertThat(MagicNumber().compileAndLint(code)).isEmpty()
}
@Test
fun `should ignore named arguments in parameter annotations - #1115`() {
val code =
"@JvmStatic fun setCustomDimension(@IntRange(from = 0, to = 19) index: Int, value: String?) {}"
assertThat(MagicNumber().lint(code)).isEmpty()
}
}
@Nested
inner class `Issue#659 - false-negative reporting on unnamed argument when ignore is true` {
private fun code(numberString: String) = """
data class Model(
val someVal: Int,
val other: String = "default"
)
var model = Model($numberString)
"""
@Test
fun `should detect the argument by default`() {
assertThat(MagicNumber().lint(code("53"))).hasSize(1)
}
}
@Nested
inner class `in function invocation` {
private fun code(number: Number) = """
fun tested(someVal: Int, other: String = "default")
val t = tested(someVal = $number)
"""
@Test
fun `should ignore int by default`() {
assertThat(MagicNumber().lint(code(53))).isEmpty()
}
@Test
fun `should ignore float by default`() {
assertThat(MagicNumber().lint(code(53f))).isEmpty()
}
@Test
fun `should ignore binary by default`() {
assertThat(MagicNumber().lint(code(0b01001))).isEmpty()
}
@Test
fun `should ignore integer with underscores`() {
assertThat(MagicNumber().lint(code(101_000))).isEmpty()
}
}
@Nested
inner class `in enum constructor argument` {
val code = """
enum class Bag(id: Int) {
SMALL(1),
EXTRA_LARGE(5)
}
"""
@Test
fun `should be reported by default`() {
assertThat(MagicNumber().lint(code)).hasSize(1)
}
@Test
fun `numbers when 'ignoreEnums' is set to true`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_ENUMS to "true")))
assertThat(rule.lint(code)).isEmpty()
}
}
@Nested
inner class `in enum constructor as named argument` {
val code = """
enum class Bag(id: Int) {
SMALL(id = 1),
EXTRA_LARGE(id = 5)
}
"""
@Test
fun `should be reported`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_NAMED_ARGUMENT to "false")))
assertThat(rule.lint(code)).hasSize(1)
}
@Test
fun `numbers when 'ignoreEnums' is set to true`() {
val rule = MagicNumber(
TestConfig(
mapOf(
IGNORE_NAMED_ARGUMENT to "false",
IGNORE_ENUMS to "true"
)
)
)
assertThat(rule.lint(code)).isEmpty()
}
}
}
@Nested
inner class `functions with and without braces which return values` {
@Test
fun `does not report functions that always returns a constant value`() {
val code = """
fun x() = 9
fun y(): Int { return 9 }
"""
assertThat(MagicNumber().compileAndLint(code)).isEmpty()
}
@Test
fun `reports functions that does not return a constant value`() {
val code = """
fun x() = 9 + 1
fun y(): Int { return 9 + 1 }
"""
assertThat(MagicNumber().compileAndLint(code)).hasSize(2)
}
}
@Nested
inner class `in-class declaration with default parameters` {
@Test
fun `reports no finding`() {
val code = "class SomeClassWithDefault(val defaultValue: Int = 10)"
assertThat(MagicNumber().lint(code)).isEmpty()
}
@Test
fun `reports no finding for an explicit declaration`() {
val code = "class SomeClassWithDefault constructor(val defaultValue: Int = 10)"
assertThat(MagicNumber().lint(code)).isEmpty()
}
@Test
fun `reports no finding for a function expression`() {
val code =
"class SomeClassWithDefault constructor(val defaultValue: Duration = 10.toDuration(DurationUnit.MILLISECONDS))"
assertThat(MagicNumber().lint(code)).isEmpty()
}
}
@Nested
inner class `default parameters in secondary constructor` {
@Test
fun `reports no finding`() {
val code = """
class SomeClassWithDefault {
constructor(val defaultValue: Int = 10) { }
}
"""
assertThat(MagicNumber().lint(code)).isEmpty()
}
@Test
fun `reports no finding for a function expression`() {
val code = """
class SomeClassWithDefault {
constructor(val defaultValue: Duration = 10.toDuration(DurationUnit.MILLISECONDS)) { }
}
"""
assertThat(MagicNumber().lint(code)).isEmpty()
}
}
@Nested
inner class `default parameters in function` {
@Test
fun `reports no finding`() {
val code = "fun f(p: Int = 100)"
assertThat(MagicNumber().lint(code)).isEmpty()
}
@Test
fun `reports no finding for a function expression`() {
val code = "fun f(p: Duration = 10.toDuration(DurationUnit.MILLISECONDS))"
assertThat(MagicNumber().lint(code)).isEmpty()
}
}
@Nested
inner class `a number as part of a range` {
@Suppress("UnusedPrivateMember")
private fun cases() = listOf(
"val range = 1..27",
"val range = (1..27)",
"val range = 27 downTo 1",
"val range = 1 until 27 step 1",
"val inRange = 1 in 1..27",
"val inRange = (1 in 27 downTo 0 step 1)",
"val inRange = (1..27 step 1).last"
)
@ParameterizedTest
@MethodSource("cases")
fun `reports a code smell by default`(code: String) {
assertThat(MagicNumber().lint(code)).hasSize(1)
}
@ParameterizedTest
@MethodSource("cases")
fun `reports a code smell if ranges are not ignored`(code: String) {
assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "false"))).lint(code))
.hasSize(1)
}
@ParameterizedTest
@MethodSource("cases")
fun `reports no finding if ranges are ignored`(code: String) {
assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code))
.isEmpty()
}
@Test
fun `reports a finding for a parenthesized number if ranges are ignored`() {
val code = "val foo : Int = (127)"
assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code)).hasSize(1)
}
@Test
fun `reports a finding for an addition if ranges are ignored`() {
val code = "val foo : Int = 1 + 27"
assertThat(MagicNumber(TestConfig(mapOf(IGNORE_RANGES to "true"))).lint(code)).hasSize(1)
}
}
@Nested
inner class `a number assigned to a local variable` {
val code = """fun f() { val a = 3; }"""
@Test
fun `reports 3 due to the assignment to a local variable`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_LOCAL_VARIABLES to "false")))
assertThat(rule.compileAndLint(code)).hasSize(1)
}
@Test
fun `should not report 3 due to the ignored local variable config`() {
val rule = MagicNumber(TestConfig(mapOf(IGNORE_LOCAL_VARIABLES to "true")))
assertThat(rule.compileAndLint(code)).isEmpty()
}
}
@Nested
inner class `meaningful variables - #1536` {
val rule =
MagicNumber(
TestConfig(
mapOf(
IGNORE_LOCAL_VARIABLES to "true",
IGNORE_NAMED_ARGUMENT to "true"
)
)
)
@Test
fun `should report 3`() {
assertThat(rule.compileAndLint("""fun bar() { foo(3) }; fun foo(n: Int) {}""")).hasSize(1)
}
@Test
fun `should not report named 3`() {
assertThat(rule.compileAndLint("""fun bar() { foo(param=3) }; fun foo(param: Int) {}""")).isEmpty()
}
@Test
fun `should not report 3 due to scoped describing variable`() {
assertThat(rule.compileAndLint("""fun bar() { val a = 3; foo(a) }; fun foo(n: Int) {}""")).isEmpty()
}
}
@Nested
inner class `with extension function` {
val rule =
MagicNumber(
TestConfig(
mapOf(
IGNORE_EXTENSION_FUNCTIONS to "true"
)
)
)
@Test
fun `should not report when function`() {
val code = """
fun Int.dp() = this + 1
val a = 500.dp()
"""
assertThat(rule.compileAndLint(code)).isEmpty()
}