Skip to content

Commit

Permalink
✅ Add class descendants & pledge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Mar 22, 2024
1 parent b4d21bb commit d97f704
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/function_inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ defClassMethod(function getDescendantsDict(constructor) {
}

namespace = child.namespace;

// Add extra identifier info if this child comes from another namespace
if (namespace != parent_namespace) {
if (namespace.startsWith(parent_namespace)) {
Expand Down
53 changes: 53 additions & 0 deletions test/function_inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,59 @@ describe('Inheritance', function() {
});
});

describe('#getDescendants()', () => {
it('should return all the descendants of a class as an array', () => {

const RootRole = Function.inherits('Informer', 'RootRole');

RootRole.postInherit(function afterInherit() {
// A type_name or type_path is required,
// and this is actually not (yet) automatically set by Protoblast
this.type_name = this.name.underscore();
});

const RootRoleChild = Function.inherits('RootRole', 'RootRoleChild');
const RootRoleSecondChild = Function.inherits('RootRole', 'RootRoleSecondChild');
const RootRoleGrandChild = Function.inherits('RootRoleChild', 'RootRoleGrandChild');

let children = RootRole.getDescendants();
assert.strictEqual(children.length, 3);
});

it('should do the same for classes in a namespace', () => {

const NsRole = Function.inherits('RootRole', 'Nested', 'NestedRole');
const NestedChild = Function.inherits('Nested.NestedRole', 'NestedChild');
const NestedGrandChild = Function.inherits('Nested.NestedChild', 'NestedGrandChild');

let children = NsRole.getDescendants();
assert.strictEqual(children.length, 2);

let root_children = Blast.Classes.RootRole.getDescendants();
assert.strictEqual(root_children.length, 6);
});
});

describe('#getDescendantsDict()', () => {
it('should return all the descendants in an Object dictionary', () => {

let children = Blast.Classes.RootRole.getDescendantsDict();

let keys = Object.keys(children);

assert.strictEqual(keys.length, 6);

assert.deepStrictEqual(keys, [
'root_role_child',
'root_role_grand_child',
'root_role_second_child',
'nested.nested_role',
'nested.nested_child',
'nested.nested_grand_child'
]);
});
});

describe('#setProperty(getter)', function() {

var Alpha,
Expand Down
60 changes: 60 additions & 0 deletions test/pledge.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,66 @@ describe('Swift', function() {

assert.strictEqual(result, 42);
});

it('should always return errors using a pledge', async () => {

let result = Pledge.Swift.waterfall(
47,
val => {
throw new Error('Sync error!');
}
);

assert.strictEqual(result instanceof Pledge, true);

let error;

try {
await result;
} catch (err) {
error = err;
}

assert.strictEqual(error.message, 'Sync error!');

result = Pledge.Swift.waterfall(
47,
val => {
let pledge = new Pledge.Swift();
pledge.reject(new Error('Sync rejection!'));
return pledge;
}
);

assert.strictEqual(result instanceof Pledge, true);

try {
await result;
} catch (err) {
error = err;
}

assert.strictEqual(error.message, 'Sync rejection!');

result = Pledge.Swift.waterfall(
47,
val => {
let pledge = new Pledge.Swift();
setTimeout(() => pledge.reject(new Error('Async rejection!')), 1);
return pledge;
}
);

assert.strictEqual(result instanceof Pledge, true);

try {
await result;
} catch (err) {
error = err;
}

assert.strictEqual(error.message, 'Async rejection!');
});
});

describe('#cancel()', () => {
Expand Down

0 comments on commit d97f704

Please sign in to comment.