Rename .toString and .equals to be less confusing.

There will now be "resultToString" and "resultEquals" to stringify / compare
the method results.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=150135475
This commit is contained in:
guyben 2017-03-14 17:14:58 -07:00 committed by Ben McIlwain
parent 3a13a4d6b6
commit e6a5083b55
2 changed files with 23 additions and 22 deletions

View file

@ -83,14 +83,14 @@ public abstract class ComparingInvocationHandler<T> implements InvocationHandler
* @param method the method whose return value is given * @param method the method whose return value is given
* @param object the object returned by a call to method * @param object the object returned by a call to method
*/ */
protected String toString( protected String stringifyResult(
@SuppressWarnings("unused") Method method, @SuppressWarnings("unused") Method method,
@Nullable Object object) { @Nullable Object object) {
return String.valueOf(object); return String.valueOf(object);
} }
/** /**
* Implements equals for specific types. * Checks whether the method results are as similar as we expect.
* *
* <p>By default objects are compared using their .equals. If .equals isn't implemented for some * <p>By default objects are compared using their .equals. If .equals isn't implemented for some
* relevant classes (or if we want change what is considered "not equal"), override this method * relevant classes (or if we want change what is considered "not equal"), override this method
@ -100,7 +100,7 @@ public abstract class ComparingInvocationHandler<T> implements InvocationHandler
* @param actual the object returned by a call to method for the "actual" implementation * @param actual the object returned by a call to method for the "actual" implementation
* @param second the object returned by a call to method for the "second" implementation * @param second the object returned by a call to method for the "second" implementation
*/ */
protected boolean equals( protected boolean compareResults(
@SuppressWarnings("unused") Method method, @SuppressWarnings("unused") Method method,
@Nullable Object actual, @Nullable Object actual,
@Nullable Object second) { @Nullable Object second) {
@ -108,7 +108,7 @@ public abstract class ComparingInvocationHandler<T> implements InvocationHandler
} }
/** /**
* Implements equals for thrown exceptions. * Checks whether the thrown exceptions are as similar as we expect.
* *
* <p>By default this returns 'true' for any input: all we check by default is that both * <p>By default this returns 'true' for any input: all we check by default is that both
* implementations threw something. Override if you need to actually compare both throwables. * implementations threw something. Override if you need to actually compare both throwables.
@ -117,7 +117,7 @@ public abstract class ComparingInvocationHandler<T> implements InvocationHandler
* @param actual the exception thrown by a call to method for the "actual" implementation * @param actual the exception thrown by a call to method for the "actual" implementation
* @param second the exception thrown by a call to method for the "second" implementation * @param second the exception thrown by a call to method for the "second" implementation
*/ */
protected boolean exceptionEquals( protected boolean compareException(
@SuppressWarnings("unused") Method method, @SuppressWarnings("unused") Method method,
Throwable actual, Throwable actual,
Throwable second) { Throwable second) {
@ -133,7 +133,7 @@ public abstract class ComparingInvocationHandler<T> implements InvocationHandler
* @param method the method whose return value is given * @param method the method whose return value is given
* @param exception the exception thrown by a call to method * @param exception the exception thrown by a call to method
*/ */
protected String exceptionToString( protected String stringifyException(
@SuppressWarnings("unused") Method method, @SuppressWarnings("unused") Method method,
Throwable exception) { Throwable exception) {
return exception.toString(); return exception.toString();
@ -159,35 +159,35 @@ public abstract class ComparingInvocationHandler<T> implements InvocationHandler
// First compare the two implementations' result, and log any differences: // First compare the two implementations' result, and log any differences:
if (actualException != null && secondException != null) { if (actualException != null && secondException != null) {
if (!exceptionEquals(method, actualException, secondException)) { if (!compareException(method, actualException, secondException)) {
log( log(
method, method,
String.format( String.format(
"Both implementations threw, but got different exceptions! '%s' vs '%s'", "Both implementations threw, but got different exceptions! '%s' vs '%s'",
exceptionToString(method, actualException), stringifyException(method, actualException),
exceptionToString(method, secondException))); stringifyException(method, secondException)));
} }
} else if (actualException != null) { } else if (actualException != null) {
log( log(
method, method,
String.format( String.format(
"Only actual implementation threw exception: %s", "Only actual implementation threw exception: %s",
exceptionToString(method, actualException))); stringifyException(method, actualException)));
} else if (secondException != null) { } else if (secondException != null) {
log( log(
method, method,
String.format( String.format(
"Only second implementation threw exception: %s", "Only second implementation threw exception: %s",
exceptionToString(method, secondException))); stringifyException(method, secondException)));
} else { } else {
// Neither threw exceptions - we compare the results // Neither threw exceptions - we compare the results
if (!equals(method, actualResult, secondResult)) { if (!compareResults(method, actualResult, secondResult)) {
log( log(
method, method,
String.format( String.format(
"Got different results! '%s' vs '%s'", "Got different results! '%s' vs '%s'",
toString(method, actualResult), stringifyResult(method, actualResult),
toString(method, secondResult))); stringifyResult(method, secondResult)));
} }
} }

View file

@ -79,26 +79,27 @@ public class ComparingInvocationHandlerTest {
log.add(String.format("%s: %s", method.getName(), message)); log.add(String.format("%s: %s", method.getName(), message));
} }
@Override protected boolean equals(Method method, @Nullable Object a, @Nullable Object b) { @Override protected boolean compareResults(
Method method, @Nullable Object a, @Nullable Object b) {
if (method.getReturnType().equals(Dummy.class)) { if (method.getReturnType().equals(Dummy.class)) {
return dummyEqualsResult; return dummyEqualsResult;
} }
return super.equals(method, a, b); return super.compareResults(method, a, b);
} }
@Override protected String toString(Method method, @Nullable Object a) { @Override protected String stringifyResult(Method method, @Nullable Object a) {
if (method.getReturnType().equals(Dummy.class)) { if (method.getReturnType().equals(Dummy.class)) {
return "dummy"; return "dummy";
} }
return super.toString(method, a); return super.stringifyResult(method, a);
} }
@Override protected boolean exceptionEquals(Method method, Throwable a, Throwable b) { @Override protected boolean compareException(Method method, Throwable a, Throwable b) {
return exceptionEqualsResult && super.exceptionEquals(method, a, b); return exceptionEqualsResult && super.compareException(method, a, b);
} }
@Override protected String exceptionToString(Method method, Throwable a) { @Override protected String stringifyException(Method method, Throwable a) {
return String.format("testException(%s)", super.exceptionToString(method, a)); return String.format("testException(%s)", super.stringifyException(method, a));
} }
} }