|
| 1 | +/* |
| 2 | + Copyright (c) 2017 FinancialForce.com, inc. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * 'Classic' invocation verifier - checks that a method was called with the given arguments the expected number of times. |
| 7 | + * The order of method calls is not important. |
| 8 | + * @group Core |
| 9 | + */ |
| 10 | +public class fflib_AnyOrder extends fflib_MethodVerifier |
| 11 | +{ |
| 12 | + /* |
| 13 | + * Verifies a method was invoked the expected number of times, with the expected arguments. |
| 14 | + * @param qualifiedMethod The method to be verified. |
| 15 | + * @param methodArg The arguments of the method that needs to be verified. |
| 16 | + * @param verificationMode The verification mode that holds the setting about how the verification should be performed. |
| 17 | + */ |
| 18 | + protected override void verify( |
| 19 | + fflib_QualifiedMethod qm, |
| 20 | + fflib_MethodArgValues expectedArguments, |
| 21 | + fflib_VerificationMode verificationMode) |
| 22 | + { |
| 23 | + List<fflib_IMatcher> expectedMatchers = fflib_Match.Matching ? fflib_Match.getAndClearMatchers(expectedArguments.argValues.size()) : null; |
| 24 | + List<fflib_MethodArgValues> actualArguments = fflib_MethodCountRecorder.getMethodArgumentsByTypeName().get(qm); |
| 25 | + |
| 26 | + Integer methodCount = getMethodCount(expectedArguments, expectedMatchers, actualArguments); |
| 27 | + |
| 28 | + String qualifier = ''; |
| 29 | + Integer expectedCount = null; |
| 30 | + |
| 31 | + if((verificationMode.VerifyMin == verificationMode.VerifyMax) && methodCount != verificationMode.VerifyMin) |
| 32 | + { |
| 33 | + expectedCount = verificationMode.VerifyMin; |
| 34 | + } |
| 35 | + else if (verificationMode.VerifyMin != null && verificationMode.VerifyMin > methodCount) |
| 36 | + { |
| 37 | + expectedCount = verificationMode.VerifyMin; |
| 38 | + qualifier = ' or more times'; |
| 39 | + } |
| 40 | + else if (verificationMode.VerifyMax != null && verificationMode.VerifyMax < methodCount) |
| 41 | + { |
| 42 | + expectedCount = verificationMode.VerifyMax; |
| 43 | + qualifier = ' or fewer times'; |
| 44 | + } |
| 45 | + |
| 46 | + if (expectedCount != null) |
| 47 | + { |
| 48 | + throwException(qm, '', expectedCount, qualifier, methodCount, verificationMode.CustomAssertMessage, expectedArguments, expectedMatchers, actualArguments); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private Integer getMethodCount(fflib_MethodArgValues methodArg, List<fflib_IMatcher> matchers, List<fflib_MethodArgValues> methodArgs) |
| 53 | + { |
| 54 | + Integer retval = 0; |
| 55 | + |
| 56 | + if (methodArgs != null) |
| 57 | + { |
| 58 | + if (matchers != null) |
| 59 | + { |
| 60 | + for (fflib_MethodArgValues args : methodArgs) |
| 61 | + { |
| 62 | + if (fflib_Match.matchesAllArgs(args, matchers)) |
| 63 | + { |
| 64 | + capture(matchers); |
| 65 | + retval ++; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + return countCalls(methodArgs, methodArg); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return retval; |
| 76 | + } |
| 77 | + |
| 78 | + private Integer countCalls(List<fflib_MethodArgValues> methodArgs, fflib_MethodArgValues methodArg) |
| 79 | + { |
| 80 | + Integer count = 0; |
| 81 | + |
| 82 | + for(fflib_MethodArgValues arg: methodArgs) |
| 83 | + { |
| 84 | + if( arg == methodArg) count++; |
| 85 | + } |
| 86 | + |
| 87 | + return count; |
| 88 | + } |
| 89 | + |
| 90 | + /* |
| 91 | + * Method that validate the verification mode used in the verify. |
| 92 | + * Not all the methods from the fflib_VerificationMode are implemented for the different classes that extends the fflib_MethodVerifier. |
| 93 | + * The error is thrown at run time, so this method is called in the method that actually performs the verify. |
| 94 | + * @param verificationMode The verification mode that have to been verified. |
| 95 | + * @throws Exception with message for the fflib_VerificationMode not implemented. |
| 96 | + */ |
| 97 | + protected override void validateMode(fflib_VerificationMode verificationMode) |
| 98 | + { |
| 99 | + if(verificationMode.Method == fflib_VerificationMode.ModeName.CALLS) |
| 100 | + { |
| 101 | + throw new fflib_ApexMocks.ApexMocksException( |
| 102 | + 'The calls() method is available only in the InOrder Verification.'); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments