Skip to content

Commit

Permalink
Replace node-async-testing with nodeunit and inflections-js with unde…
Browse files Browse the repository at this point in the history
…rscore.string
  • Loading branch information
danieldkim committed Sep 22, 2011
1 parent 5e64517 commit 0cd6a5f
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 253 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ format.
## Requirements

* Node.js >0.2.4
* [Underscore.js](http://documentcloud.github.com/underscore/) 1.0.2
* [inflection-js](http://code.google.com/p/inflection-js/) r37
* [node-async-testing](http://github.com/bentomas/node-async-testing) 0.4.0
* [Underscore.js](http://documentcloud.github.com/underscore/)
* [Underscore.string](https://github.com/edtsech/underscore.string)
* [nodeunit](https://github.com/caolan/nodeunit)
(optional, only needed to run tests)

## Usage
Expand Down
15 changes: 9 additions & 6 deletions lib/test-util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var async_testing = require('async_testing')
var validations = require('$1}$2');
var async_testing = require('async_testing'),
_ = require('underscore')
validations = require('validations');

exports.run = function(filename, suite) {
if (_.include(process.ARGV, '--debug'))
Expand All @@ -16,9 +17,10 @@ exports.interp_s = function interp_s(tmpl, values) {
}

exports._test_should_error = function _test_should_error(test, config, msg) {
var context = this;
return function(val) {
test.o.p = val;
var errors = validations.validate(test.o, config);
context.o.p = val;
var errors = validations.validate(context.o, config);
test.ok(errors, "no errors returned.");
test.ok(errors.on('p'), "no errors on 'p'");
test.equal(1, errors.on('p').length, "should only have 1 error on 'p'");
Expand All @@ -27,9 +29,10 @@ exports._test_should_error = function _test_should_error(test, config, msg) {
}

exports._test_should_not_error = function _test_should_not_error(test, config) {
var context = this;
return function(val) {
test.o.p = val;
var errors = validations.validate(test.o, config);
context.o.p = val;
var errors = validations.validate(context.o, config);
test.equal(true, !errors, "errors is a non-blank value.");
}
}
Expand Down
34 changes: 9 additions & 25 deletions test/new_suite_prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@ var validations = require('validations');
var _ = require('underscore')._;
var assert = require('assert');
var test_util = require('test-util');
var async_testing = require('async_testing')
, wrap = async_testing.wrap
;

// if this module is the script being run, then run the tests:
if (module == require.main) {
test_util.run(__filename, suite);
}
var nodeunit = require('nodeunit');

var suite = wrap({
suiteSetup: function(done) {
done();
},
setup: function(test, done) {
test.o = {};
test.validation_config = {
module.exports = nodeunit.testCase({
setUp: function(callback) {
this.o = {};
this.validation_config = {
default_messages: {},
properties: {
p: {}
}
};
done();
},
teardown: function(test, done) {
done();
},
suite: {
callback();
},
suiteTeardown: function(done) {
done();
}
tearDown: function(callback) {
callback();
}
});

module.exports = { 'Generic suite name (change me)': suite };
32 changes: 8 additions & 24 deletions test/test_errors_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@ var validations = require('validations');
var _ = require('underscore')._;
var assert = require('assert');
var test_util = require('test-util');
var async_testing = require('async_testing')
, wrap = async_testing.wrap
;

// if this module is the script being run, then run the tests:
if (module == require.main) {
test_util.run(__filename, suite);
}
var nodeunit = require('nodeunit');

var suite = wrap({
suiteSetup: function(done) {
done();
},
setup: function(test, done) {
test.o = {};
test.validation_config = {
module.exports = nodeunit.testCase({
setUp: function(callback) {
this.o = {};
this.validation_config = {
default_messages: {},
properties: {
p: {}
}
};
done();
},
teardown: function(test, done) {
done();
callback();
},
suite: {
tearDown: function(callback) {
callback();
},
suiteTeardown: function(done) {
done();
}
});

module.exports = { 'Errors object tests': suite };
46 changes: 15 additions & 31 deletions test/test_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ var validations = require('validations');
var _ = require('underscore')._;
var assert = require('assert');
var test_util = require('test-util');
var async_testing = require('async_testing')
, wrap = async_testing.wrap
;

// if this module is the script being run, then run the tests:
if (module == require.main) {
test_util.run(__filename, suite);
}
var nodeunit = require('nodeunit');

var suite = wrap({
suiteSetup: function(done) {
done();
},
setup: function(test, done) {
test.o = {};
test.validation_config = {
module.exports = nodeunit.testCase({
setUp: function(callback) {
this.o = {};
this.validation_config = {
default_messages: {
format: {
pattern: '*{{name}} should match pattern {{compare_to}}.*'
Expand All @@ -34,32 +24,26 @@ var suite = wrap({
}
}
};
done();
callback();
},
teardown: function(test, done) {
done();
tearDown: function(callback) {
callback();
},
suite: {
'format': test_format
},
suiteTeardown: function(done) {
done();
}

'format': test_format
});

module.exports = { 'Format tests': suite };

function test_format(test) {
var errors, config = test.validation_config,
var errors, config = this.validation_config,
msg_tmpl = config.default_messages.format.pattern;

test_util.test_val_should_error_tuples([
[undefined, false], [null, false], [0, true], [1, true], ["a", true],
["fo", true], ["foo", false], ["ffooo", false]
],
test_util._test_should_error(test, config,
test_util.interp_s(msg_tmpl, {name: 'P', compare_to: config.properties.p.format.pattern})),
test_util._test_should_not_error(test, config));
test_util._test_should_error.call(this, test, config,
test_util.interp_s(msg_tmpl, {name: 'p', compare_to: config.properties.p.format.pattern})),
test_util._test_should_not_error.call(this, test, config));

test.finish();
test.done();
}
65 changes: 24 additions & 41 deletions test/test_length.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ var validations = require('validations');
var _ = require('underscore')._;
var assert = require('assert');
var test_util = require('test-util');
var async_testing = require('async_testing')
, wrap = async_testing.wrap
;

// if this module is the script being run, then run the tests:
if (module == require.main) {
test_util.run(__filename, suite);
}
var nodeunit = require('nodeunit');

var suite = wrap({
suiteSetup: function(done) {
done();
},
setup: function(test, done) {
test.o = {};
test.validation_config = {
module.exports = nodeunit.testCase({
setUp: function(callback) {
this.o = {};
this.validation_config = {
default_messages: {
length: {}
},
Expand All @@ -30,68 +20,61 @@ var suite = wrap({
}
}
};
done();
callback();
},
teardown: function(test, done) {
done();
tearDown: function(callback) {
callback();
},
suite: {
'is': test_is,
'min': test_min,
'max': test_max
},
suiteTeardown: function(done) {
done();
}
'is': test_is,
'min': test_min,
'max': test_max
});

module.exports = { 'Length tests': suite };

function test_is(test) {
var errors, config = test.validation_config,
var errors, config = this.validation_config,
msg_tmpl = config.default_messages.required;

test_length(test, "is", "*{{name}} must be of length {{compare_to}}.*", [
test_length.call(this, test, "is", "*{{name}} must be of length {{compare_to}}.*", [
[undefined, false], [null, false], [0, false], [1, false],
[11, true], ["", true], ["a", false], ["aa", true]
], 1);

test.finish();
test.done();
}

function test_min(test) {
var errors, config = test.validation_config,
var errors, config = this.validation_config,
msg_tmpl = config.default_messages.required;

test_length(test, "min", "*{{name}} must be at least {{compare_to}} characters.*", [
test_length.call(this, test, "min", "*{{name}} must be at least {{compare_to}} characters.*", [
[undefined, false], [null, false], [0, false], [1, false],
[11, false], ["", true], ["a", false], ["aa", false]
], 1);

test.finish();
test.done();
}

function test_max(test) {
var errors, config = test.validation_config,
var errors, config = this.validation_config,
msg_tmpl = config.default_messages.required;

test_length(test, "max", "*{{name}} must be at most {{compare_to}} characters.*", [
test_length.call(this, test, "max", "*{{name}} must be at most {{compare_to}} characters.*", [
[undefined, false], [null, false], [0, false], [1, false],
[11, true], ["", false], ["a", false], ["aa", true]
], 1);

test.finish();
test.done();
}

function test_length(test, length_type, msg_tmpl,
val_should_error_tuples, compare_to) {
var config = test.validation_config;
var config = this.validation_config;
config.default_messages.length[length_type] = msg_tmpl;
config.properties.p.length[length_type] =
compare_to == undefined ? true : compare_to;

test_util.test_val_should_error_tuples(val_should_error_tuples,
test_util._test_should_error(test, config,
test_util.interp_s(msg_tmpl, {name: 'P', compare_to: compare_to})),
test_util._test_should_not_error(test, config));
test_util._test_should_error.call(this, test, config,
test_util.interp_s(msg_tmpl, {name: 'p', compare_to: compare_to})),
test_util._test_should_not_error.call(this, test, config));
}
29 changes: 6 additions & 23 deletions test/test_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,20 @@ var validations = require('validations');
var _ = require('underscore')._;
var assert = require('assert');
var test_util = require('test-util');
var async_testing = require('async_testing')
, wrap = async_testing.wrap
;

// if this module is the script being run, then run the tests:
if (module == require.main) {
test_util.run(__filename, suite);
}
var nodeunit = require('nodeunit');

var suite = wrap({
suiteSetup: function(done) {
done();
},
setup: function(test, done) {
module.exports = nodeunit.testCase({
setUp: function(callback) {
test.o = {};
test.validation_config = {
default_messages: {},
properties: {
p: {}
}
};
done();
callback();
},
teardown: function(test, done) {
done();
tearDown: function(callback) {
callback();
},
suite: {
},
suiteTeardown: function(done) {
done();
}
});

module.exports = { 'Messages tests': suite };
Loading

0 comments on commit 0cd6a5f

Please sign in to comment.