Permalink
Browse files

play with mocha and should

  • Loading branch information...
1 parent efe34bd commit 2004a82b8713782118c3372f6596c21ad4cf3023 @lloyd lloyd committed Jan 26, 2012
Showing with 81 additions and 26 deletions.
  1. +3 −0 README.md
  2. +1 −1 example/server.js
  3. +4 −6 lib/convict.js
  4. +2 −1 package.json
  5. +3 −0 test/cases/env_precedent.conf
  6. +10 −0 test/cases/env_precedent.js
  7. +1 −0 test/cases/env_precedent.out
  8. +42 −0 test/get.js
  9. +15 −18 test/run.js
View
3 README.md
@@ -0,0 +1,3 @@
+## What
+
+convict is a node
View
2 example/server.js
@@ -13,7 +13,7 @@ conf = convict({
env: "PORT",
doc: "The port to bind."
}
-}).loadFile('./config.json');
+}).loadFile(__dirname + '/config.json');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
View
10 lib/convict.js
@@ -47,7 +47,7 @@ function importEnvironment(o) {
function addDefaultValues(schema, c) {
Object.keys(schema.properties).forEach(function(name) {
- p = schema.properties[name];
+ var p = schema.properties[name];
if (p.properties) {
var kids = c[name] || {};
addDefaultValues(p, kids);
@@ -101,13 +101,11 @@ module.exports = function(def) {
var ar = path.split('.');
while (ar.length) {
var k = ar.shift();
- if (typeof o[k] !== undefined) o = o[k];
- else {
- o = undefined;
- break;
- }
+ if (typeof o[k] !== undefined) o = o[k];
+ if (o === undefined) break;
}
}
+ if (o === undefined) throw "cannot find configuration param '" + path + "'";
return o;
},
set: function(k, v) {
View
3 package.json
@@ -19,7 +19,8 @@
},
"devDependencies": {
"obj_diff": "0.2.0",
- "should": "0.5.1"
+ "should": "0.5.1",
+ "mocha": "0.10.2"
},
"scripts": {
"test": "test/run.js"
View
3 test/cases/env_precedent.conf
@@ -0,0 +1,3 @@
+{
+ "foo": "b"
+}
View
10 test/cases/env_precedent.js
@@ -0,0 +1,10 @@
+exports.conf = {
+ foo: {
+ format: 'string = "a"',
+ env: "FOO"
+ }
+};
+
+exports.env = {
+ FOO: 'c'
+};
View
1 test/cases/env_precedent.out
@@ -0,0 +1 @@
+{ "foo": "c" }
View
42 test/get.js
@@ -0,0 +1,42 @@
+const should = require('should');
+
+describe('convict', function() {
+ const convict = require('../');
+ var conf;
+
+ it('should parse a config specification', function() {
+ conf = convict({
+ foo: {
+ bar: "number = 7",
+ baz: {
+ bing: 'string = "foo"',
+ "name with spaces": {
+ name_with_underscores: "boolean = true"
+ }
+ }
+ }
+ });
+ });
+
+ it('should be valid', function() {
+ (function() { conf.validate() }).should.not.throw();
+ });
+
+ describe('.get()', function() {
+ it('should find a nested value', function() {
+ (conf.get('foo.bar')).should.equal(7);
+ });
+
+ it('should handle three levels of nesting', function() {
+ (conf.get('foo.baz.bing')).should.equal('foo');
+ });
+
+ it('should handle names with spaces and underscores', function() {
+ (conf.get('foo.baz.name with spaces.name_with_underscores')).should.equal(true);
+ });
+
+ it("should throw if conf doesn't exist", function() {
+ (function() { conf.get('foo.no') }).should.throw();
+ });
+ });
+});
View
33 test/run.js
@@ -1,11 +1,12 @@
-#!/usr/bin/env node
-
const
fs = require('fs'),
path = require('path'),
convict = require('../lib/convict.js'),
cp = require('child_process'),
-obj_diff = require('obj_diff').diff;
+obj_diff = require('obj_diff').diff,
+mocha = require('mocha');
+
+mocha.Suite('static tests');
const casesDir = path.join(__dirname, 'cases');
var files = fs.readdirSync(casesDir);
@@ -41,15 +42,10 @@ function diffObjects(a, b) {
}
// time to run!
-var passed = 0;
var toRun = Object.keys(tests);
-function runOne() {
- if (!toRun.length) return;
- var name = toRun.shift();
-
+function run(name, done) {
var test = tests[name];
- process.stdout.write(name + " - ");
var env = require(path.join(casesDir, test.spec)).env || {};
@@ -65,24 +61,25 @@ function runOne() {
// check that configuration is what we expect
var err = diffObjects(expected, got);
if (err) throw err;
-
- passed++;
- console.log("ok");
+ return done();
} else {
var expected = fs.readFileSync(path.join(casesDir, test.output)).toString().trim();
var got = m.error.trim();
if (expected.trim() !== got.trim()) throw got;
-
- passed++;
- console.log("ok");
+ return done();
}
} catch(e) {
- console.log("fail (", e, ")");
+ done(e);
}
- runOne();
});
n.send(tests[name]);
};
-runOne();
+describe('Static tests', function() {
+ toRun.forEach(function(name) {
+ it(name, function(done) {
+ run(name, done);
+ });
+ });
+});

0 comments on commit 2004a82

Please sign in to comment.