• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import { Core } from '../../core';
16import { SuiteService } from '../service/SuiteService';
17import { SpecService } from '../service/SpecService';
18import { StaticSpec } from '../service/StaticSpec';
19import {
20  DataDriverData,
21  DataDriverAttr,
22  DataDriverSuite,
23} from '../../interface';
24import { AnyType } from '../types/common';
25
26class DataDriver {
27  public id: string;
28  public coreContext: Core | null;
29  public suiteService: SuiteService | null;
30  public specService: SpecService | null;
31  public data: DataDriverData;
32  constructor(attr: DataDriverAttr) {
33    this.id = 'dataDriver';
34    this.data = attr.data;
35    this.coreContext = null;
36    this.suiteService = null;
37    this.specService = null;
38  }
39
40  init(coreContext: Core) {
41    this.coreContext = coreContext;
42    const suiteService = coreContext.getDefaultService('suite');
43    if (suiteService) {
44      this.suiteService = suiteService as SuiteService;
45    }
46    const specService = coreContext.getDefaultService('spec');
47    if (specService) {
48      this.specService = specService as SpecService;
49    }
50  }
51
52  getSpecParamsValue(specs: DataDriverSuite[]) {
53    let specParams: Record<string, AnyType>[] = [];
54    return specParams;
55  }
56
57  getSpecParams() {
58    let specParams: Record<string, AnyType>[] = [];
59    return specParams;
60  }
61
62  getSuiteParams() {
63    let suiteParams: Record<string, AnyType> = {};
64    const suiteService = this.suiteService;
65    if (suiteService) {
66      const sService = suiteService as SuiteService;
67      let suiteDesc = sService.getCurrentRunningSuite().description;
68      let suites: DataDriverSuite[] = [];
69      const tempSuite = this.data.suites;
70      if (tempSuite) {
71        suites = tempSuite as DataDriverSuite[];
72      }
73      for (const suiteItem of suites) {
74        let describeValue: string[] = [];
75        const tempdescribe = suiteItem.describe;
76        if (tempdescribe) {
77          describeValue = tempdescribe as string[];
78        }
79        if (
80          suiteDesc &&
81          Array.isArray(describeValue) &&
82          (describeValue as Array<string>).includes(suiteDesc)
83        ) {
84          const params = suiteItem.params as Record<string, AnyType>;
85          for (const entries of params.entries()) {
86            if (!entries) {
87              continue;
88            }
89            const key = entries[0];
90            let value = entries[1];
91            if (!key) {
92              continue;
93            }
94            suiteParams[key] = value;
95          }
96        }
97      }
98    }
99    return suiteParams;
100  }
101
102  getStressNum(specs: DataDriverSuite[], specDesc: string): int {
103    let stress = 1;
104    if (specs === null || specs === undefined) {
105      return stress;
106    }
107    for (const specItem of specs) {
108      if (specItem.it && specItem.it === specDesc) {
109        let tempStress = specItem.stress;
110        return tempStress && Number.isInteger(tempStress as number) && (tempStress as int) >= 1
111          ? (tempStress as int)
112          : stress;
113      }
114    }
115    return stress;
116  }
117
118  getSpecStress(specDesc: string): int {
119    let stress = 1;
120    const suiteService = this.suiteService;
121    if (suiteService) {
122      const sService = suiteService as SuiteService;
123      let suiteDesc = sService.getCurrentRunningSuite().description;
124      let suites: DataDriverSuite[] = [];
125      const tempSuite = this.data.suites;
126      if (tempSuite) {
127        suites = tempSuite as DataDriverSuite[];
128      }
129      for (const suiteItem of suites) {
130        const suite = suiteItem as DataDriverSuite;
131        let describeValue: string[] = [];
132        const tempdescribe = suite.describe;
133        if (tempdescribe) {
134          describeValue = tempdescribe as string[];
135        }
136        if (
137          suiteDesc &&
138          Array.isArray(describeValue) &&
139          (describeValue as Array<string>).includes(suiteDesc)
140        ) {
141          let specs: DataDriverSuite[] = [];
142          const tempspecs = suite.items;
143          if (tempspecs) {
144            specs = tempspecs as DataDriverSuite[];
145          }
146          return this.getStressNum(specs, specDesc);
147        }
148      }
149    }
150
151    return stress;
152  }
153
154  getSuiteStress(suiteDesc: string): int {
155    let stress = 1;
156    let suites: DataDriverSuite[] = [];
157    const tempSuite = this.data.suites;
158    if (tempSuite) {
159      suites = tempSuite as DataDriverSuite[];
160    }
161    for (const suiteItem of suites) {
162      const suite = suiteItem as DataDriverSuite;
163      let describeValue: string[] = [];
164      const tempdescribe = suite.describe;
165      if (tempdescribe) {
166        describeValue = tempdescribe as string[];
167      }
168      if (
169        suiteDesc &&
170        Array.isArray(describeValue) &&
171        (describeValue as Array<string>).includes(suiteDesc)
172      ) {
173        let tempStress = suite.stress;
174        return tempStress && Number.isInteger(tempStress as number) && (tempStress as int) >= 1
175          ? (tempStress as int)
176          : stress;
177      }
178    }
179    return stress;
180  }
181}
182
183export default DataDriver;
184