1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16class ArgumentMatchers { 17 ANY = "<any>"; 18 ANY_STRING = "<any String>"; 19 ANY_BOOLEAN = "<any Boolean>"; 20 ANY_NUMBER = "<any Number>"; 21 ANY_OBJECT = "<any Object>"; 22 ANY_FUNCTION = "<any Function>"; 23 MATCH_REGEXS = "<match regexs>"; 24 25 static any() { 26 } 27 28 static anyString() { 29 } 30 31 static anyBoolean() { 32 } 33 34 static anyNumber() { 35 } 36 37 static anyObj() { 38 } 39 40 static anyFunction() { 41 } 42 43 static matchRegexs() { 44 let regex = arguments[0]; 45 if (ArgumentMatchers.isRegExp(regex)) { 46 return regex; 47 } 48 throw Error("not a regex"); 49 } 50 51 static isRegExp(value) { 52 return Object.prototype.toString.call(value) === "[object RegExp]"; 53 } 54 55 matcheReturnKey() { 56 let arg = arguments[0]; 57 let regex = arguments[1]; 58 let stubSetKey = arguments[2]; 59 60 if (stubSetKey && stubSetKey == this.ANY) { 61 return this.ANY; 62 } 63 64 if (typeof arg === "string" && !regex) { 65 return this.ANY_STRING; 66 } 67 68 if (typeof arg === "boolean" && !regex) { 69 return this.ANY_BOOLEAN; 70 } 71 72 if (typeof arg === "number" && !regex) { 73 return this.ANY_NUMBER; 74 } 75 76 if (typeof arg === "object" && !regex) { 77 return this.ANY_OBJECT; 78 } 79 80 if (typeof arg === "function" && !regex) { 81 return this.ANY_FUNCTION; 82 } 83 84 if (typeof arg === "string" && regex) { 85 return regex.test(arg); 86 } 87 88 return null; 89 } 90 91 matcheStubKey() { 92 let key = arguments[0]; 93 94 if (key === ArgumentMatchers.any) { 95 return this.ANY; 96 } 97 98 if (key === ArgumentMatchers.anyString) { 99 return this.ANY_STRING; 100 } 101 if (key === ArgumentMatchers.anyBoolean) { 102 return this.ANY_BOOLEAN; 103 } 104 if (key === ArgumentMatchers.anyNumber) { 105 return this.ANY_NUMBER; 106 } 107 if (key === ArgumentMatchers.anyObj) { 108 return this.ANY_OBJECT; 109 } 110 if (key === ArgumentMatchers.anyFunction) { 111 return this.ANY_FUNCTION; 112 } 113 114 if (ArgumentMatchers.isRegExp(key)) { 115 return key; 116 } 117 118 return null; 119 } 120} 121 122export default ArgumentMatchers;