Permalink
Browse files

new tests to match the changes

  • Loading branch information...
1 parent 0672b9a commit 56cc5df0aeae6c4a037524175c4ca3bd1a6be232 @haggen committed Aug 24, 2012
Showing with 68 additions and 105 deletions.
  1. +68 −105 test/test.js
View
@@ -1,99 +1,82 @@
-var tests, assert, way;
+var routes, tests, assert, way;
assert = require('assert');
way = require('../way.js');
-// Add new tests cases inside the array below.
-// Test cases consist of:
-// - a route pattern
-// - a successfully built regex
-// - optional groups (underscores) and named parameters
-// - successful matches
-// - captures of each match
-// - mismatches
-
-tests = [
+routes = [
{
- pattern: '/hello',
- re: /^\/hello$/,
+ pattern: '/a',
+ re: /^\/a/,
params: [],
- action: function() {},
- matches: ['/hello'],
- captures: [],
- mismatch: ['/bye']
+ action: function() {}
}, {
- pattern: '/hello/:name',
- re: /^\/hello\/([^\/]+?)$/,
- params: ['name'],
- action: function() {},
- matches: ['/hello/world'],
- captures: [{ name: 'world' }],
- mismatch: ['/hello', '/hello/you/me']
+ pattern: '/b/:c',
+ re: /^\/b\/([^\/]+?)/,
+ params: ['c'],
+ action: function() {}
+ }, {
+ pattern: '/b/d',
+ re: /^\/b\/d/,
+ params: [],
+ action: function() {}
}, {
- pattern: '/about(/me)',
- re: /^\/about(\/me)?$/,
+ pattern: '/c(/d)',
+ re: /^\/c(\/d)?/,
params: ['_'],
- action: function() {},
- matches: ['/about', '/about/me'],
- captures: [],
- mismatch: ['/not/about', '/about/you', '/about/me/more']
+ action: function() {}
}, {
- pattern: '/say/*/quickly',
- re: /^\/say\/(.+?)\/quickly$/,
+ pattern: '/d/*/e',
+ re: /^\/d\/(.+?)\/e/,
params: ['splat'],
- action: function() {},
- matches: ['/say/something/quickly', '/say/a/b/c/quickly'],
- captures: [{ splat: 'something' }, { splat: 'a/b/c' }],
- mismatch: ['/say', '/say/quickly']
+ action: function() {}
}, {
- pattern: '/sort(/:order)',
- re: /^\/sort(\/([^\/]+?))?$/,
- params: ['_', 'order'],
- action: function() {},
- matches: ['/sort', '/sort/desc', '/sort/asc'],
- captures: [{}, { order: 'desc' }, { order: 'asc' }],
- mismatch: ['/sort/desc/more', '/sorting']
- }, {
- pattern: '/have(/not)(/you)',
- re: /^\/have(\/not)?(\/you)?$/,
+ pattern: '/e(/f)(/g)',
+ re: /^\/e(\/f)?(\/g)?/,
params: ['_', '_'],
- action: function() {},
- matches: ['/have', '/have/you', '/have/not/you'],
- captures: [],
- mismatch: ['/havent', '/have/you/not']
+ action: function() {}
}, {
- pattern: '/do/:action/:mod(/:target)',
- re: /^\/do\/([^\/]+?)\/([^\/]+?)(\/([^\/]+?))?$/,
- params: ['action', 'mod', '_', 'target'],
- action: function() {},
- matches: ['/do/run/fast', '/do/jump/high/thefence'],
- captures: [{ action: 'run', mod: 'fast' }, { action: 'jump', mod: 'high', target: 'thefence' }],
- mismatch: ['/do', '/do/action', '/do/a/b/c/d']
+ pattern: '/f/:g(/:h)',
+ re: /^\/f\/([^\/]+?)(\/([^\/]+?))?/,
+ params: ['g', '_', 'h'],
+ action: function() {}
}, {
- pattern: '/api(/:version)/:collection(/:target)(/:action)',
- re: /^\/api(\/([^\/]+?))?\/([^\/]+?)(\/([^\/]+?))?(\/([^\/]+?))?$/,
- params: ['_', 'version', 'collection', '_', 'target', '_', 'action'],
- action: function() {},
- matches: ['/api/users', '/api/1/users', '/api/1/users/1', '/api/1/users/1/destroy'],
- captures: [{ collection: 'users' }, { version: '1', collection: 'users' }, { version: '1', collection: 'users', target: '1' }, { version: '1', collection: 'users', target: '1', action: 'destroy' }],
- mismatch: ['/api', '/api/a/b/c/d/e']
+ pattern: '/g(/:h)/:i/*',
+ re: /^\/g(\/([^\/]+?))?\/([^\/]+?)\/(.+?)/,
+ params: ['_', 'h', 'i', 'splat'],
+ action: function() {}
}
];
-// Every test case will be tested to see if:
-// - they're properly mapped
-// - their patterns were correctly translated to regex
-// - their matches succeed
-// - their mismatches fail
-// - their named parameters are correctly captured
+tests = [
+ {
+ path: '/a',
+ matches: [
+ {
+ action: routes[0].action,
+ params: {}
+ }
+ ]
+ }, {
+ path: '/b/d',
+ matches: [
+ {
+ action: routes[1].action,
+ params: { c: 'd' }
+ }, {
+ action: routes[2].action,
+ params: {}
+ }
+ ]
+ }
+];
describe('Way', function() {
describe('#map', function() {
it('should account new routes', function() {
var i;
- for(i = 0; i < tests.length; i++) {
- assert.equal(way.map(tests[i].pattern, tests[i].action), null);
+ for(i = 0; i < routes.length; i++) {
+ way.map(routes[i].pattern, routes[i].action);
assert.equal(way.routes.length, i + 1);
}
});
@@ -103,56 +86,36 @@ describe('Way', function() {
it('should have built a regex for each route', function() {
var i;
- for(i = 0; i < tests.length; i++) {
+ for(i = 0; i < way.routes.length; i++) {
assert(way.routes[i].hasOwnProperty('pattern'));
- assert.equal(String(way.routes[i].pattern), String(tests[i].re));
+ assert.equal(String(way.routes[i].pattern), String(routes[i].re));
}
});
- it('should have saved named parameters and optional groups', function() {
+ it('should have parsed named parameters, optional groups and splats', function() {
var i;
- for(i = 0; i < tests.length; i++) {
+ for(i = 0; i < routes.length; i++) {
assert(way.routes[i].hasOwnProperty('params'));
- assert.equal(String(way.routes[i].params), String(tests[i].params));
+ assert.equal(String(way.routes[i].params), String(routes[i].params));
}
});
});
describe('#match', function() {
- it('should succeed against matches of each test case', function() {
- var i, j;
+ it('should return the matches described in each test', function() {
+ var m, i, j, k;
for(i = 0; i < tests.length; i++) {
- for(j = 0; j < tests[i].matches.length; j++) {
- assert.equal(way.match(tests[i].matches[j]), tests[i].action);
- }
- }
- });
-
- it('should fail against mismatches of each test case', function() {
- var i, j;
+ m = way.match(tests[i].path);
- for(i = 0; i < tests.length; i++) {
- for(j = 0; j < tests[i].mismatch.length; j++) {
- assert.notEqual(way.match(tests[i].mismatch[j]), tests[i].action);
- }
- }
- });
-
- it('should capture named parameters as defined by each test case', function() {
- var i, j, k;
-
- for(i = 0; i < tests.length; i++) {
for(j = 0; j < tests[i].matches.length; j++) {
- way.match(tests[i].matches[j]);
+ assert.equal(m[j].action, tests[i].matches[j].action);
- if(j in tests[i].captures) {
- for(k in tests[i].captures[j]) {
- if(tests[i].captures[j].hasOwnProperty(k)) {
- assert(way.params.hasOwnProperty(k));
- assert.equal(way.params[k], tests[i].captures[j][k]);
- }
+ for(k in tests[i].matches[j].params) {
+ if(tests[i].matches[j].params.hasOwnProperty(k)) {
+ assert(k in m[j].params);
+ assert.equal(m[j].params[k], tests[i].matches[j].params[k]);
}
}
}

0 comments on commit 56cc5df

Please sign in to comment.