Skip to content

Commit

Permalink
Cleanup, handle and test undefined cases
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Sep 19, 2017
1 parent 79bb5bc commit a97fb80
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ module.exports = (dirname, manifest) => {
// Resolve arguments

let argGroups = internals.tryToRequire(place);
const dirFiles = (typeof argGroups === 'undefined') && bone.list && internals.isDirectory(place);
const fullPlace = (typeof argGroups !== 'undefined') ? require.resolve(place) : '';
let dirFiles = false;
const fullPlaces = []; // When listed in directory
const useFilenames = []; // When listed in directory
const fullPlaces = [];
const useFilenames = [];

if (typeof argGroups === 'undefined' && bone.list && internals.isDirectory(place)) {
if (dirFiles) {

dirFiles = true;
argGroups = RequireDir(place);
argGroups = Object.keys(argGroups).map((name) => {
const requiredDir = RequireDir(place);

argGroups = Object.keys(requiredDir).map((name) => {

useFilenames.push(bone.useFilename && ((v) => bone.useFilename(name, v)));
fullPlaces.push(require.resolve(Path.join(place, name)));

return argGroups[name];
return requiredDir[name];
});
}

Expand Down Expand Up @@ -76,7 +76,7 @@ module.exports = (dirname, manifest) => {
dirFile: dirFiles,
method: bone.method,
signature: bone.signature,
args: argGroups, // When list is true, this might be a function
args: argGroups,
list: bone.list,
async: bone.async
});
Expand All @@ -89,10 +89,11 @@ module.exports = (dirname, manifest) => {

let args = call.args;

if (typeof args === 'function' && !internals.isClass(args) && !call.inListFunc) {
if (typeof args === 'function' && !internals.isClass(args) && !call.evaluated) {
args = args(instance, options);
if (call.list && !call.dirFile) {
const subCalls = args.map((arg) => Object.assign({}, call, { args: arg, inListFunc: true }));

if (call.list && !call.dirFile && Array.isArray(args)) {
const subCalls = args.map((arg) => Object.assign({}, call, { args: arg, evaluated: true }));
return Items.serial(subCalls, makeCall, nextCall);
}
}
Expand All @@ -101,6 +102,10 @@ module.exports = (dirname, manifest) => {
args = call.useFilename(args);
}

if (typeof args === 'undefined') {
return nextCall();
}

let appliableArgs;

if (call.signature) {
Expand Down
9 changes: 9 additions & 0 deletions test/closet/func-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = (instance, options) => {

instance.insideFunc = 'instance';
options.insideFunc = 'options';

return;
};
1 change: 1 addition & 0 deletions test/closet/list-func-as-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = (instance, options) => {
options.insideFunc = 'options';

return [
undefined, // For good measure, to ensure it's a no-op
{
listOne: 'valueOne'
},
Expand Down
5 changes: 5 additions & 0 deletions test/closet/list-single-as-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
listOne: 'valueOne'
};
55 changes: 55 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,33 @@ describe('Haute', () => {
});
});

it('calls with a list item as argument from a plain file.', (done) => {

const calledWith = [];

const instance = {
callThis: function (arg) {

calledWith.push({ arg, length: arguments.length });
}
};

const manifest = [{
method: 'callThis',
place: 'list-single-as-file',
list: true
}];

Haute(dirname, manifest)(instance, {}, (err) => {

expect(err).to.not.exist();
expect(calledWith).to.equal([
{ arg: { listOne: 'valueOne' }, length: 1 }
]);
done();
});
});

it('calls with evaluated function argument in list file.', (done) => {

const calledWith = [];
Expand Down Expand Up @@ -645,6 +672,34 @@ describe('Haute', () => {
});
});

it('does not call with evaluated function undefined in non-list.', (done) => {

let called = false;

const instance = {
callThis: function (arg) {

called = true;
}
};

const options = {};

const manifest = [{
method: 'callThis',
place: 'func-empty'
}];

Haute(dirname, manifest)(instance, options, (err) => {

expect(err).to.not.exist();
expect(called).to.equal(false);
expect(instance.insideFunc).to.equal('instance');
expect(options.insideFunc).to.equal('options');
done();
});
});

it('calls with class argument in non-list.', (done) => {

const calledWith = {};
Expand Down

0 comments on commit a97fb80

Please sign in to comment.