• 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 { ServiceAttrIF, AssertMatcherIF, ApiIF } from '../../interface';
16import { Core } from '../../core';
17import { assertNull } from './assertNull';
18import { assertClose } from './assertClose';
19import { assertContain } from './assertContain';
20import { assertLess } from './assertLess';
21import { assertLarger } from './assertLarger';
22import { assertFail } from './assertFail';
23import { assertUndefined } from './assertUndefined';
24import { assertFalseFn } from './assertFalse';
25import { assertTrueFn } from './assertTrue';
26import { assertEqual } from './assertEqual';
27import { assertInstanceOf } from './assertInstanceOf';
28import { assertThrowError } from './assertThrowError';
29import { assertLargerOrEqual } from './assertLargerOrEqual';
30import { assertLessOrEqual } from './assertLessOrEqual';
31import { assertNaN } from './assertNaN';
32import { assertNegUnlimited } from './assertNegUnlimited';
33import { assertPosUnlimited } from './assertPosUnlimited';
34import { assertDeepEquals } from './deepEquals/assertDeepEquals';
35import { assertPromiseIsPending } from './assertPromiseIsPending';
36import { assertPromiseIsRejected } from './assertPromiseIsRejected';
37import { assertPromiseIsRejectedWith } from './assertPromiseIsRejectedWith';
38import { assertPromiseIsRejectedWithError } from './assertPromiseIsRejectedWithError';
39import { assertPromiseIsResolved } from './assertPromiseIsResolved';
40import { assertPromiseIsResolvedWith } from './assertPromiseIsResolvedWith';
41import { AnyType } from '../types/common';
42import { ExpectService } from '../service/ExpectService';
43import { assertThrow } from './assertThrow';
44
45class ExpectExtend {
46  public id: string;
47  public matchers: AssertMatcherIF | null;
48  public coreContext: Core | null;
49  constructor(attr: ServiceAttrIF) {
50    this.id = attr.id;
51    this.matchers = null;
52    this.coreContext = null;
53  }
54
55  extendsMatchers() {
56    const matcher: AssertMatcherIF = {
57      assertClose,
58      assertFail,
59      assertContain,
60      assertTrue: assertTrueFn,
61      assertFalse: assertFalseFn,
62      assertInstanceOf,
63      assertLarger,
64      assertLess,
65      assertNull,
66      assertUndefined,
67      assertLargerOrEqual,
68      assertLessOrEqual,
69      assertNaN,
70      assertNegUnlimited,
71      assertPosUnlimited,
72      assertEqual,
73      assertDeepEquals,
74      assertThrow,
75      assertThrowError,
76      assertPromiseIsPending,
77      assertPromiseIsRejected,
78      assertPromiseIsRejectedWith,
79      assertPromiseIsRejectedWithError,
80      assertPromiseIsResolved,
81      assertPromiseIsResolvedWith,
82    };
83    this.matchers = matcher;
84  }
85
86  init(coreContext: Core) {
87    this.coreContext = coreContext;
88    this.extendsMatchers();
89    const expectService = coreContext.getDefaultService('expect');
90    if (expectService !== null) {
91      const matchers = this.matchers;
92      if (matchers) {
93        (expectService as ExpectService).addMatchers(matchers);
94      }
95    }
96  }
97
98  apis(): ApiIF {
99    return {
100      name: 'ExpoctExtend',
101      expect: (actualValue?: AnyType) => {
102        const core = this.coreContext;
103        if (core) {
104          const coreContext = core as Core;
105          const expect = coreContext.getDefaultService('expect');
106          if (expect) {
107            return (expect as ExpectService).expect(actualValue);
108          }
109        }
110      },
111    };
112  }
113}
114export { ExpectExtend };
115