-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
START: improve equality strategy to use
check
- Loading branch information
Showing
2 changed files
with
67 additions
and
36 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
generator/src/main/java/io/stubbs/truth/generator/internal/EqualityStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.stubbs.truth.generator.internal; | ||
|
||
import io.stubbs.truth.generator.internal.model.ThreeSystem; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jboss.forge.roaster.model.source.JavaClassSource; | ||
import org.jboss.forge.roaster.model.source.MethodSource; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import static java.lang.String.format; | ||
import static org.apache.commons.lang3.StringUtils.capitalize; | ||
import static org.apache.commons.lang3.StringUtils.removeStart; | ||
|
||
/** | ||
* @author Antony Stubbs | ||
*/ | ||
@Slf4j | ||
public class EqualityStrategy extends AssertionMethodStrategy { | ||
|
||
|
||
@Override | ||
public boolean addStrategyMaybe(ThreeSystem<?> threeSystem, Method method, JavaClassSource generated) { | ||
equalityStrategyGeneric(method, generated, false); | ||
equalityStrategyGeneric(method, generated, true); | ||
return true; | ||
} | ||
|
||
private void equalityStrategyGeneric(Method method, JavaClassSource generated, boolean positive) { | ||
Class<?> returnType = method.getReturnType(); | ||
boolean primitive = returnType.isPrimitive(); | ||
String equality = primitive ? " == expected" : ".equals(expected)"; | ||
|
||
String body = "" + | ||
" if (%s(actual.%s()%s)) {\n" + | ||
" failWithActual(fact(\"expected %s %sto be equal to\", expected));\n" + | ||
" }\n"; | ||
|
||
body = "" + | ||
" check().that().isEqualTo()if (%s(actual.%s()%s)) {\n" + | ||
" failWithActual(fact(\"expected %s %sto be equal to\", expected));\n" + | ||
" }\n"; | ||
|
||
String testPrefix = positive ? "!" : ""; | ||
String say = positive ? "" : "NOT "; | ||
String fieldName = removeStart(method.getName(), "get"); | ||
body = format(body, testPrefix, method.getName(), equality, fieldName, say); | ||
|
||
String methodName = "has" + capitalize(fieldName) + capitalize(say.toLowerCase()).trim() + "EqualTo"; | ||
MethodSource<JavaClassSource> newMethod = generated.addMethod(); | ||
newMethod.setName(methodName) | ||
.setReturnTypeVoid() | ||
.setBody(body) | ||
.setPublic(); | ||
newMethod.addParameter(returnType, "expected"); | ||
|
||
newMethod.getJavaDoc().setText("Simple check for equality for all fields."); | ||
|
||
copyThrownExceptions(method, newMethod); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters