provide test cases for config (and fix null case); refs #24

This commit is contained in:
Radek Stepan 2013-10-04 19:17:28 +01:00
parent dfeddcb4e4
commit 03c61624d0
3 changed files with 138 additions and 42 deletions

View File

@ -26,14 +26,10 @@ function require(path, parent, orig) {
// perform real require() // perform real require()
// by invoking the module's // by invoking the module's
// registered function // registered function
if (!module._resolving && !module.exports) { if (!module.exports) {
var mod = {}; module.exports = {};
mod.exports = {}; module.client = module.component = true;
mod.client = mod.component = true; module.call(this, module.exports, require.relative(resolved), module);
module._resolving = true;
module.call(this, mod.exports, require.relative(resolved), mod);
delete module._resolving;
module.exports = mod.exports;
} }
return module.exports; return module.exports;
@ -205,7 +201,7 @@ module.exports = require('./dist/lodash.compat.js');
require.register("lodash-lodash/dist/lodash.compat.js", function(exports, require, module){ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, require, module){
/** /**
* @license * @license
* Lo-Dash 2.2.0 (Custom Build) <http://lodash.com/> * Lo-Dash 2.2.1 (Custom Build) <http://lodash.com/>
* Build: `lodash -o ./dist/lodash.compat.js` * Build: `lodash -o ./dist/lodash.compat.js`
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/> * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE> * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
@ -792,8 +788,8 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* Creates a `lodash` object which wraps the given value to enable method * Creates a `lodash` object which wraps the given value to enable intuitive
* chaining. * method chaining.
* *
* In addition to Lo-Dash methods, wrappers also have the following `Array` methods: * In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`, * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
@ -827,6 +823,8 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
* The wrapper functions `first` and `last` return wrapped values when `n` is * The wrapper functions `first` and `last` return wrapped values when `n` is
* provided, otherwise they return unwrapped values. * provided, otherwise they return unwrapped values.
* *
* Explicit chaining can be enabled by using the `_.chain` method.
*
* @name _ * @name _
* @constructor * @constructor
* @category Chaining * @category Chaining
@ -6104,9 +6102,12 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
push.apply(args, arguments); push.apply(args, arguments);
var result = func.apply(object, args); var result = func.apply(object, args);
return (value && typeof value == 'object' && value === result) if (value && typeof value == 'object' && value === result) {
? this return this;
: new ctor(result); }
result = new ctor(result);
result.__chain__ = this.__chain__;
return result;
}; };
} }
}); });
@ -6336,7 +6337,7 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
// and Laura Doktorova's doT.js // and Laura Doktorova's doT.js
// https://github.com/olado/doT // https://github.com/olado/doT
var settings = lodash.templateSettings; var settings = lodash.templateSettings;
text || (text = ''); text = String(text || '');
// avoid missing dependencies when `iteratorTemplate` is not defined // avoid missing dependencies when `iteratorTemplate` is not defined
options = defaults({}, options, settings); options = defaults({}, options, settings);
@ -6508,7 +6509,8 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* Creates a `lodash` object that wraps the given value. * Creates a `lodash` object that wraps the given value with explicit
* method chaining enabled.
* *
* @static * @static
* @memberOf _ * @memberOf _
@ -6524,9 +6526,10 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
* ]; * ];
* *
* var youngest = _.chain(stooges) * var youngest = _.chain(stooges)
* .sortBy(function(stooge) { return stooge.age; }) * .sortBy('age')
* .map(function(stooge) { return stooge.name + ' is ' + stooge.age; }) * .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })
* .first(); * .first()
* .value();
* // => 'moe is 40' * // => 'moe is 40'
*/ */
function chain(value) { function chain(value) {
@ -6563,7 +6566,7 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
} }
/** /**
* Enables method chaining on the wrapper object. * Enables explicit method chaining on the wrapper object.
* *
* @name chain * @name chain
* @memberOf _ * @memberOf _
@ -6571,11 +6574,21 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
* @returns {*} Returns the wrapper object. * @returns {*} Returns the wrapper object.
* @example * @example
* *
* var sum = _([1, 2, 3]) * var stooges = [
* .chain() * { 'name': 'moe', 'age': 40 },
* .reduce(function(sum, num) { return sum + num; }) * { 'name': 'larry', 'age': 50 }
* .value() * ];
* // => 6` *
* // without explicit chaining
* _(stooges).first();
* // => { 'name': 'moe', 'age': 40 }
*
* // with explicit chaining
* _(stooges).chain()
* .first()
* .pick('age')
* .value()
* // => { 'age': 40 }
*/ */
function wrapperChain() { function wrapperChain() {
this.__chain__ = true; this.__chain__ = true;
@ -6809,7 +6822,7 @@ require.register("lodash-lodash/dist/lodash.compat.js", function(exports, requir
* @memberOf _ * @memberOf _
* @type string * @type string
*/ */
lodash.VERSION = '2.2.0'; lodash.VERSION = '2.2.1';
// add "Chaining" functions to the wrapper // add "Chaining" functions to the wrapper
lodash.prototype.chain = wrapperChain; lodash.prototype.chain = wrapperChain;
@ -28108,6 +28121,9 @@ validators = {
}; };
module.exports = function(cb) { module.exports = function(cb) {
if (typeof window === 'undefined') {
config = null;
}
if (config) { if (config) {
return cb(null, config); return cb(null, config);
} }
@ -28115,16 +28131,9 @@ module.exports = function(cb) {
if (!wait) { if (!wait) {
wait = true; wait = true;
return request.config(function(err, result) { return request.config(function(err, result) {
var field, k, v, validator; var field, validator, _results;
if (err || !_.isObject(result)) { wait = false;
config = {}; config = _.defaults(result || {}, defaults);
}
for (k in defaults) {
v = defaults[k];
if (config[k] == null) {
config[k] = v;
}
}
if (config.size_label) { if (config.size_label) {
config.size_label = new RegExp(config.size_label); config.size_label = new RegExp(config.size_label);
} else { } else {
@ -28138,9 +28147,11 @@ module.exports = function(cb) {
} }
} }
} }
return _.each(queue, function(cb) { _results = [];
return cb(null, config); while (queue.length) {
}); _results.push(queue.pop()(null, config));
}
return _results;
}); });
} }
}; };

View File

@ -33,6 +33,8 @@ validators =
# Get (& cache) configuration from the server. # Get (& cache) configuration from the server.
module.exports = (cb) -> module.exports = (cb) ->
# Skip cache in node.
config = null if typeof window is 'undefined'
# Have config? # Have config?
return cb null, config if config return cb null, config if config
# Enqueue. # Enqueue.
@ -43,9 +45,12 @@ module.exports = (cb) ->
wait = yes wait = yes
# Make the request. # Make the request.
request.config (err, result) -> request.config (err, result) ->
# The wait is over.
wait = no
# We do not strictly require config files. # We do not strictly require config files.
config = _.defaults result, defaults config = _.defaults result or {}, defaults
# RegExpify the size label? # RegExpify the size label?
if config.size_label if config.size_label
config.size_label = new RegExp config.size_label config.size_label = new RegExp config.size_label
@ -58,5 +63,4 @@ module.exports = (cb) ->
return cb "Config field `#{field}` misconfigured" return cb "Config field `#{field}` misconfigured"
# Call back for each enqueued. # Call back for each enqueued.
_.each queue, (cb) -> ( queue.pop() null, config while queue.length )
cb null, config

81
test/config.coffee Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env coffee
proxy = do require('proxyquire').noCallThru
assert = require 'assert'
path = require 'path'
req = {}
config = proxy path.resolve(__dirname, '../src/modules/config.coffee'),
'./request': req
{ size_label } = require path.resolve __dirname, '../src/modules/regex.coffee'
module.exports =
'config - is null': (done) ->
req.config = (cb) ->
cb null, null
config (err, cfg) ->
assert.ifError err
assert.deepEqual cfg,
'host': 'api.github.com'
'protocol': 'https'
'size_label': new RegExp size_label
do done
'config - is empty': (done) ->
req.config = (cb) ->
cb null, {}
config (err, cfg) ->
assert.ifError err
assert.deepEqual cfg,
'host': 'api.github.com'
'protocol': 'https'
'size_label': new RegExp size_label
do done
'config - custom size label': (done) ->
size = '/^taille (\d+)$/'
req.config = (cb) ->
cb null, { 'size_label': size }
config (err, cfg) ->
assert.ifError err
assert.deepEqual cfg,
'host': 'api.github.com'
'protocol': 'https'
'size_label': new RegExp size
do done
'config - custom valid protocol': (done) ->
req.config = (cb) ->
cb null, { 'protocol': 'http' }
config (err, cfg) ->
assert.ifError err
assert.deepEqual cfg,
'host': 'api.github.com'
'protocol': 'http'
'size_label': new RegExp size_label
do done
'config - custom invalid protocol': (done) ->
req.config = (cb) ->
cb null, { 'protocol': 'nntp' }
config (err, cfg) ->
assert.equal err, 'Config field `protocol` misconfigured'
assert.equal cfg, null
do done
'config - custom invalid off days': (done) ->
req.config = (cb) ->
cb null, { 'off_days': [ 0 ] }
config (err, cfg) ->
assert.equal err, 'Config field `off_days` misconfigured'
assert.equal cfg, null
do done