File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 39
39
import java .util .Collections ;
40
40
import org .hamcrest .MatcherAssert ;
41
41
import org .hamcrest .Matchers ;
42
+ import org .hamcrest .core .IsEqual ;
43
+ import org .hamcrest .core .IsNot ;
42
44
import org .junit .Test ;
43
45
44
46
/**
@@ -310,6 +312,24 @@ public void forbidsCodeInConstructor()
310
312
).validate ();
311
313
}
312
314
315
+ /**
316
+ * PmdValidator allows lambda in constructor.
317
+ * @throws Exception If something wrong happens inside.
318
+ */
319
+ @ Test
320
+ public void allowsLambdaInConstructor ()
321
+ throws Exception {
322
+ final String file = "LambdaInConstructor.java" ;
323
+ new PmdAssert (
324
+ file , new IsEqual <>(true ),
325
+ new IsNot <>(
326
+ RegexMatchers .containsPattern (
327
+ String .format (PmdValidatorTest .CODE_IN_CON , file )
328
+ )
329
+ )
330
+ ).validate ();
331
+ }
332
+
313
333
/**
314
334
* PmdValidator forbids usage of Files.createFile in tests.
315
335
* @throws Exception If something wrong happens inside.
Original file line number Diff line number Diff line change
1
+ package foo ;
2
+
3
+ public final class LambdaInConstructor {
4
+ private final transient int number ;
5
+ private final transient int another ;
6
+
7
+ public LambdaInConstructor (final int parameter ) {
8
+ this (
9
+ parameter ,
10
+ () -> {
11
+ final int ret ;
12
+ if (parameter % 2 == 0 ){
13
+ ret = 10 ;
14
+ } else {
15
+ ret = 20 ;
16
+ }
17
+ return ret ;
18
+ }
19
+ );
20
+ }
21
+
22
+ public LambdaInConstructor (final int parameter , final int other ) {
23
+ this .number = parameter ;
24
+ this .another = other ;
25
+ }
26
+
27
+ public int num () {
28
+ return number + another ;
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments