1/* 2 * Copyright (c) 2022-2024 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 54 if (typeof arg === 'string' && !regex) { 55 return this.ANY_STRING; 56 } 57 58 if (typeof arg === 'boolean' && !regex) { 59 return this.ANY_BOOLEAN; 60 } 61 62 if (typeof arg === 'number' && !regex) { 63 return this.ANY_NUMBER; 64 } 65 66 if (typeof arg === 'object' && !regex) { 67 return this.ANY_OBJECT; 68 } 69 70 if (typeof arg === 'function' && !regex) { 71 return this.ANY_FUNCTION; 72 } 73 74 if (typeof arg === 'string' && regex) { 75 return regex.test(arg); 76 } 77 return null; 78 } 79 matcheStubKey(key) { 80 if (key === ArgumentMatchers.any) { 81 return this.ANY; 82 } 83 if (key === ArgumentMatchers.anyString) { 84 return this.ANY_STRING; 85 } 86 if (key === ArgumentMatchers.anyBoolean) { 87 return this.ANY_BOOLEAN; 88 } 89 if (key === ArgumentMatchers.anyNumber) { 90 return this.ANY_NUMBER; 91 } 92 if (key === ArgumentMatchers.anyObj) { 93 return this.ANY_OBJECT; 94 } 95 if (key === ArgumentMatchers.anyFunction) { 96 return this.ANY_FUNCTION; 97 } 98 if (ArgumentMatchers.isRegExp(key)) { 99 return key; 100 } 101 return null; 102 } 103} 104