Skip to content

Commit

Permalink
Move tests and raise on .to(name, func) mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhuda committed Jan 9, 2013
1 parent 4b2d016 commit d47e5c3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions dist/route-recognizer.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ define("route-recognizer",
this.matcher.add(this.path, target);

if (callback) {
if (callback.length === 0) { throw new Error("You must have an argument in the function passed to `to`"); }
this.matcher.addChild(this.path, target, callback, this.delegate);
}
}
Expand Down
1 change: 1 addition & 0 deletions dist/route-recognizer.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ Target.prototype = {
this.matcher.add(this.path, target);

if (callback) {
if (callback.length === 0) { throw new Error("You must have an argument in the function passed to `to`"); }
this.matcher.addChild(this.path, target, callback, this.delegate);
}
}
Expand Down
1 change: 1 addition & 0 deletions dist/route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
this.matcher.add(this.path, target);

if (callback) {
if (callback.length === 0) { throw new Error("You must have an argument in the function passed to `to`"); }
this.matcher.addChild(this.path, target, callback, this.delegate);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Target.prototype = {
this.matcher.add(this.path, target);

if (callback) {
if (callback.length === 0) { throw new Error("You must have an argument in the function passed to `to`"); }
this.matcher.addChild(this.path, target, callback, this.delegate);
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/recognizer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ test("Nested routes recognize", function() {
var handler2 = { handler: 2 };

var router = new RouteRecognizer();
router.add([{ path: "/foo/:bar", handler: handler1 }, { path: "/baz/:bat", handler: handler2 }]);
router.add([{ path: "/foo/:bar", handler: handler1 }, { path: "/baz/:bat", handler: handler2 }], { as: 'foo' });

deepEqual(router.recognize("/foo/1/baz/2"), [{ handler: handler1, params: { bar: "1" }, isDynamic: true }, { handler: handler2, params: { bat: "2" }, isDynamic: true }]);

equal(router.hasRoute('foo'), true);
equal(router.hasRoute('bar'), false);
});

test("If there are multiple matches, the route with the most dynamic segments wins", function() {
Expand Down
26 changes: 10 additions & 16 deletions tests/router-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ test("supports multiple calls to match", function() {
matchesRoute("/posts/edit", [{ handler: "editPost", params: {}, isDynamic: false }]);
});

test("checks whether a route exists", function() {
router.map(function(match) {
match("/").to("index", function(match) {
match("/home").to("home");
});
match("/posts/new").to("newPost");
match("/posts/:id").to("showPost");
match("/posts/edit").to("editPost");
});

equal(router.hasRoute('newPost'), true);
equal(router.hasRoute('home'), true);
equal(router.hasRouter('zomg'), false);
equal(router.hasRouter('index'), false);
});

test("supports nested match", function() {
router.map(function(match) {
match("/posts", function(match) {
Expand All @@ -53,6 +37,16 @@ test("supports nested match", function() {
matchesRoute("/posts/edit", [{ handler: "editPost", params: {}, isDynamic: false }]);
});

test("not passing a function with `match` as a parameter raises", function() {
raises(function() {
router.map(function(match) {
match("/posts").to("posts", function() {

});
});
});
});

test("supports nested handlers", function() {
router.map(function(match) {
match("/posts").to("posts", function(match) {
Expand Down

0 comments on commit d47e5c3

Please sign in to comment.