From e14ff21d76fa256fc7d8e4f545296bf3c3c8f28d Mon Sep 17 00:00:00 2001 From: Wyatt Preul Date: Thu, 7 Feb 2013 16:44:51 -0600 Subject: [PATCH] Adding basic function support --- lib/types/function.js | 44 ++++++++++++++++++++++++++++++++++++++++++ test/types/function.js | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 lib/types/function.js create mode 100644 test/types/function.js diff --git a/lib/types/function.js b/lib/types/function.js new file mode 100644 index 000000000..f0b9e6a53 --- /dev/null +++ b/lib/types/function.js @@ -0,0 +1,44 @@ +// Load modules + +var NodeUtil = require('util'); +var BaseType = require('./base'); +var Utils = require('../utils'); + + +// Declare internals + +var internals = {}; + + +module.exports = internals.createType = function () { + + return new internals.FunctionType(); +}; + + +module.exports.FunctionType = internals.FunctionType = function () { + + internals.FunctionType.super_.call(this); + Utils.mixin(this, BaseType); + return this; +}; + +NodeUtil.inherits(internals.FunctionType, BaseType); + + +internals.FunctionType.prototype.__name = "Function"; + + +internals.FunctionType.prototype._base = function() { + + return function(value) { + + return (value === null || typeof value === "function"); + }; +}; + +internals.FunctionType.prototype.base = function () { + + this.add('base', this._base(), arguments); + return this; +}; \ No newline at end of file diff --git a/test/types/function.js b/test/types/function.js new file mode 100644 index 000000000..32ca125ef --- /dev/null +++ b/test/types/function.js @@ -0,0 +1,43 @@ +// Load modules + +var Chai = require('chai'); +var Joi = process.env.TEST_COV ? require('../../lib-cov') : require('../../lib'); +var FunctionType = process.env.TEST_COV ? require('../../lib-cov/types/function') : require('../../lib/types/function'); +var Support = require('../support/meta'); + + +// Declare internals + +var internals = {}; + + +// Test shortcuts + +var expect = Chai.expect; +var verifyBehavior = Support.verifyValidatorBehavior; + + +describe('Types', function () { + + describe('Function', function () { + + var F = FunctionType; // Joi.types.Function; + + it('should have mixins', function (done) { + + var result = F(); + expect(result.validate).to.exist; + done(); + }); + + it('should validate a function', function (done) { + + var t = F().required(); + verifyBehavior(t, [ + [function(){ }, true], + ['', false] + ], done); + }); + }); +}); +