• 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 */
15
16const etsVm = globalThis.gtest.etsVm;
17
18const rangeErr = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').rangeErr;
19const referenceErr = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').referenceErr;
20const syntaxErr = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').syntaxErr;
21const uriErr = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').uriErr;
22const typeErr = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').typeErr;
23const customRangeError = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').customRangeError;
24const customReferenceError = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').customReferenceError;
25const customSyntaxError = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').customSyntaxError;
26const customURIError = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').customURIError;
27const customTypeError = etsVm.getClass('Lbuiltinerror/ETSGLOBAL;').customTypeError;
28
29let GetThrowRangeError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetThrowRangeError');
30let GetThrowReferenceError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetThrowReferenceError');
31let GetThrowSyntaxError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetThrowSyntaxError');
32let GetThrowURIError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetThrowURIError');
33let GetThrowTypeError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetThrowTypeError');
34
35let GetRangeError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetRangeError');
36let GetReferenceError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetReferenceError');
37let GetSyntaxError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetSyntaxError');
38let GetURIError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetURIError');
39let GetTypeError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetTypeError');
40
41let GetCustomThrowRangeError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetCustomThrowRangeError');
42let GetCustomThrowReferenceError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetCustomThrowReferenceError');
43let GetCustomThrowSyntaxError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetCustomThrowSyntaxError');
44let GetCustomThrowURIError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetCustomThrowURIError');
45let GetCustomThrowTypeError = etsVm.getFunction('Lbuiltinerror/ETSGLOBAL;', 'GetCustomThrowTypeError');
46
47const SYNTAX_MESSAGE = 'Builtin Error';
48const CUSTOM_SYNTAX_MESSAGE = 'cunstom Error!';
49
50function testCheckInstanceofRangeError(): boolean {
51    let res: boolean;
52    let ranError: Object = GetRangeError(SYNTAX_MESSAGE);
53    let refError: Object = GetReferenceError(SYNTAX_MESSAGE);
54    let synError: Object = GetSyntaxError(SYNTAX_MESSAGE);
55    let uriError: Object = GetURIError(SYNTAX_MESSAGE);
56    let typeError: Object = GetTypeError(SYNTAX_MESSAGE);
57    res = (rangeErr instanceof RangeError) &&
58        (referenceErr instanceof ReferenceError) &&
59        (syntaxErr instanceof SyntaxError) &&
60        (uriErr instanceof URIError) &&
61        (typeErr instanceof TypeError) &&
62        (ranError instanceof RangeError) &&
63        (refError instanceof ReferenceError) &&
64        (synError instanceof SyntaxError) &&
65        (uriError instanceof URIError) &&
66        (typeError instanceof TypeError) &&
67        (customRangeError instanceof RangeError) &&
68        (customReferenceError instanceof ReferenceError) &&
69        (customSyntaxError instanceof SyntaxError) &&
70        (customURIError instanceof URIError) &&
71        (customTypeError instanceof TypeError);
72    return res;
73}
74
75function testCheckCatchError(): boolean {
76    let res: boolean;
77    try {
78        GetThrowRangeError();
79        res = false;
80    } catch (e) {
81        res = (e.toString() === new RangeError().toString()) &&
82            (e.message === new RangeError().message);
83    }
84    try {
85        GetThrowReferenceError();
86        res = false;
87    } catch (e) {
88        res = res && (e.toString() === new ReferenceError().toString()) &&
89            (e.message === new ReferenceError().message);
90    }
91    try {
92        GetThrowSyntaxError();
93        res = false;
94    } catch (e) {
95        res = res && (e.toString() === new SyntaxError().toString()) &&
96            (e.message === new SyntaxError().message);
97    }
98    try {
99        GetThrowURIError();
100        res = false;
101    } catch (e) {
102        res = res && (e.toString() === new URIError().toString()) &&
103            (e.message === new URIError().message);
104    }
105    try {
106        GetThrowTypeError();
107        res = false;
108    } catch (e) {
109        res = res && (e.toString() === new TypeError().toString()) &&
110            (e.message === new TypeError().message);
111    }
112    return res;
113}
114
115function testCheckCustomError(): boolean {
116    let res: boolean;
117    try {
118        GetCustomThrowRangeError(CUSTOM_SYNTAX_MESSAGE);
119        res = false;
120    } catch (err) {
121        res = (err instanceof RangeError) &&
122            (err.toString() === new RangeError(CUSTOM_SYNTAX_MESSAGE).toString()) &&
123            (err.message === CUSTOM_SYNTAX_MESSAGE);
124    }
125    try {
126        GetCustomThrowReferenceError(CUSTOM_SYNTAX_MESSAGE);
127        res = false;
128    } catch (err) {
129        res = res && (err instanceof ReferenceError) &&
130            (err.toString() === new ReferenceError(CUSTOM_SYNTAX_MESSAGE).toString()) &&
131            (err.message === CUSTOM_SYNTAX_MESSAGE);
132    }
133    try {
134        GetCustomThrowSyntaxError(CUSTOM_SYNTAX_MESSAGE);
135        res = false;
136    } catch (err) {
137        res = res && (err instanceof SyntaxError) &&
138            (err.toString() === new SyntaxError(CUSTOM_SYNTAX_MESSAGE).toString()) &&
139            (err.message === CUSTOM_SYNTAX_MESSAGE);
140    }
141    try {
142        GetCustomThrowURIError(CUSTOM_SYNTAX_MESSAGE);
143        res = false;
144    } catch(err) {
145        res = res && (err instanceof URIError) &&
146            (err.toString() === new URIError(CUSTOM_SYNTAX_MESSAGE).toString()) &&
147            (err.message === CUSTOM_SYNTAX_MESSAGE);
148    }
149    try {
150        GetCustomThrowTypeError(CUSTOM_SYNTAX_MESSAGE);
151        res = false;
152    } catch(err) {
153        res = res && (err instanceof TypeError) &&
154            (err.toString() === new TypeError(CUSTOM_SYNTAX_MESSAGE).toString()) &&
155            (err.message === CUSTOM_SYNTAX_MESSAGE);
156    }
157    return res;
158}
159
160ASSERT_TRUE(testCheckInstanceofRangeError());
161ASSERT_TRUE(testCheckCatchError());
162ASSERT_TRUE(testCheckCustomError());
163