• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 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
16
17import { describe, it, expect } from '@ohos/hypium';
18import { test2021_05_ans } from './test2021_05'
19
20export default function es2021Test() {
21  describe('ES2021Test', () => {
22    /**
23     * @tc.number    : SUB_COMMONLIBRARY_ETSUTILS_ES2021NewFeatures_0001
24     * @tc.name      : testReplaceAll
25     * @tc.desc      : add name of task for taskpool
26     * @tc.size      : MediumTest
27     * @tc.type      : Function
28     * @tc.level     : Level 0
29     */
30    it('testReplaceAll', 0, () => {
31      expect('abbbaab'.replaceAll('b', 'x')).assertEqual("axxxaax");
32      expect('abbbaab'.replaceAll(/b/g, 'x')).assertEqual("axxxaax");
33    })
34    /**
35     * @tc.number    : SUB_COMMONLIBRARY_ETSUTILS_ES2021NewFeatures_0002
36     * @tc.name      : testPromise.any Success
37     * @tc.desc      : add name of task for taskpool
38     * @tc.size      : MediumTest
39     * @tc.type      : Function
40     * @tc.level     : Level 0
41     */
42    it('testPromise.any Success', 0, () => {
43      const Promise1: Promise<string> = new Promise((resolve, reject) => {
44        setTimeout(() => resolve('Promise 1'), 100)
45      });
46      const Promise2: Promise<string> = new Promise((resolve, reject) => {
47        setTimeout(() => reject('Promise 2 error'), 200)
48      });
49      const Promise3: Promise<string> = new Promise((resolve, reject) => {
50        setTimeout(() => reject('Promise 3 error'), 300)
51      });
52
53      Promise.any([Promise1, Promise2, Promise3])
54        .then((value: string) => {
55          expect(value).assertEqual("Promise 1")
56        })
57        .catch((error: string) => {
58        })
59    })
60    /**
61     * @tc.number    : SUB_COMMONLIBRARY_ETSUTILS_ES2021NewFeatures_0003
62     * @tc.name      : testPromise.any Error
63     * @tc.desc      : add name of task for taskpool
64     * @tc.size      : MediumTest
65     * @tc.type      : Function
66     * @tc.level     : Level 0
67     */
68    it('testPromise.any Error', 0, () => {
69      const Promise1: Promise<string> = new Promise((resolve, reject) => {
70        setTimeout(() => reject('Promise 1 error'), 100)
71      });
72      const Promise2: Promise<string> = new Promise((resolve, reject) => {
73        setTimeout(() => reject('Promise 2 error'), 200)
74      });
75      const Promise3: Promise<string> = new Promise((resolve, reject) => {
76        setTimeout(() => reject('Promise 3 error'), 300)
77      });
78
79      Promise.any([Promise1, Promise2, Promise3])
80        .then((value: string) => {
81          expect(value).assertEqual("Promise 1")
82        })
83        .catch((error: AggregateError) => {
84          expect(error.errors).assertDeepEquals(["Promise 1 error", "Promise 2 error", "Promise 3 error"])
85        })
86    })
87    /**
88     * @tc.number    : SUB_COMMONLIBRARY_ETSUTILS_ES2021NewFeatures_0004
89     * @tc.name      : testLogicalAssignment
90     * @tc.desc      : add name of task for taskpool
91     * @tc.size      : MediumTest
92     * @tc.type      : Function
93     * @tc.level     : Level 0
94     */
95    it('testLogicalAssignment', 0, () => {
96      let a = 0;
97      let b = 10;
98      a &&= b;
99      expect(a).assertEqual(0);
100      a ||= b;
101      expect(a).assertEqual(10);
102      let c: string | null = null;
103      let d = "hello";
104      c ??= d;
105      expect(c).assertEqual("hello")
106    })
107    /**
108     * @tc.number    : SUB_COMMONLIBRARY_ETSUTILS_ES2021NewFeatures_0005
109     * @tc.name      : testNumberSeparator
110     * @tc.desc      : add name of task for taskpool
111     * @tc.size      : MediumTest
112     * @tc.type      : Function
113     * @tc.level     : Level 0
114     */
115    it('testNumberSeparator', 0, () => {
116      expect(1_1).assertEqual(11)
117      expect(1_1n).assertEqual(11n)
118    })
119    /**
120     * @tc.number    : SUB_COMMONLIBRARY_ETSUTILS_ES2021NewFeatures_0006
121     * @tc.name      : testWeakRef
122     * @tc.desc      : add name of task for taskpool
123     * @tc.size      : MediumTest
124     * @tc.type      : Function
125     * @tc.level     : Level 0
126     */
127    it('testWeakRef', 0, () => {
128      expect(test2021_05_ans).assertEqual("21")
129    })
130  })
131}