Skip to content

Commit d284c34

Browse files
committedJan 15, 2019
For #887: Ignore lambdas on constructors.
1 parent 039e678 commit d284c34

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
 

‎qulice-pmd/src/test/java/com/qulice/pmd/PmdValidatorTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.Collections;
4040
import org.hamcrest.MatcherAssert;
4141
import org.hamcrest.Matchers;
42+
import org.hamcrest.core.IsEqual;
43+
import org.hamcrest.core.IsNot;
4244
import org.junit.Test;
4345

4446
/**
@@ -310,6 +312,24 @@ public void forbidsCodeInConstructor()
310312
).validate();
311313
}
312314

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+
313333
/**
314334
* PmdValidator forbids usage of Files.createFile in tests.
315335
* @throws Exception If something wrong happens inside.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.