From 6dbccd2be6a974f0096109c2ddfc5b966bd919e2 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Thu, 20 Oct 2016 05:21:59 -0400 Subject: [PATCH] Stack Overflow now, haha --- .idea/runConfigurations/Use.xml | 6 ++++++ lib/src/route.dart | 3 ++- lib/src/router.dart | 19 +++++++++++++++---- test/router/all_tests.dart | 4 ++++ test/router/use.dart | 17 +++++++++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 .idea/runConfigurations/Use.xml create mode 100644 test/router/use.dart diff --git a/.idea/runConfigurations/Use.xml b/.idea/runConfigurations/Use.xml new file mode 100644 index 00000000..e8a7c7d0 --- /dev/null +++ b/.idea/runConfigurations/Use.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/lib/src/route.dart b/lib/src/route.dart index 4a4658ad..e711a365 100644 --- a/lib/src/route.dart +++ b/lib/src/route.dart @@ -217,6 +217,7 @@ class Route { final route = new Route('$path1/$path2', children: child.children, + debug: parent.debug || child.debug, handlers: child.handlers, method: child.method, name: child.name); @@ -241,7 +242,7 @@ class Route { /// Adds the given route as a hierarchical child of this one. Route addChild(Route route, {bool join: true}) { Route created = join ? new Route.join(this, route) : route.._parent = this; - return created; + return created..debug = debug; } /// Assigns a name to this route. diff --git a/lib/src/router.dart b/lib/src/router.dart index c85d5f33..e35588df 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -17,7 +17,7 @@ class Router extends Extensible { /// Provide a `root` to make this Router revolve around a pre-defined route. /// Not recommended. - Router([Route root]) : this.root = root ?? new Route('/', name: ''); + Router([Route root]) : this.root = root ?? new _RootRoute(); void _printDebug(msg) { if (debug) @@ -89,7 +89,7 @@ class Router extends Extensible { } } - return result; + return result..debug = debug; } } @@ -102,7 +102,10 @@ class Router extends Extensible { void dumpRoute(Route route, {String replace: null}) { for (var i = 0; i < tabs; i++) buf.write(tab); - buf.write('- ${route.method} '); + + if (route == root) + buf.write('(root) ${route.method} '); + else buf.write('- ${route.method} '); final p = replace != null ? route.path.replaceAll(replace, '') : route.path; @@ -188,7 +191,7 @@ class Router extends Extensible { copiedMiddleware[middlewareName]; } - root.addChild(router.root); + root.child(path).addChild(router.root); } /// Adds a route that responds to any request matching the given path. @@ -231,3 +234,11 @@ class Router extends Extensible { return addRoute('PUT', path, handler, middleware: middleware); } } + +class _RootRoute extends Route { + _RootRoute():super("/", name: ""); + + + @override + String toString() => "ROOT"; +} \ No newline at end of file diff --git a/test/router/all_tests.dart b/test/router/all_tests.dart index fc41511b..655da558 100644 --- a/test/router/all_tests.dart +++ b/test/router/all_tests.dart @@ -1,6 +1,7 @@ import 'package:angel_route/angel_route.dart'; import 'package:test/test.dart'; import 'fallback.dart' as fallback; +import 'use.dart' as use; final ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; @@ -44,4 +45,7 @@ main() { expect(router.resolve('letter/a/lower'), equals(lower)); expect(router.resolve('letter/2/lower'), isNull); }); + + + test('use', use.main); } diff --git a/test/router/use.dart b/test/router/use.dart new file mode 100644 index 00000000..59496fcb --- /dev/null +++ b/test/router/use.dart @@ -0,0 +1,17 @@ +import 'package:angel_route/angel_route.dart'; +import 'package:test/test.dart'; + +main() { + final parent = new Router()..debug = true; + final child = new Router()..debug = true; + final a = child.get('a', ['c']); + parent.use('child', child); + parent.dumpTree(); + + group('no params', () { + test('resolve', () { + expect(parent.resolve('child/a'), equals(a)); + expect(parent.resolve('a'), isNull); + }); + }); +}