1/*
2 * Copyright (c) 2021-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 */
15
16class ReportExtend {
17    constructor(fileModule) {
18        this.id = 'extend';
19        this.fileModule = fileModule;
20    }
21
22    init(coreContext) {
23        this.coreContext = coreContext;
24        this.suiteService = this.coreContext.getDefaultService('suite');
25    }
26
27    taskStart() {
28
29    }
30
31    handleSpecs(specs, report, suiteReport, testsuite) {
32        for (let testcase of specs) {
33            report.tests++;
34            suiteReport.tests++;
35            let caseReport = {
36                tag: 'testcase',
37                name: testcase.description,
38                status: 'run',
39                time: '0.0',
40                classname: testsuite.description
41            };
42            if (testcase.error) {
43                caseReport.result = false;
44                caseReport.children = [{
45                    tag: 'error',
46                    type: '',
47                    message: testcase.error.message
48                }];
49                report.errors++;
50                suiteReport.errors++;
51            } else if (testcase.result.failExpects.length > 0) {
52                caseReport.result = false;
53                let message = '';
54                testcase.result.failExpects.forEach(failExpect => {
55                    message += failExpect.message || ('expect ' + failExpect.actualValue + ' ' + failExpect.checkFunc + ' ' + (failExpect.expectValue || '')) + ';';
56                });
57                caseReport.children = [{
58                    tag: 'failure',
59                    type: '',
60                    message: message
61                }];
62                report.failures++;
63                suiteReport.failures++;
64            } else {
65                caseReport.result = true;
66            }
67            suiteReport.children.push(caseReport);
68        }
69    }
70
71    taskDone() {
72        const report = {
73            tag: 'testsuites',
74            name: 'summary_report',
75            timestamp: new Date().toDateString(),
76            time: '1',
77            errors: 0,
78            failures: 0,
79            tests: 0,
80            children: []
81        };
82        const rootSuite = this.suiteService.rootSuite;
83        if (rootSuite && rootSuite.childSuites) {
84            for (let testsuite of rootSuite.childSuites) {
85                let suiteReport = {
86                    tag: 'testsuite',
87                    name: testsuite['description'],
88                    errors: 0,
89                    tests: 0,
90                    failures: 0,
91                    time: '0.1',
92                    children: []
93                };
94                let specs = testsuite['specs'];
95                this.handleSpecs(specs, report, suiteReport, testsuite);
96                report.children.push(suiteReport);
97            }
98        }
99
100        writeXmlReport(report);
101    }
102}
103
104function writeXmlReport(report) {
105    let reportXml = '<?xml version="1.0" encoding="UTF-8"?>\n' + json2xml(report);
106    this.fileModule.writeText({
107        uri: 'internal://app/report.xml',
108        text: reportXml,
109        success: function () {
110            console.info('call success callback success');
111        },
112        fail: function (data, code) {
113            console.info('call fail callback success:');
114        },
115        complete: function () {
116            console.info('call complete callback success');
117        }
118    });
119}
120
121function handleChild(json, key, hasChildren, childrenStr) {
122    if (json[key].length > 0) {
123        hasChildren = true;
124        for (let child of json[key]) {
125            childrenStr += json2xml(child);
126        }
127    }
128}
129
130function json2xml(json) {
131    let tagName;
132    let hasChildren = false;
133    let childrenStr = '';
134    let attrStr = '';
135    for (let key in json) {
136        if (key === 'tag') {
137            tagName = json[key];
138        } else if (key === 'children') {
139            handleChild(json, key, hasChildren, childrenStr);
140        } else {
141            attrStr += ` ${key}="${json[key]}"`;
142        }
143    }
144    let xml = `<${tagName}${attrStr}`;
145    xml += hasChildren ? `>${childrenStr}</${tagName}>` : '/>';
146    return xml;
147}
148
149export default ReportExtend;
150