Browse Source

Replace giant switch-statement test with multiple tests

pull/273/head
Julian Simioni 9 years ago
parent
commit
18de9a6766
  1. 87
      test/unit/sanitiser/_ids.js

87
test/unit/sanitiser/_ids.js

@ -16,32 +16,75 @@ var defaultLengthError = function(input) { return 'invalid param \''+ input + '\
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.sanitize_id = function(test, common) { module.exports.tests.invalid_ids = function(test, common) {
test('ids: invalid input', function(t) { test('invalid id: empty string', function(t) {
inputs.invalid.forEach( function( input ){ var raw = { ids: '' };
var raw = { ids: input }; var clean = {};
var clean = {};
var messages = sanitize(raw, clean); var messages = sanitize(raw, clean);
switch (messages.errors[0]) { t.equal(messages.errors[0], defaultError, 'ids length error returned');
case defaultError: t.equal(clean.ids, undefined, 'ids unset in clean object');
t.equal(messages.errors[0], defaultError, input + ' is invalid input'); break; t.end();
case defaultLengthError(input): });
t.equal(messages.errors[0], defaultLengthError(input), input + ' is invalid (missing id/type)'); break;
case defaultFormatError: test('invalid id: single colon', function(t) {
t.equal(messages.errors[0], defaultFormatError, input + ' is invalid (invalid format)'); break; var raw = { ids: ':' };
case defaultMissingTypeError(input): var clean = {};
t.equal(messages.errors[0], defaultMissingTypeError(input), input + ' is an unknown type'); break;
default: var messages = sanitize(raw, clean);
t.fail('error didn\'t match');
break; t.equal(messages.errors[0], defaultLengthError(':'), 'format error returned');
} t.equal(clean.ids, undefined, 'ids unset in clean object');
t.equal(clean.ids, undefined, 'clean has no ids value set');
});
t.end(); t.end();
}); });
test('invalid id: double colon', function(t) {
var raw = { ids: '::' };
var clean = {};
var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError('::'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end();
});
test('invalid id: only type section present', function(t) {
var raw = { ids: 'geoname:' };
var clean = {};
var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError('geoname:'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end();
});
test('invalid id: only type id section present', function(t) {
var raw = { ids: ':234' };
var clean = {};
var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError(':234'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end();
});
test('invalid id: type name invalid', function(t) {
var raw = { ids: 'gibberish:23' };
var clean = {};
var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultMissingTypeError('gibberish:23'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end();
});
};
module.exports.tests.valid_ids = function(test, common) {
test('ids: valid input', function(t) { test('ids: valid input', function(t) {
inputs.valid.forEach( function( input ){ inputs.valid.forEach( function( input ){
var input_parts = input.split(delimiter); var input_parts = input.split(delimiter);

Loading…
Cancel
Save