From e586d5db79e19c1a2e32e34e78933d87b198b91d Mon Sep 17 00:00:00 2001 From: Fan Lin Date: Sun, 15 Nov 2020 14:34:29 +0800 Subject: [PATCH] handle matcher in verification --- .../testable/core/tool/InvokeVerifier.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java index fbab093..508ccb0 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/InvokeVerifier.java @@ -118,11 +118,14 @@ public class InvokeVerifier { for (Object[] r : records) { if (r.length == args.length) { for (int i = 0; i < r.length; i++) { - if (!r[i].equals(args[i])) { + if (!matches(r[i], args[i])) { 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; @@ -145,10 +148,14 @@ public class InvokeVerifier { * @param count number of invocations */ 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) { 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++) { if (lastVerification.inOrder) { @@ -174,13 +181,19 @@ public class InvokeVerifier { throw new VerifyFailedError("parameter " + (i + 1) + " type mismatch", ": " + 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)); } } 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) { StringBuilder sb = new StringBuilder(": "); for (int i = 0; i < args.length; i++) {