From d47e5c3c164058124c860908b586d0e20075235d Mon Sep 17 00:00:00 2001 From: tomhuda Date: Tue, 8 Jan 2013 19:42:12 -0800 Subject: [PATCH] Move tests and raise on .to(name, func) mistake --- dist/route-recognizer.amd.js | 1 + dist/route-recognizer.cjs.js | 1 + dist/route-recognizer.js | 1 + lib/dsl.js | 1 + tests/recognizer-tests.js | 5 ++++- tests/router-tests.js | 26 ++++++++++---------------- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/dist/route-recognizer.amd.js b/dist/route-recognizer.amd.js index af2a2c7..5f7661d 100644 --- a/dist/route-recognizer.amd.js +++ b/dist/route-recognizer.amd.js @@ -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); } } diff --git a/dist/route-recognizer.cjs.js b/dist/route-recognizer.cjs.js index 0a3739e..18f417d 100644 --- a/dist/route-recognizer.cjs.js +++ b/dist/route-recognizer.cjs.js @@ -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); } } diff --git a/dist/route-recognizer.js b/dist/route-recognizer.js index 9e556ef..85c7c09 100644 --- a/dist/route-recognizer.js +++ b/dist/route-recognizer.js @@ -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); } } diff --git a/lib/dsl.js b/lib/dsl.js index c2e6ec7..637015f 100644 --- a/lib/dsl.js +++ b/lib/dsl.js @@ -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); } } diff --git a/tests/recognizer-tests.js b/tests/recognizer-tests.js index f036b6c..19a65f5 100644 --- a/tests/recognizer-tests.js +++ b/tests/recognizer-tests.js @@ -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() { diff --git a/tests/router-tests.js b/tests/router-tests.js index 4569950..e32713a 100644 --- a/tests/router-tests.js +++ b/tests/router-tests.js @@ -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) { @@ -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) {