Skip to content

Commit

Permalink
START: improve equality strategy to use check
Browse files Browse the repository at this point in the history
  • Loading branch information
astubbs committed Apr 6, 2022
1 parent 2326307 commit ba7d6a4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class SubjectMethodGenerator extends AssertionMethodStrategy {

private final BooleanStrategy booleanStrategy;
private final EqualityStrategy equalityStrategy = new EqualityStrategy();
private final Set<AssertionMethodStrategy> strategies = new HashSet<>();
private final ChainStrategy chainStrategy;
private final JDKOverrideAnalyser bs = new JDKOverrideAnalyser(Options.get());
Expand Down Expand Up @@ -191,47 +192,14 @@ private void addFieldAccessors(Method method, JavaClassSource generated, Class<?
addMapStrategy(method, generated, classUnderTest);
}

addEqualityStrategy(method, generated, classUnderTest);
equalityStrategy.addStrategyMaybe(context, method, generated);
}

generated.addImport(Fact.class)
.setStatic(true)
.setName(Fact.class.getCanonicalName() + ".*");
}

private void addEqualityStrategy(Method method, JavaClassSource generated, Class<?> classUnderTest) {
equalityStrategyGeneric(method, generated, false);
equalityStrategyGeneric(method, generated, 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";

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);
}

private void addMapStrategy(Method method, JavaClassSource generated, Class<?> classUnderTest) {
addMapStrategyGeneric(method, generated, false);
addMapStrategyGeneric(method, generated, true);
Expand Down Expand Up @@ -343,9 +311,9 @@ public static class ClassOrGenerated {
* a new generated Subject
*/
@Getter
final ThreeSystem generated;
final ThreeSystem<?> generated;

ClassOrGenerated(final Class<? extends Subject> clazz, final ThreeSystem generated) {
ClassOrGenerated(final Class<? extends Subject> clazz, final ThreeSystem<?> generated) {
this.clazz = clazz;
this.generated = generated;
}
Expand All @@ -355,6 +323,7 @@ static Optional<ClassOrGenerated> ofClass(Class<? extends Subject> compiledSubje
}

static Optional<ClassOrGenerated> ofClass(Optional<Class<? extends Subject>> clazz) {
//noinspection OptionalIsPresent java 8
if (clazz.isPresent())
return of(new ClassOrGenerated(clazz.get(), null));
else return empty();
Expand Down

0 comments on commit ba7d6a4

Please sign in to comment.