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 */ 15export class ArgumentMatchers { 16 constructor() { 17 this.ANY = "<any>"; 18 this.ANY_STRING = "<any String>"; 19 this.ANY_BOOLEAN = "<any Boolean>"; 20 this.ANY_NUMBER = "<any Number>"; 21 this.ANY_OBJECT = "<any Object>"; 22 this.ANY_FUNCTION = "<any Function>"; 23 this.MATCH_REGEXS = "<match regexs>"; 24 } 25 static any() { 26 } 27 static anyString() { 28 } 29 static anyBoolean() { 30 } 31 static anyNumber() { 32 } 33 static anyObj() { 34 } 35 static anyFunction() { 36 } 37 static matchRegexs(regex) { 38 if (ArgumentMatchers.isRegExp(regex)) { 39 return regex; 40 } 41 throw Error("not a regex"); 42 } 43 static isRegExp(value) { 44 return Object.prototype.toString.call(value) === "[object RegExp]"; 45 } 46 matcheReturnKey(...args) { 47 let arg = args[0]; 48 let regex = args[1]; 49 let stubSetKey = args[2]; 50 if (stubSetKey && stubSetKey == this.ANY) { 51 return this.ANY; 52 } 53 if (typeof arg === "string" && !regex) { 54 return this.ANY_STRING; 55 } 56 if (typeof arg === "boolean" && !regex) { 57 return this.ANY_BOOLEAN; 58 } 59 if (typeof arg === "number" && !regex) { 60 return this.ANY_NUMBER; 61 } 62 if (typeof arg === "object" && !regex) { 63 return this.ANY_OBJECT; 64 } 65 if (typeof arg === "function" && !regex) { 66 return this.ANY_FUNCTION; 67 } 68 if (typeof arg === "string" && regex) { 69 return regex.test(arg); 70 } 71 return null; 72 } 73 matcheStubKey(key) { 74 if (key === ArgumentMatchers.any) { 75 return this.ANY; 76 } 77 if (key === ArgumentMatchers.anyString) { 78 return this.ANY_STRING; 79 } 80 if (key === ArgumentMatchers.anyBoolean) { 81 return this.ANY_BOOLEAN; 82 } 83 if (key === ArgumentMatchers.anyNumber) { 84 return this.ANY_NUMBER; 85 } 86 if (key === ArgumentMatchers.anyObj) { 87 return this.ANY_OBJECT; 88 } 89 if (key === ArgumentMatchers.anyFunction) { 90 return this.ANY_FUNCTION; 91 } 92 if (ArgumentMatchers.isRegExp(key)) { 93 return key; 94 } 95 return null; 96 } 97} 98