|
5 | 5 | import com.github.javaparser.ast.body.*; |
6 | 6 | import com.github.javaparser.ast.nodeTypes.NodeWithName; |
7 | 7 | import com.github.javaparser.ast.nodeTypes.modifiers.NodeWithAbstractModifier; |
| 8 | +import com.github.javaparser.ast.type.ClassOrInterfaceType; |
8 | 9 | import org.assertj.core.api.AbstractAssert; |
9 | 10 | import org.assertj.core.api.Assertions; |
10 | 11 | import org.assertj.core.util.CanIgnoreReturnValue; |
|
15 | 16 | import java.util.Arrays; |
16 | 17 | import java.util.List; |
17 | 18 | import java.util.Optional; |
| 19 | +import java.util.Set; |
18 | 20 | import java.util.stream.Collectors; |
| 21 | +import java.util.stream.Stream; |
19 | 22 |
|
20 | 23 | @CanIgnoreReturnValue |
21 | 24 | public class JavaFileAssert extends AbstractAssert<JavaFileAssert, CompilationUnit> { |
@@ -56,6 +59,36 @@ public JavaFileAssert isNormalClass() { |
56 | 59 | return this; |
57 | 60 | } |
58 | 61 |
|
| 62 | + public JavaFileAssert implementsInterfaces(String... implementedInterfaces) { |
| 63 | + Set<String> expectedInterfaces = Stream.of(implementedInterfaces) |
| 64 | + .collect(Collectors.toSet()); |
| 65 | + |
| 66 | + Set<String> actualInterfaces = actual.getType(0) |
| 67 | + .asClassOrInterfaceDeclaration() |
| 68 | + .getImplementedTypes().stream() |
| 69 | + .map(ClassOrInterfaceType::getNameWithScope) |
| 70 | + .collect(Collectors.toSet()); |
| 71 | + |
| 72 | + Assertions.assertThat(actualInterfaces) |
| 73 | + .withFailMessage("Expected type %s to implement interfaces %s, but found %s", |
| 74 | + actual.getType(0).getName().asString(), expectedInterfaces, actualInterfaces) |
| 75 | + .isEqualTo(expectedInterfaces); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public JavaFileAssert doesNotImplementInterfaces(String... interfaces) { |
| 80 | + Set<String> forbiddenInterfaces = Stream.of(interfaces).collect(Collectors.toSet()); |
| 81 | + Set<String> implemented = actual.getType(0).asClassOrInterfaceDeclaration() |
| 82 | + .getImplementedTypes().stream() |
| 83 | + .map(ClassOrInterfaceType::getNameWithScope) |
| 84 | + .collect(Collectors.toSet()); |
| 85 | + Assertions.assertThat(implemented) |
| 86 | + .withFailMessage("Expected type %s to not implement interfaces %s, but found %s", |
| 87 | + actual.getType(0).getName().asString(), forbiddenInterfaces, implemented) |
| 88 | + .doesNotContainAnyElementsOf(forbiddenInterfaces); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
59 | 92 | public JavaFileAssert isAbstractClass() { |
60 | 93 | Assertions.assertThat(actual.getType(0).asClassOrInterfaceDeclaration()) |
61 | 94 | .withFailMessage("Expected type %s to be an abstract class", actual.getType(0).getName().asString()) |
|
0 commit comments