13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport {
173af6ab5fSopenharmony_ci  isViewPUBasedClass,
183af6ab5fSopenharmony_ci  getElementAccessExpressionProperties,
193af6ab5fSopenharmony_ci  stringPropsSet,
203af6ab5fSopenharmony_ci  structPropsSet,
213af6ab5fSopenharmony_ci  getTypeAliasProperties,
223af6ab5fSopenharmony_ci  getInterfaceProperties,
233af6ab5fSopenharmony_ci  getClassProperties,
243af6ab5fSopenharmony_ci  getEnumProperties,
253af6ab5fSopenharmony_ci  getObjectProperties
263af6ab5fSopenharmony_ci} from '../../../src/utils/OhsUtil';
273af6ab5fSopenharmony_ciimport { describe, it } from 'mocha';
283af6ab5fSopenharmony_ciimport { expect } from 'chai';
293af6ab5fSopenharmony_ciimport * as ts from 'typescript';
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_cidescribe('unit test for OhsUtil.ts', function () {
323af6ab5fSopenharmony_ci  describe('test for isViewPUBasedClass function', function () {
333af6ab5fSopenharmony_ci    it('should return false if classNode is an empty object', () => {
343af6ab5fSopenharmony_ci      const classNode = {} as any;
353af6ab5fSopenharmony_ci      expect(isViewPUBasedClass(classNode)).to.be.false;
363af6ab5fSopenharmony_ci    });
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_ci    it('should return false if heritageClauses is undefined', () => {
393af6ab5fSopenharmony_ci      const classNode = ts.factory.createClassDeclaration(undefined, "Class", undefined, undefined, []);
403af6ab5fSopenharmony_ci      expect(isViewPUBasedClass(classNode)).to.be.false;
413af6ab5fSopenharmony_ci    });
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_ci    it('should return false if classNode is undefined', () => {
443af6ab5fSopenharmony_ci      expect(isViewPUBasedClass(undefined)).to.be.false;
453af6ab5fSopenharmony_ci    });
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_ci    it('should return false if heritageClause.types is undefined', () => {
483af6ab5fSopenharmony_ci      const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, undefined);
493af6ab5fSopenharmony_ci      const classNode = ts.factory.createClassDeclaration(undefined, "Class", undefined, [heritageClause], []);
503af6ab5fSopenharmony_ci      expect(isViewPUBasedClass(classNode)).to.be.false;
513af6ab5fSopenharmony_ci    });
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci    it('should return false if classNode.heritageClause.types.member is undefined', () => {
543af6ab5fSopenharmony_ci      const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [undefined]);
553af6ab5fSopenharmony_ci      const classNode = ts.factory.createClassDeclaration(undefined, "Class", undefined, [heritageClause], []);
563af6ab5fSopenharmony_ci      expect(isViewPUBasedClass(classNode)).to.be.false;
573af6ab5fSopenharmony_ci    })
583af6ab5fSopenharmony_ci
593af6ab5fSopenharmony_ci    it('should return true if classNode is set', () => {
603af6ab5fSopenharmony_ci      const typeArguments = [
613af6ab5fSopenharmony_ci        ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
623af6ab5fSopenharmony_ci      ];
633af6ab5fSopenharmony_ci      const type = ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier('ViewPU'), typeArguments);
643af6ab5fSopenharmony_ci      const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [type]);
653af6ab5fSopenharmony_ci      const classNode = ts.factory.createClassDeclaration(undefined, "Class", undefined, [heritageClause], []);
663af6ab5fSopenharmony_ci      expect(isViewPUBasedClass(classNode)).to.be.true;
673af6ab5fSopenharmony_ci    });
683af6ab5fSopenharmony_ci  })
693af6ab5fSopenharmony_ci
703af6ab5fSopenharmony_ci  describe('test for getTypeAliasProperties function', function () {
713af6ab5fSopenharmony_ci    it('should not add items to propertySet if type is undefined', () => {
723af6ab5fSopenharmony_ci      const name = ts.factory.createIdentifier('MyType');
733af6ab5fSopenharmony_ci      const typeAliasNode = ts.factory.createTypeAliasDeclaration(undefined, name, undefined, undefined);
743af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
753af6ab5fSopenharmony_ci      getTypeAliasProperties(typeAliasNode, propertySet);
763af6ab5fSopenharmony_ci      expect(stringPropsSet.size == 0).to.be.true;
773af6ab5fSopenharmony_ci    });
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci    it('should add to propertySet if member.name is set', () => {
803af6ab5fSopenharmony_ci      const name = ts.factory.createIdentifier('MyType');
813af6ab5fSopenharmony_ci      const type = ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
823af6ab5fSopenharmony_ci      const identifierName = ts.factory.createIdentifier('Identifier');
833af6ab5fSopenharmony_ci      const stringName = ts.factory.createStringLiteral('String');
843af6ab5fSopenharmony_ci      const computedPropertyName = ts.factory.createComputedPropertyName(ts.factory.createStringLiteral('3 + 2'));
853af6ab5fSopenharmony_ci      const types = ts.factory.createTypeLiteralNode([
863af6ab5fSopenharmony_ci        ts.factory.createPropertySignature(undefined, undefined, undefined, type),
873af6ab5fSopenharmony_ci        ts.factory.createPropertySignature(undefined, identifierName, undefined, type),
883af6ab5fSopenharmony_ci        ts.factory.createPropertySignature(undefined, stringName, undefined, type),
893af6ab5fSopenharmony_ci        ts.factory.createPropertySignature(undefined, computedPropertyName, undefined, type),
903af6ab5fSopenharmony_ci      ]);
913af6ab5fSopenharmony_ci      const typeAliasNode = ts.factory.createTypeAliasDeclaration(undefined, name, undefined, types);
923af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
933af6ab5fSopenharmony_ci      getTypeAliasProperties(typeAliasNode, propertySet);
943af6ab5fSopenharmony_ci      expect(propertySet.has('Identifier') && propertySet.has('String') && propertySet.has('3 + 2')).to.be.true;
953af6ab5fSopenharmony_ci      expect(stringPropsSet.has('String') && propertySet.has('3 + 2')).to.be.true;
963af6ab5fSopenharmony_ci    });
973af6ab5fSopenharmony_ci  })
983af6ab5fSopenharmony_ci
993af6ab5fSopenharmony_ci  describe('test for getElementAccessExpressionProperties function', function () {
1003af6ab5fSopenharmony_ci    it('should not add items to propertySet if elementAccessExpression is undefined', () => {
1013af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1023af6ab5fSopenharmony_ci      getElementAccessExpressionProperties(undefined, propertySet);
1033af6ab5fSopenharmony_ci      expect(stringPropsSet.has('value')).to.be.false;
1043af6ab5fSopenharmony_ci    });
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci    it('should add to propertySet if elementAccessExpressionNode.argumentExpression is set string value', () => {
1073af6ab5fSopenharmony_ci      const key = ts.factory.createStringLiteral('key');
1083af6ab5fSopenharmony_ci      const value = ts.factory.createStringLiteral('value');
1093af6ab5fSopenharmony_ci      const elementAccessExpression = ts.factory.createElementAccessExpression(key, value);
1103af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1113af6ab5fSopenharmony_ci      getElementAccessExpressionProperties(elementAccessExpression, propertySet);
1123af6ab5fSopenharmony_ci      expect(stringPropsSet.has('value')).to.be.true;
1133af6ab5fSopenharmony_ci    });
1143af6ab5fSopenharmony_ci
1153af6ab5fSopenharmony_ci    it('should not add items to propertySet if elementAccessExpressionNode.argumentExpression is set int value', () => {
1163af6ab5fSopenharmony_ci      const key = ts.factory.createIdentifier('key');
1173af6ab5fSopenharmony_ci      const value = ts.factory.createBigIntLiteral("9999999");
1183af6ab5fSopenharmony_ci      const elementAccessExpression = ts.factory.createElementAccessExpression(key, value);
1193af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1203af6ab5fSopenharmony_ci      getElementAccessExpressionProperties(elementAccessExpression, propertySet)
1213af6ab5fSopenharmony_ci      expect(stringPropsSet.has('9999999')).to.be.false;
1223af6ab5fSopenharmony_ci    });
1233af6ab5fSopenharmony_ci  })
1243af6ab5fSopenharmony_ci
1253af6ab5fSopenharmony_ci  describe('test for getInterfaceProperties function', function () {
1263af6ab5fSopenharmony_ci    it('should not add items to propertySet if interfaceNode is undefined', () => {
1273af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1283af6ab5fSopenharmony_ci      getInterfaceProperties(undefined, propertySet);
1293af6ab5fSopenharmony_ci      expect(stringPropsSet.has('property')).to.be.false;
1303af6ab5fSopenharmony_ci    });
1313af6ab5fSopenharmony_ci
1323af6ab5fSopenharmony_ci    it('should add to propertySet if member.name is set string value', () => {
1333af6ab5fSopenharmony_ci      const members = [
1343af6ab5fSopenharmony_ci        ts.factory.createPropertySignature([], undefined, undefined, undefined),
1353af6ab5fSopenharmony_ci        ts.factory.createPropertySignature([], ts.factory.createStringLiteral('property'), undefined, undefined)
1363af6ab5fSopenharmony_ci      ];
1373af6ab5fSopenharmony_ci      const interfaceNode = ts.factory.createInterfaceDeclaration(undefined, 'Interface', undefined, undefined, members);
1383af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1393af6ab5fSopenharmony_ci      getInterfaceProperties(interfaceNode, propertySet);
1403af6ab5fSopenharmony_ci      expect(stringPropsSet.has('property')).to.be.true;
1413af6ab5fSopenharmony_ci      expect(propertySet.has('property')).to.be.true;
1423af6ab5fSopenharmony_ci    });
1433af6ab5fSopenharmony_ci  })
1443af6ab5fSopenharmony_ci
1453af6ab5fSopenharmony_ci  describe('test for getClassProperties function', function () {
1463af6ab5fSopenharmony_ci    it('should not add items to propertySet if classNode is undefined', () => {
1473af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1483af6ab5fSopenharmony_ci      getClassProperties(undefined, propertySet);
1493af6ab5fSopenharmony_ci      expect(propertySet.size == 0).to.be.true;
1503af6ab5fSopenharmony_ci    });
1513af6ab5fSopenharmony_ci
1523af6ab5fSopenharmony_ci    it('should add to propertySet if classNode is StructDeclaration', () => {
1533af6ab5fSopenharmony_ci      const classProperty = ts.factory.createClassExpression(
1543af6ab5fSopenharmony_ci        undefined,
1553af6ab5fSopenharmony_ci        undefined,
1563af6ab5fSopenharmony_ci        undefined,
1573af6ab5fSopenharmony_ci        undefined,
1583af6ab5fSopenharmony_ci        undefined,
1593af6ab5fSopenharmony_ci        []
1603af6ab5fSopenharmony_ci      );
1613af6ab5fSopenharmony_ci      const parameters = [
1623af6ab5fSopenharmony_ci        ts.factory.createParameterDeclaration(
1633af6ab5fSopenharmony_ci          [ts.factory.createModifier(ts.SyntaxKind.AbstractKeyword)],
1643af6ab5fSopenharmony_ci          undefined,
1653af6ab5fSopenharmony_ci          'parameter',
1663af6ab5fSopenharmony_ci          undefined,
1673af6ab5fSopenharmony_ci          ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
1683af6ab5fSopenharmony_ci          undefined
1693af6ab5fSopenharmony_ci        ),
1703af6ab5fSopenharmony_ci        ts.factory.createParameterDeclaration(
1713af6ab5fSopenharmony_ci          [ts.factory.createModifier(ts.SyntaxKind.PublicKeyword)],
1723af6ab5fSopenharmony_ci          undefined,
1733af6ab5fSopenharmony_ci          'classParameter',
1743af6ab5fSopenharmony_ci          undefined,
1753af6ab5fSopenharmony_ci          ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
1763af6ab5fSopenharmony_ci          classProperty
1773af6ab5fSopenharmony_ci        )
1783af6ab5fSopenharmony_ci      ];
1793af6ab5fSopenharmony_ci      const expression = ts.factory.createBinaryExpression(
1803af6ab5fSopenharmony_ci        ts.factory.createIdentifier('left'),
1813af6ab5fSopenharmony_ci        ts.SyntaxKind.EqualsToken,
1823af6ab5fSopenharmony_ci        ts.factory.createIdentifier('right')
1833af6ab5fSopenharmony_ci      );
1843af6ab5fSopenharmony_ci      const statements = [ts.factory.createExpressionStatement(expression)];
1853af6ab5fSopenharmony_ci      const members = [
1863af6ab5fSopenharmony_ci        undefined,
1873af6ab5fSopenharmony_ci        ts.factory.createPropertyDeclaration(undefined, 'name', undefined, undefined, undefined),
1883af6ab5fSopenharmony_ci        ts.factory.createConstructorDeclaration(
1893af6ab5fSopenharmony_ci          undefined,
1903af6ab5fSopenharmony_ci          undefined,
1913af6ab5fSopenharmony_ci          parameters,
1923af6ab5fSopenharmony_ci          ts.factory.createBlock(statements, true)
1933af6ab5fSopenharmony_ci        )
1943af6ab5fSopenharmony_ci      ];
1953af6ab5fSopenharmony_ci      const structDeclaration = ts.factory.createStructDeclaration(undefined, undefined, undefined, undefined, members);
1963af6ab5fSopenharmony_ci      const propertySet = new Set<string>();
1973af6ab5fSopenharmony_ci      getClassProperties(structDeclaration, propertySet);
1983af6ab5fSopenharmony_ci      expect(structPropsSet.has('name')).to.be.true;
1993af6ab5fSopenharmony_ci      expect(propertySet.has('parameter')).to.be.false;
2003af6ab5fSopenharmony_ci      expect(propertySet.has('classParameter')).to.be.true;
2013af6ab5fSopenharmony_ci    });
2023af6ab5fSopenharmony_ci
2033af6ab5fSopenharmony_ci    describe('test for getEnumProperties function', function () {
2043af6ab5fSopenharmony_ci      it('should not add items to propertySet if enumNode is undefined', () => {
2053af6ab5fSopenharmony_ci        const propertySet = new Set<string>();
2063af6ab5fSopenharmony_ci        getEnumProperties(undefined, propertySet);
2073af6ab5fSopenharmony_ci        expect(propertySet.size === 0).to.be.true;
2083af6ab5fSopenharmony_ci      });
2093af6ab5fSopenharmony_ci
2103af6ab5fSopenharmony_ci      it('should add to propertySet if member.name is set', () => {
2113af6ab5fSopenharmony_ci        const members = [
2123af6ab5fSopenharmony_ci          undefined,
2133af6ab5fSopenharmony_ci          ts.factory.createEnumMember('enumMember', ts.factory.createIdentifier('enumMember')),
2143af6ab5fSopenharmony_ci        ];
2153af6ab5fSopenharmony_ci        const enumNode = ts.factory.createEnumDeclaration(undefined, undefined, 'enum', members);
2163af6ab5fSopenharmony_ci        const propertySet = new Set<string>();
2173af6ab5fSopenharmony_ci        getEnumProperties(enumNode, propertySet);
2183af6ab5fSopenharmony_ci        expect(propertySet.has('enumMember')).to.be.true;
2193af6ab5fSopenharmony_ci      });
2203af6ab5fSopenharmony_ci    })
2213af6ab5fSopenharmony_ci
2223af6ab5fSopenharmony_ci    describe('test for getObjectProperties function', function () {
2233af6ab5fSopenharmony_ci      it('should not add items to propertySet if objNode is undefined', () => {
2243af6ab5fSopenharmony_ci        const propertySet = new Set<string>();
2253af6ab5fSopenharmony_ci        getObjectProperties(undefined, propertySet);
2263af6ab5fSopenharmony_ci        expect(propertySet.size === 0).to.be.true;
2273af6ab5fSopenharmony_ci      });
2283af6ab5fSopenharmony_ci
2293af6ab5fSopenharmony_ci      it('should add to propertySet if property.name is set', () => {
2303af6ab5fSopenharmony_ci        const objProperties = [
2313af6ab5fSopenharmony_ci          ts.factory.createPropertyAssignment('objKey', ts.factory.createStringLiteral('objValue'))
2323af6ab5fSopenharmony_ci        ];
2333af6ab5fSopenharmony_ci        const obj = ts.factory.createObjectLiteralExpression(objProperties);
2343af6ab5fSopenharmony_ci        const properties = [
2353af6ab5fSopenharmony_ci          undefined,
2363af6ab5fSopenharmony_ci          ts.factory.createPropertyAssignment('key', obj),
2373af6ab5fSopenharmony_ci          ts.factory.createShorthandPropertyAssignment('key2', obj)
2383af6ab5fSopenharmony_ci        ];
2393af6ab5fSopenharmony_ci        const objNode = ts.factory.createObjectLiteralExpression(properties, true);
2403af6ab5fSopenharmony_ci        const propertySet = new Set<string>();
2413af6ab5fSopenharmony_ci        getObjectProperties(objNode, propertySet);
2423af6ab5fSopenharmony_ci        expect(propertySet.has('key')).to.be.true;
2433af6ab5fSopenharmony_ci        expect(propertySet.has('objKey')).to.be.true;
2443af6ab5fSopenharmony_ci      });
2453af6ab5fSopenharmony_ci    })
2463af6ab5fSopenharmony_ci  })
2473af6ab5fSopenharmony_ci}); 
248