mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
handle matcher in verification
This commit is contained in:
parent
bf9ceeea0f
commit
e586d5db79
@ -118,13 +118,16 @@ public class InvokeVerifier {
|
|||||||
for (Object[] r : records) {
|
for (Object[] r : records) {
|
||||||
if (r.length == args.length) {
|
if (r.length == args.length) {
|
||||||
for (int i = 0; i < r.length; i++) {
|
for (int i = 0; i < r.length; i++) {
|
||||||
if (!r[i].equals(args[i])) {
|
if (!matches(r[i], args[i])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
if (i == r.length - 1) {
|
||||||
|
// all arguments are equal
|
||||||
throw new VerifyFailedError("was invoked with " + desc(args));
|
throw new VerifyFailedError("was invoked with " + desc(args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,10 +148,14 @@ public class InvokeVerifier {
|
|||||||
* @param count number of invocations
|
* @param count number of invocations
|
||||||
*/
|
*/
|
||||||
public InvokeVerifier times(int count) {
|
public InvokeVerifier times(int count) {
|
||||||
|
if (lastVerification == null) {
|
||||||
|
// when used independently, equals to `withTimes()`
|
||||||
|
System.out.println("Warning: using \"times()\" check without \"with()\" or \"withInOrder()\" method " +
|
||||||
|
"is not recommended, please use \"withTimes()\" instead.");
|
||||||
|
return withTimes(count);
|
||||||
|
}
|
||||||
if (count < 2) {
|
if (count < 2) {
|
||||||
throw new InvalidParameterException("should only use times() method with count equal or larger than 2.");
|
throw new InvalidParameterException("should only use times() method with count equal or larger than 2.");
|
||||||
} else if (lastVerification == null) {
|
|
||||||
throw new InvalidParameterException("should only use times() after with() or withInOrder() method.");
|
|
||||||
}
|
}
|
||||||
for (int i = 0; i < count - 1; i++) {
|
for (int i = 0; i < count - 1; i++) {
|
||||||
if (lastVerification.inOrder) {
|
if (lastVerification.inOrder) {
|
||||||
@ -174,13 +181,19 @@ public class InvokeVerifier {
|
|||||||
throw new VerifyFailedError("parameter " + (i + 1) + " type mismatch",
|
throw new VerifyFailedError("parameter " + (i + 1) + " type mismatch",
|
||||||
": " + args[i].getClass(), ": " + record[i].getClass());
|
": " + args[i].getClass(), ": " + record[i].getClass());
|
||||||
}
|
}
|
||||||
if (!args[i].equals(record[i])) {
|
if (!matches(args[i], record[i])) {
|
||||||
throw new VerifyFailedError("parameter " + (i + 1) + " mismatched", desc(args), desc(record));
|
throw new VerifyFailedError("parameter " + (i + 1) + " mismatched", desc(args), desc(record));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
records.remove(order);
|
records.remove(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean matches(Object realValue, Object expectValue) {
|
||||||
|
return expectValue instanceof InvokeMatcher ?
|
||||||
|
((InvokeMatcher) expectValue).matchFunction.check(realValue) :
|
||||||
|
expectValue.equals(realValue);
|
||||||
|
}
|
||||||
|
|
||||||
private String desc(Object[] args) {
|
private String desc(Object[] args) {
|
||||||
StringBuilder sb = new StringBuilder(": ");
|
StringBuilder sb = new StringBuilder(": ");
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user