1/* 2 * Copyright (c) 2025 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 16import { StaticSpecIF } from '../../interface'; 17import { Core } from '../../core'; 18import { SpecService } from './SpecService'; 19import DataDriver from '../config/DataDriver'; 20import { getFunctionArgumentsCount } from '../../util'; 21import { AnyType } from '../types/common'; 22class StaticSpec { 23 public description: string; 24 public fi: int; 25 public fn: () => void; 26 public asyncFn: (() => Promise<void>) | ((params: AnyType) => Promise<void>) | null; 27 public isSkip: boolean; 28 public skipReason: string; 29 public fail: Error | null; 30 public pass: boolean; 31 public isExecuted: boolean; 32 public error: Error | null; 33 public expectMsg: string; 34 public duration: number; 35 public startTime: number; 36 constructor(attrs: StaticSpecIF) { 37 this.description = attrs.description; 38 this.fi = attrs.fi; 39 this.fail = null; 40 this.pass = true; 41 this.error = null; 42 this.duration = 0; 43 this.startTime = 0; 44 this.isExecuted = false; // 当前用例是否执行 45 this.isSkip = false; 46 this.skipReason = ''; 47 this.expectMsg = ''; 48 const fn = attrs.fn; 49 if (fn) { 50 this.fn = fn; 51 } else { 52 this.fn = () => {}; 53 } 54 const asyncFn = attrs.asyncFn; 55 if (asyncFn) { 56 this.asyncFn = asyncFn; 57 } else { 58 this.asyncFn = null; 59 } 60 } 61 62 setResult() { 63 if (this.fail) { 64 this.pass = false; 65 } else { 66 this.pass = true; 67 } 68 } 69 70 run(coreContext: Core) { 71 const sService = coreContext.getDefaultService('spec'); 72 73 coreContext.fireEvents('spec', 'specStart'); 74 if (sService !== null) { 75 const specService = sService as SpecService; 76 specService.setCurrentRunningSpec(this); 77 this.isExecuted = true; 78 try { 79 this.fn(); 80 this.setResult(); 81 } catch (e: Error) { 82 this.error = e; 83 specService.setStatus(true); 84 } 85 } 86 coreContext.fireEvents('spec', 'specDone'); 87 } 88 89 async asyncRun(coreContext: Core) { 90 const dataDriverMap = coreContext.getServices('dataDriver'); 91 if(dataDriverMap) { 92 const dataDriver = dataDriverMap.get('dataDriver') as DataDriver; 93 const suiteParams = dataDriver.getSuiteParams(); 94 const specParams = dataDriver.getSpecParams(); 95 const funcStr = Type.of(this.asyncFn as object).getLiteral(); 96 const argsCount = getFunctionArgumentsCount(funcStr) 97 if(argsCount === 1) { 98 if(specParams.length === 0) { 99 await (this.asyncFn as ((params: AnyType) => Promise<void>))(suiteParams); 100 } else { 101 const tempParams: Record<string, AnyType> = suiteParams 102 for(const paraItem of specParams) { 103 if(!paraItem) { 104 await (this.asyncFn as ((params: AnyType) => Promise<void>))(tempParams); 105 } else { 106 for(const entries of paraItem.entries()) { 107 if(!entries) { 108 continue 109 } else { 110 const key = entries[0] 111 const value = entries[1] 112 tempParams[key] = value 113 } 114 } 115 await (this.asyncFn as ((params: AnyType) => Promise<void>))(tempParams); 116 } 117 } 118 } 119 120 } else { 121 await (this.asyncFn as (() => Promise<void>))(); 122 } 123 } else { 124 const asyncFn = this.asyncFn; 125 const fn = this.fn; 126 if (asyncFn !== null) { 127 await (asyncFn as (() => Promise<void>))(); 128 } else if (fn !== null) { 129 fn(); 130 } 131 } 132 133 this.isExecuted = true; 134 } 135 136 filterCheck(coreContext: Core) { 137 const sService = coreContext.getDefaultService('spec'); 138 if (sService !== null) { 139 const specService = sService as SpecService; 140 specService.setCurrentRunningSpec(this); 141 } 142 return true; 143 } 144} 145 146export { StaticSpec }; 147