mirror of
https://github.com/gundb/panic-server.git
synced 2026-04-15 03:00:16 -04:00
Add callback list parser
Pass in an array of objects, with optional conditional expressions/functions, and get back a list of objects that passed the conditional and parsed properly as functions. It's in the format that the server test configuration constructor will send off to the client, so that instead of being terribly verbose, you can say `response.cbs = parse(response.cbs)` and get a validated list of test objects.
This commit is contained in:
38
client/configuration/parse.js
Normal file
38
client/configuration/parse.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
// provides `Function.parse`
|
||||
require('../../lib/configuration/extensions');
|
||||
|
||||
function condition(obj) {
|
||||
|
||||
// immediate pass
|
||||
if (obj.conditional === undefined) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// parse the conditional
|
||||
var result = Function.parse(obj.conditional);
|
||||
|
||||
// if it's a primitive
|
||||
if (typeof result !== 'function') {
|
||||
return Boolean(result);
|
||||
}
|
||||
|
||||
// it's a function
|
||||
return result();
|
||||
}
|
||||
|
||||
module.exports = function (array) {
|
||||
if (!array) {
|
||||
return [];
|
||||
}
|
||||
return array.filter(condition).map(function (obj) {
|
||||
// parse the callbacks
|
||||
obj.cb = Function.parse(obj.cb);
|
||||
return obj;
|
||||
}).filter(function (obj) {
|
||||
// filter out non-functions
|
||||
return typeof obj.cb === 'function';
|
||||
});
|
||||
};
|
||||
70
spec/client/configuration/parse.spec.js
Normal file
70
spec/client/configuration/parse.spec.js
Normal file
@@ -0,0 +1,70 @@
|
||||
/*global jasmine, describe, it, expect*/
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
var parse = require('../../../client/configuration/parse');
|
||||
|
||||
function condition(val) {
|
||||
return {
|
||||
conditional: val,
|
||||
cb: 'function () {}'
|
||||
};
|
||||
}
|
||||
|
||||
describe('The client callback parser', function () {
|
||||
it('should be a function', function () {
|
||||
expect(parse).toEqual(jasmine.any(Function));
|
||||
});
|
||||
|
||||
it('should not throw without input', function () {
|
||||
expect(parse).not.toThrow();
|
||||
});
|
||||
|
||||
it('should return an array', function () {
|
||||
var output = parse([]);
|
||||
expect(output).toEqual(jasmine.any(Array));
|
||||
});
|
||||
|
||||
it('should return a parsed objects containing callbacks', function () {
|
||||
var output = parse([
|
||||
condition()
|
||||
]);
|
||||
expect(output[0]).toEqual(jasmine.any(Object));
|
||||
});
|
||||
|
||||
it('should filter against the conditional method', function () {
|
||||
var output = parse([
|
||||
condition('function () { return false }')
|
||||
]);
|
||||
expect(output.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should accept expressions as conditionals', function () {
|
||||
var output = parse([
|
||||
condition(false),
|
||||
condition('false'),
|
||||
condition('typeof true === "boolean"')
|
||||
]);
|
||||
expect(output.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should only return a list of objects', function () {
|
||||
var output = parse([
|
||||
condition(true),
|
||||
condition(false),
|
||||
condition('function () { return true }'),
|
||||
condition(1)
|
||||
]);
|
||||
expect(output.length).toBe(3);
|
||||
output.forEach(function (cb) {
|
||||
expect(cb).toEqual(jasmine.any(Object));
|
||||
});
|
||||
});
|
||||
|
||||
it('should filter out bad input', function () {
|
||||
var output = parse([{
|
||||
cb: 5
|
||||
}]);
|
||||
expect(output.length).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
/*global jasmine, describe, it, expect*/
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
var parser = require('../../../client/framework/parser');
|
||||
|
||||
describe('The client test parser', function () {
|
||||
it('should be a function', function () {
|
||||
expect(parser).toEqual(jasmine.any(Function));
|
||||
});
|
||||
});
|
||||
11
spec/client/framework/runner.spec.js
Normal file
11
spec/client/framework/runner.spec.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/*globals jasmine, describe, it, expect*/
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
var runner = require('../../../client/framework/runner');
|
||||
|
||||
describe('The client test runner', function () {
|
||||
it('should be a function', function () {
|
||||
expect(runner).toEqual(jasmine.any(Function));
|
||||
});
|
||||
});
|
||||
@@ -116,6 +116,12 @@ describe('The extension', function () {
|
||||
var func = JSON.parse(JSON.stringify(function test() {}));
|
||||
Function.parse(func); // shouldn't throw
|
||||
});
|
||||
|
||||
it('should return the value if not a function', function () {
|
||||
expect(Function.parse('true')).toBe(true);
|
||||
expect(Function.parse(true)).toBe(true);
|
||||
expect(Function.parse('5')).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('gun.each method', function () {
|
||||
|
||||
Reference in New Issue
Block a user