|
| 1 | +package org.openapitools.codegen.kotlin.assertions; |
| 2 | + |
| 3 | +import org.assertj.core.api.AbstractAssert; |
| 4 | +import org.assertj.core.api.Assertions; |
| 5 | +import org.assertj.core.util.CanIgnoreReturnValue; |
| 6 | +import org.jetbrains.kotlin.psi.KtNamedFunction; |
| 7 | +import org.jetbrains.kotlin.psi.KtObjectDeclaration; |
| 8 | +import org.jetbrains.kotlin.psi.KtProperty; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +@CanIgnoreReturnValue |
| 15 | +public class CompanionAssert extends AbstractAssert<CompanionAssert, KtObjectDeclaration> { |
| 16 | + private final ClassAssert classAssert; |
| 17 | + |
| 18 | + CompanionAssert(final ClassAssert classAssert, final KtObjectDeclaration companion) { |
| 19 | + super(companion, CompanionAssert.class); |
| 20 | + this.classAssert = classAssert; |
| 21 | + } |
| 22 | + |
| 23 | + public CompanionPropertyAssert assertProperty(final String propertyName) { |
| 24 | + Assertions.assertThat(actual.getBody()) |
| 25 | + .withFailMessage("Expected companion to have a body, but it was null") |
| 26 | + .isNotNull(); |
| 27 | + final List<KtProperty> properties = actual.getBody().getProperties().stream() |
| 28 | + .filter(p -> Objects.equals(p.getName(), propertyName)) |
| 29 | + .collect(Collectors.toList()); |
| 30 | + Assertions.assertThat(properties) |
| 31 | + .withFailMessage("Expected companion object to have a single property %s, but found %s", propertyName, properties.size()) |
| 32 | + .hasSize(1); |
| 33 | + |
| 34 | + return new CompanionPropertyAssert(this, properties.get(0)); |
| 35 | + } |
| 36 | + |
| 37 | + public CompanionFunctionAssert assertFunction(final String functionName) { |
| 38 | + Assertions.assertThat(actual.getBody()) |
| 39 | + .withFailMessage("Expected companion to have a body, but it was null") |
| 40 | + .isNotNull(); |
| 41 | + final List<KtNamedFunction> functions = actual.getBody().getFunctions().stream() |
| 42 | + .filter(f -> Objects.equals(f.getName(), functionName)) |
| 43 | + .collect(Collectors.toList()); |
| 44 | + Assertions.assertThat(functions) |
| 45 | + .withFailMessage("Expected companion object to have a single function %s, but found %s", functionName, functions.size()) |
| 46 | + .hasSize(1); |
| 47 | + |
| 48 | + return new CompanionFunctionAssert(classAssert, functions.get(0)); |
| 49 | + } |
| 50 | + |
| 51 | + public ClassAssert toClass() { |
| 52 | + return classAssert; |
| 53 | + } |
| 54 | +} |
0 commit comments