73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
import {JsonResource, Resource} from '@avocado/resource';
|
|
import {expect} from 'chai';
|
|
|
|
Resource.root = 'test/fixtures';
|
|
JsonResource.root = 'test/fixtures';
|
|
|
|
class ResourceSubclass extends Resource {
|
|
async load(buffer) {
|
|
await super.load(buffer);
|
|
this.buffer = buffer;
|
|
}
|
|
}
|
|
|
|
class JsonResourceSubclass extends JsonResource {
|
|
async load(json) {
|
|
await super.load(json);
|
|
this.json = json;
|
|
}
|
|
}
|
|
|
|
const testJsonBuffer = Buffer.from([
|
|
0x7b, 0x0a, 0x20, 0x20, 0x22, 0x66, 0x6f, 0x6f,
|
|
0x22, 0x3a, 0x20, 0x22, 0x62, 0x61, 0x72, 0x22,
|
|
0x2c, 0x0a, 0x20, 0x20, 0x22, 0x62, 0x61, 0x7a,
|
|
0x22, 0x3a, 0x20, 0x36, 0x39, 0x0a, 0x7d, 0x0a,
|
|
]);
|
|
|
|
describe('Resource', () => {
|
|
it('can read resources', async () => {
|
|
const buffer = await Resource.read('/test.json');
|
|
expect(Buffer.compare(testJsonBuffer, Buffer.from(buffer))).to.equal(0);
|
|
});
|
|
it('generates unique uuids', async () => {
|
|
const resources = [
|
|
new Resource(),
|
|
new Resource(),
|
|
];
|
|
expect(resources[0].instanceUuid).to.not.equal(resources[1].instanceUuid);
|
|
});
|
|
it('can be subclassed', async () => {
|
|
const resource = await ResourceSubclass.load('/test.json');
|
|
expect(Buffer.compare(testJsonBuffer, Buffer.from(resource.buffer))).to.equal(0);
|
|
});
|
|
});
|
|
describe('JsonResource', () => {
|
|
it('can read resources', async () => {
|
|
const json = await JsonResource.read('/test.json');
|
|
expect(json.foo).to.equal('bar');
|
|
expect(json.baz).to.equal(69);
|
|
});
|
|
it('can read extended resources', async () => {
|
|
const json = await JsonResource.read('/test2.json');
|
|
expect(json.foo).to.equal('baz');
|
|
expect(json.baz).to.equal(69);
|
|
});
|
|
it('can be subclassed', async () => {
|
|
const resource = await JsonResourceSubclass.load({extends: '/test.json'});
|
|
expect(resource.json.foo).to.equal('bar');
|
|
expect(resource.json.baz).to.equal(69);
|
|
});
|
|
it('can load flat', async () => {
|
|
const resource = await JsonResourceSubclass.load({foo: 420, bar: 'hoo'});
|
|
expect(resource.json.foo).to.equal(420);
|
|
expect(resource.json.bar).to.equal('hoo');
|
|
});
|
|
it('can load to instance', async () => {
|
|
const resource = new JsonResourceSubclass();
|
|
await resource.load({foo: 420, bar: 'hoo'});
|
|
expect(resource.json.foo).to.equal(420);
|
|
expect(resource.json.bar).to.equal('hoo');
|
|
});
|
|
});
|