浏览代码

fix query string parsing. add tests. add travis config

Luke 8 年之前
父节点
当前提交
48dffe822a
共有 6 个文件被更改,包括 81 次插入15 次删除
  1. 19 0
      .travis.yml
  2. 2 3
      lib/src/router.dart
  3. 13 12
      lib/src/tree.dart
  4. 4 0
      pubspec.yaml
  5. 1 0
      scripts/travis_setup.sh
  6. 42 0
      test/parser_test.dart

+ 19 - 0
.travis.yml

@@ -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

+ 2 - 3
lib/src/router.dart

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

+ 13 - 12
lib/src/tree.dart

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

+ 4 - 0
pubspec.yaml

@@ -8,5 +8,9 @@ dependencies:
   flutter:
     sdk: flutter
 
+dev_dependencies:
+  flutter_test:
+    sdk: flutter
+
 flutter:
   uses-material-design: false

+ 1 - 0
scripts/travis_setup.sh

@@ -0,0 +1 @@
+git clone https://github.com/flutter/flutter.git -b alpha --depth 1

+ 42 - 0
test/parser_test.dart

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