mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Unit test on retrieving class id and class name (#10559)
This commit is contained in:
committed by
GitHub
parent
5866b04333
commit
b1271e6370
@@ -1,6 +1,9 @@
|
||||
import { ccclass } from 'cc.decorator';
|
||||
import { warnID } from '../../cocos/core';
|
||||
import { float, property } from '../../cocos/core/data/class-decorator';
|
||||
import * as requiringFrame from '../../cocos/core/data/utils/requiring-frame';
|
||||
import { getClassName, unregisterClass, _getClassId } from '../../cocos/core/utils/js-typed';
|
||||
import { Component } from '../../cocos/core/components/component'
|
||||
|
||||
/**
|
||||
* Happened when:
|
||||
@@ -56,4 +59,80 @@ describe('ccclass warnings', () => {
|
||||
{ @ccclass class _ { @property(C) p; } }
|
||||
expect(warnID).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Class id & class name', () => {
|
||||
type ExpectedNameComeFrom = 'self-name' | 'explicit-name' | 'frame-name';
|
||||
type ExpectedIdComeFrom = 'self-name' | 'explicit-name' | 'frame-uuid' | 'temp-id';
|
||||
|
||||
test.each([
|
||||
[true, true, true, 'explicit-name', 'frame-uuid'], // Usually: in user land, components, with explicit name
|
||||
[true, true, false, 'explicit-name', 'explicit-name'], // Usually: in user land, non-components, with explicit name
|
||||
[true, false, true, 'explicit-name', 'explicit-name'], // Usually: in engine, components
|
||||
[true, false, false, 'explicit-name', 'explicit-name'], // Usually: in engine, non-components
|
||||
|
||||
[false, true, true, 'frame-name', 'frame-uuid'], // Usually: in user land, components, no explicit name
|
||||
[false, true, false, 'self-name', 'temp-id'], // Usually: in user land, non-components, no explicit name
|
||||
[false, false, true, 'self-name', 'temp-id'],
|
||||
[false, false, false, 'self-name', 'temp-id'],
|
||||
] as Array<[
|
||||
withExplicitName: boolean,
|
||||
withinRequiringFrame: boolean,
|
||||
inheritingFromComponent: boolean,
|
||||
expectedNameComeFrom: ExpectedNameComeFrom,
|
||||
expectedIdComeFrom: ExpectedIdComeFrom,
|
||||
]>)(
|
||||
`WithExplicitName: %j, WithinRequiringFrame: %j, InheritingFromComponent: %j`,
|
||||
(withExplicitName, withinRequiringFrame, inheritingFromComponent, expectedNameComeFrom, expectedIdComeFrom) => {
|
||||
const EXPLICIT_NAME = '__EXPLICIT_NAME__';
|
||||
const FRAME_NAME = '__FRAME_NAME__';
|
||||
const FRAME_UUID = '__FRAME_UUID__';
|
||||
const expectedName = (() => {
|
||||
switch (expectedNameComeFrom) {
|
||||
case 'frame-name': return FRAME_NAME;
|
||||
case 'explicit-name': return EXPLICIT_NAME;
|
||||
default: case 'self-name': return 'Cls';
|
||||
}
|
||||
})();
|
||||
const expectedId = (() => {
|
||||
switch (expectedIdComeFrom) {
|
||||
case 'frame-uuid': return FRAME_UUID;
|
||||
case 'explicit-name': return EXPLICIT_NAME;
|
||||
case 'temp-id': return expect.stringMatching(/^TmpCId\.\.[0-9]+/i);
|
||||
default: case 'self-name': return 'Cls';
|
||||
}
|
||||
})();
|
||||
|
||||
if (withinRequiringFrame) {
|
||||
requiringFrame.push(
|
||||
{}, // module
|
||||
FRAME_UUID, // uuid
|
||||
FRAME_NAME, // script
|
||||
);
|
||||
}
|
||||
|
||||
let Cls_: Function;
|
||||
if (inheritingFromComponent) {
|
||||
Cls_ = (() => {
|
||||
@(withExplicitName ? ccclass(EXPLICIT_NAME) : ccclass) class Cls extends Component {}
|
||||
return Cls;
|
||||
})();
|
||||
} else {
|
||||
Cls_ = (() => {
|
||||
@(withExplicitName ? ccclass(EXPLICIT_NAME) : ccclass) class Cls {}
|
||||
return Cls;
|
||||
})();
|
||||
}
|
||||
|
||||
if (withinRequiringFrame) {
|
||||
requiringFrame.pop();
|
||||
}
|
||||
|
||||
try {
|
||||
expect(getClassName(Cls_)).toBe(expectedName);
|
||||
expect(_getClassId(Cls_)).toStrictEqual(expectedId);
|
||||
} finally {
|
||||
unregisterClass(Cls_);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user