2020-03-30 23:09:36 +00:00
|
|
|
import { expect } from 'chai';
|
2022-10-08 19:10:17 +00:00
|
|
|
|
|
|
|
import { InvalidArgumentError } from '../../src/error/InvalidArgumentError.js';
|
|
|
|
import { constructorName, getConstructor, getMethods } from '../../src/Reflect.js';
|
2020-03-31 02:31:51 +00:00
|
|
|
|
|
|
|
class Test {
|
|
|
|
public foo() { /* noop */ }
|
|
|
|
public bar() { /* noop */ }
|
|
|
|
}
|
2020-03-30 23:09:36 +00:00
|
|
|
|
|
|
|
describe('reflect utils', () => {
|
|
|
|
describe('get methods helper', () => {
|
2022-10-08 19:10:17 +00:00
|
|
|
it('should collect method functions', async () => {
|
2020-03-30 23:09:36 +00:00
|
|
|
const methods = getMethods(new Test()).values();
|
2020-03-31 02:31:51 +00:00
|
|
|
|
2020-03-30 23:09:36 +00:00
|
|
|
/* eslint-disable @typescript-eslint/unbound-method */
|
|
|
|
expect(methods).to.include(Test.prototype.foo);
|
|
|
|
expect(methods).to.include(Test.prototype.bar);
|
|
|
|
});
|
|
|
|
});
|
2020-03-31 02:31:51 +00:00
|
|
|
|
|
|
|
describe('get constructor helper', () => {
|
2022-10-08 19:10:17 +00:00
|
|
|
it('should get the constructor from an instance', async () => {
|
2020-03-31 02:31:51 +00:00
|
|
|
const instance = new Test();
|
|
|
|
expect(getConstructor(instance)).to.equal(Test);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get constructor name helper', () => {
|
2022-10-08 19:10:17 +00:00
|
|
|
it('should get the constructor name from an instance', async () => {
|
2020-03-31 02:31:51 +00:00
|
|
|
const instance = new Test();
|
|
|
|
expect(constructorName(instance)).to.equal(Test.name);
|
|
|
|
});
|
2021-03-27 23:22:36 +00:00
|
|
|
|
2022-10-08 19:10:17 +00:00
|
|
|
it('should throw when value has no prototype', async () => {
|
2021-03-27 23:22:36 +00:00
|
|
|
/* eslint-disable-next-line no-null/no-null */
|
|
|
|
const protoless = Object.create(null, {});
|
|
|
|
expect(() => constructorName(protoless)).to.throw(InvalidArgumentError);
|
|
|
|
});
|
|
|
|
|
|
|
|
xit('should handle nil values');
|
2020-03-31 02:31:51 +00:00
|
|
|
});
|
2020-03-30 23:09:36 +00:00
|
|
|
});
|