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
16import mocha from 'mocha';
17import { isDebug, isFileExist, sortAndDeduplicateStringArr } from '../../../src/initialization/utils';
18import { assert, expect } from 'chai';
19import { DEBUG } from "../../../src/initialization/CommonObject";
20import * as fs from 'fs';
21
22describe('Tester Cases for <utils>.', function () {
23  describe('Tester Cases for <isDebug>.', function () {
24    /** test for isDebug */
25    it('Tester: <Debug Mode> case for isDebug', function () {
26      const projectConfig: any = {
27        buildMode: DEBUG
28      };
29      assert.strictEqual(isDebug(projectConfig), true);
30    });
31  });
32
33  describe('Tester Cases for <isFileExist>.', function () {
34    /** test for isFileExist */
35    it('Tester: <file not exist> case for isFileExist', function () {
36      const path: string = 'test/ut/initialization/testFileNotExiet.txt';
37      assert.strictEqual(isFileExist(path), false);
38    });
39
40    it('Tester: <file exist> case for isFileExist', function () {
41      const path: string = 'test/ut/initialization/demo.txt';
42      fs.writeFileSync(path, 'test');
43      assert.strictEqual(isFileExist(path), true);
44      fs.unlinkSync(path);
45    });
46  });
47
48  describe('Tester Cases for <sortAndDeduplicateStringArr>.', function () {
49    /** test for sortAndDeduplicateStringArr */
50    it('Tester: <the length of arr is 0> case for sortAndDeduplicateStringArr', function () {
51      const arr: string[] = [];
52      assert.strictEqual(sortAndDeduplicateStringArr(arr).length, 0);
53    });
54
55    it('Tester: <sort and deduplicate> case for sortAndDeduplicateStringArr', function () {
56      const arr0: string[] = ['test2', 'test2', 'test1', 'test0'];
57      const arr2: string[] = ['test0', 'test1', 'test2'];
58      let arr1 = sortAndDeduplicateStringArr(arr0);
59      expect(arr1).to.deep.equal(arr2);
60    });
61  });
62});
63