Skip to content

Commit

Permalink
fix query string parsing. add tests. add travis config
Browse files Browse the repository at this point in the history
  • Loading branch information
lukef committed May 6, 2017
1 parent 6149f42 commit 48dffe8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 15 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
os:
- linux
sudo: false
addons:
apt:
# Flutter depends on /usr/lib/x86_64-linux-gnu/libstdc++.so.6 version GLIBCXX_3.4.18
sources:
- ubuntu-toolchain-r-test # if we don't specify this, the libstdc++6 we get is the wrong version
packages:
- libstdc++6
- fonts-droid
before_script:
- ./scripts/travis_setup.sh
- ./flutter/bin/flutter doctor
script:
- ./flutter/bin/flutter test
cache:
directories:
- $HOME/.pub-cache
5 changes: 2 additions & 3 deletions lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class Router {

/// Finds a defined [AppRoute] for the path value. If no [AppRoute] definition was found
/// then function will return null.
AppRoute match(String path) {
AppRouteMatch match = _routeTree.matchRoute(path);
return match?.route;
AppRouteMatch match(String path) {
return _routeTree.matchRoute(path);
}

///
Expand Down
25 changes: 13 additions & 12 deletions lib/src/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,25 @@ class RouteTree {
Map<RouteTreeNode, RouteTreeNodeMatch> currentMatches = <RouteTreeNode, RouteTreeNodeMatch>{};
List<RouteTreeNode> nextNodes = <RouteTreeNode>[];
for (RouteTreeNode node in nodesToCheck) {
bool isMatch = (node.part == checkComponent || node.isParameter());
String pathPart = checkComponent;
Map<String, String> queryMap;
if (checkComponent.contains("?")) {
var splitParam = checkComponent.split("?");
pathPart = splitParam[0];
queryMap = parseQueryString(splitParam[1]);
}
bool isMatch = (node.part == pathPart || node.isParameter());
if (isMatch) {
RouteTreeNodeMatch parentMatch = nodeMatches[node.parent];
// print("pm: ${parentMatch?.node?.part}, ${parentMatch?.parameters}");
RouteTreeNodeMatch match = new RouteTreeNodeMatch.fromMatch(parentMatch, node);
if (node.isParameter()) {
String paramKey = node.part.substring(1);
if (checkComponent.contains("?")) {
var splitParam = checkComponent.split("?");
var namedParam = splitParam[0];
var queryParams = parseQueryString(splitParam[1]);
match.parameters[paramKey] = namedParam;
match.parameters.addAll(queryParams);
} else {
match.parameters[paramKey] = checkComponent;
}
match.parameters[paramKey] = pathPart;
}
if (queryMap != null) {
match.parameters.addAll(queryMap);
}
print("matched: ${node.part}, isParam: ${node.isParameter()}, params: ${match.parameters}");
// print("matched: ${node.part}, isParam: ${node.isParameter()}, params: ${match.parameters}");
currentMatches[node] = match;
if (node.nodes != null) {
nextNodes.addAll(node.nodes);
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter

flutter:
uses-material-design: false
1 change: 1 addition & 0 deletions scripts/travis_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git clone https://github.com/flutter/flutter.git -b alpha --depth 1
42 changes: 42 additions & 0 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:router/router.dart';

void main() {

testWidgets("Router correctly parses named parameters", (WidgetTester tester) async {
String path = "/users/1234";
String route = "/users/:id";
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, String>{
"id" : "1234",
}));
});

testWidgets("Router correctly parses named parameters with query", (WidgetTester tester) async {
String path = "/users/1234?name=luke";
String route = "/users/:id";
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, String>{
"id" : "1234",
"name" : "luke",
}));
});

testWidgets("Router correctly parses query parameters", (WidgetTester tester) async {
String path = "/users/create?name=luke&phrase=hello%20world&number=7";
String route = "/users/create";
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, String>{
"name" : "luke",
"phrase" : "hello world",
"number" : "7",
}));
});

}

0 comments on commit 48dffe8

Please sign in to comment.