Stack Overflow now, haha

This commit is contained in:
thosakwe 2016-10-20 05:21:59 -04:00
parent a9e4f6d4dc
commit 6dbccd2be6
5 changed files with 44 additions and 5 deletions

View file

@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Use" type="DartTestRunConfigurationType" factoryName="Dart Test" folderName="Router Tests" singleton="true">
<option name="filePath" value="$PROJECT_DIR$/test/router/use.dart" />
<method />
</configuration>
</component>

View file

@ -217,6 +217,7 @@ class Route {
final route = new Route('$path1/$path2', final route = new Route('$path1/$path2',
children: child.children, children: child.children,
debug: parent.debug || child.debug,
handlers: child.handlers, handlers: child.handlers,
method: child.method, method: child.method,
name: child.name); name: child.name);
@ -241,7 +242,7 @@ class Route {
/// Adds the given route as a hierarchical child of this one. /// Adds the given route as a hierarchical child of this one.
Route addChild(Route route, {bool join: true}) { Route addChild(Route route, {bool join: true}) {
Route created = join ? new Route.join(this, route) : route.._parent = this; Route created = join ? new Route.join(this, route) : route.._parent = this;
return created; return created..debug = debug;
} }
/// Assigns a name to this route. /// Assigns a name to this route.

View file

@ -17,7 +17,7 @@ class Router extends Extensible {
/// Provide a `root` to make this Router revolve around a pre-defined route. /// Provide a `root` to make this Router revolve around a pre-defined route.
/// Not recommended. /// Not recommended.
Router([Route root]) : this.root = root ?? new Route('/', name: '<root>'); Router([Route root]) : this.root = root ?? new _RootRoute();
void _printDebug(msg) { void _printDebug(msg) {
if (debug) 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}) { void dumpRoute(Route route, {String replace: null}) {
for (var i = 0; i < tabs; i++) buf.write(tab); 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 = final p =
replace != null ? route.path.replaceAll(replace, '') : route.path; replace != null ? route.path.replaceAll(replace, '') : route.path;
@ -188,7 +191,7 @@ class Router extends Extensible {
copiedMiddleware[middlewareName]; copiedMiddleware[middlewareName];
} }
root.addChild(router.root); root.child(path).addChild(router.root);
} }
/// Adds a route that responds to any request matching the given path. /// 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); return addRoute('PUT', path, handler, middleware: middleware);
} }
} }
class _RootRoute extends Route {
_RootRoute():super("/", name: "<root>");
@override
String toString() => "ROOT";
}

View file

@ -1,6 +1,7 @@
import 'package:angel_route/angel_route.dart'; import 'package:angel_route/angel_route.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'fallback.dart' as fallback; import 'fallback.dart' as fallback;
import 'use.dart' as use;
final ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; final ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
@ -44,4 +45,7 @@ main() {
expect(router.resolve('letter/a/lower'), equals(lower)); expect(router.resolve('letter/a/lower'), equals(lower));
expect(router.resolve('letter/2/lower'), isNull); expect(router.resolve('letter/2/lower'), isNull);
}); });
test('use', use.main);
} }

17
test/router/use.dart Normal file
View file

@ -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);
});
});
}