Skip to content
Browse files

passing all tests

  • Loading branch information...
1 parent 2f6d77b commit efa33da76ebcaf3a0839a772454132c3a799c24b @joshbuddy committed Jul 1, 2010
Showing with 52 additions and 32 deletions.
  1. +28 −8 lib/sherpa.js
  2. +24 −24 spec/spec_generate.js
View
36 lib/sherpa.js
@@ -387,14 +387,29 @@ Sherpa.Route.prototype = {
return this;
},
generate: function(params) {
+ var sys = require('sys');
+ var path = undefined;
if (params == undefined || this.paths.length == 1) {
- return this.paths[0].generate(params);
+ path = this.paths[0].generate(params);
} else {
- for(var pathIdx = 0; pathIdx != this.paths.length == 1; pathIdx++) {
- var path = this.paths[pathIdx].generate(params);
- if (path) return path;
+ for(var pathIdx = this.paths.length - 1; pathIdx >= 0; pathIdx--) {
+ path = this.paths[pathIdx].generate(params);
+ if (path) break;
}
- return undefined;
+ }
+
+ sys.debug("path is: "+sys.inspect(path));
+ if (path) {
+ sys.debug("params are now: "+sys.inspect(params));
+
+ path = encodeURI(path);
+ var query = '';
+ for (var key in params) {
+ query += (query == '' ? '?' : '&') + encodeURIComponent(key).replace(/%20/g, '+') + '=' + encodeURIComponent(params[key]).replace(/%20/g, '+');
+ }
+ return path + query;
+ } else {
+ return undefined
}
}
};
@@ -403,8 +418,8 @@ Sherpa.Path.prototype = {
generate: function(params) {
var sys = require('sys');
sys.debug("path generate for "+sys.inspect(params) + " from "+sys.inspect(this.variableNames));
- for (var paramsKey in params) {
- if (this.variableNames.indexOf(paramsKey) == -1) return undefined;
+ for(var varIdx = 0; varIdx != this.variableNames.length; varIdx++) {
+ if (!params[this.variableNames[varIdx]]) return undefined;
}
sys.debug("passed for "+sys.inspect(params));
for(var varIdx = 0; varIdx != this.variableNames.length; varIdx++) {
@@ -420,7 +435,12 @@ Sherpa.Path.prototype = {
sys.debug("matchesWith doesn't exist...");
}
}
- return eval(this.compiledUri);
+ var path = eval(this.compiledUri);
+ for(var varIdx = 0; varIdx != this.variableNames.length; varIdx++) {
+ delete params[this.variableNames[varIdx]];
+ }
+ sys.debug("left with params "+sys.inspect(params));
+ return path;
},
compile: function() {
var sys = require('sys');
View
48 spec/spec_generate.js
@@ -45,28 +45,28 @@ minitest.context("Sherpa#generate()", function () {
assert.equal('/?test2=hello', this.router.generate('with_optional', {test2: 'hello'}))
test.finished();
});
- //
- //this.assertion("should generate extra params as a query string after", function(test) {
- // this.router.add('/:test', {matchesWith: {test: /asd|qwe|\d+/}}).name('with_variable');
- // assert.equal('/123?foo=bar', this.router.generate('with_variable', {test: '123', foo: 'bar'}));
- // test.finished();
- //});
- //
- //this.assertion("should generate extra params as a query string after", function(test) {
- // this.router.add('/:test', {matchesWith: {test: /asd|qwe|\d+/}}).name('with_variable');
- // assert.equal('/123?foo=bar', this.router.generate('with_variable', {test: '123', foo: 'bar'}));
- // test.finished();
- //});
- //
- //this.assertion("should escape values in the URI", function(test) {
- // this.router.add('/:test').name('with_variable');
- // assert.equal('/%5B%20%5D+=-', this.router.generate('with_variable', {test: '[ ]+=-'}))
- // test.finished();
- //});
- //
- //this.assertion("should escape values in the query string", function(test) {
- // this.router.add('/').name('simple');
- // assert.equal('/?test+and+more=%5B+%5D%2B%3D-', this.router.generate('simple', {"test and more": '[ ]+=-'}))
- // test.finished();
- //});
+
+ this.assertion("should generate extra params as a query string after", function(test) {
+ this.router.add('/:test', {matchesWith: {test: /asd|qwe|\d+/}}).name('with_variable').compile();
+ assert.equal('/123?foo=bar', this.router.generate('with_variable', {test: '123', foo: 'bar'}));
+ test.finished();
+ });
+
+ this.assertion("should generate extra params as a query string after", function(test) {
+ this.router.add('/:test', {matchesWith: {test: /asd|qwe|\d+/}}).name('with_variable').compile();
+ assert.equal('/123?foo=bar', this.router.generate('with_variable', {test: '123', foo: 'bar'}));
+ test.finished();
+ });
+
+ this.assertion("should escape values in the URI", function(test) {
+ this.router.add('/:test').name('with_variable').compile();
+ assert.equal('/%5B%20%5D+=-', this.router.generate('with_variable', {test: '[ ]+=-'}))
+ test.finished();
+ });
+
+ this.assertion("should escape values in the query string", function(test) {
+ this.router.add('/').name('simple').compile();
+ assert.equal('/?test+and+more=%5B+%5D%2B%3D-', this.router.generate('simple', {"test and more": '[ ]+=-'}))
+ test.finished();
+ });
});

0 comments on commit efa33da

Please sign in to comment.
Something went wrong with that request. Please try again.