| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library route.url_pattern_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:route_hierarchical/url_pattern.dart'; |
| 9 |
| 10 main() { |
| 11 test('patterns with no groups', () { |
| 12 checkPattern('/', '/', [], ['', 'a', '/a']); |
| 13 checkPattern('a', 'a', [], ['', '/', '/a']); |
| 14 }); |
| 15 |
| 16 test('patterns with basic groups', () { |
| 17 checkPattern(r'(\w+)', 'ab', ['ab'], ['(ab)', '', ' ']); |
| 18 }); |
| 19 |
| 20 test('patterns with escaping', () { |
| 21 checkPattern(r'\\', r'\', []); |
| 22 // it's ok to leave a hanging escape? |
| 23 checkPattern(r'\\\', r'\', []); |
| 24 checkPattern(r'\a', r'a', []); |
| 25 checkPattern(r'\(a\)', '(a)', [], ['a']); |
| 26 checkPattern(r'(a\))', 'a)', ['a)'], ['a']); |
| 27 checkPattern(r'(\\w)', r'\w', [r'\w'], [r'\a']); |
| 28 }); |
| 29 |
| 30 test('patterns with more complicated groups', () { |
| 31 checkPattern(r'/(\w+)', '/foo', ['foo'], ['foo']); |
| 32 checkPattern(r'/(\w+)/(\w+)', '/foo/bar', ['foo', 'bar']); |
| 33 // these are odd cases. maybe we should ban nested groups. |
| 34 checkPattern(r'((\w+))', 'a', ['a', 'a'], ['(a)']); |
| 35 checkPattern(r'((\w+)(\d+))', 'a1', ['a1', 'a', '1'], ['(a1)']); |
| 36 }); |
| 37 |
| 38 test('disallow ambiguous groups', () { |
| 39 expect(() => new UrlPattern(r'(\w+)(\w+)'), throws); |
| 40 }); |
| 41 |
| 42 test('disallow unmatched parens', () { |
| 43 expect(() => new UrlPattern('('), throws); |
| 44 expect(() => new UrlPattern(')'), throws); |
| 45 expect(() => new UrlPattern('(()'), throws); |
| 46 expect(() => new UrlPattern('())'), throws); |
| 47 }); |
| 48 |
| 49 test('patterns with fragments matches hash and path URLs', () { |
| 50 var pattern = new UrlPattern(r'/foo#(\w+)'); |
| 51 expect(pattern.matches('/foo#abc'), true); |
| 52 expect(pattern.matches('/foo/abc'), true); |
| 53 expect(pattern.reverse(['abc'], useFragment: true), '/foo#abc'); |
| 54 expect(pattern.reverse(['abc'], useFragment: false), '/foo/abc'); |
| 55 }); |
| 56 |
| 57 test('special chars outside groups', () { |
| 58 checkPattern('^', '^', []); |
| 59 checkPattern(r'$', r'$', []); |
| 60 checkPattern('.', '.', [], ['a']); |
| 61 checkPattern('|', '|', [], ['a']); |
| 62 checkPattern('+', '+', [], ['a']); |
| 63 checkPattern('[', '[', [], ['a']); |
| 64 checkPattern(']', ']', [], ['a']); |
| 65 checkPattern('{', '{', [], ['a']); |
| 66 checkPattern('}', '}', [], ['a']); |
| 67 }); |
| 68 |
| 69 test('matchesNonFragment() only matches the path', () { |
| 70 var pattern = new UrlPattern(r'/foo#(\w+)'); |
| 71 expect(pattern.matches('/foo'), false); |
| 72 expect(pattern.matchesNonFragment('/foo'), true); |
| 73 expect(() { new UrlPattern(r'(#)'); }, throws); |
| 74 expect(() { new UrlPattern(r'##'); }, throws); |
| 75 }); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Performs the following checks on a UrlPattern constructed from [p]: |
| 80 * * [url] matches the pattern. |
| 81 * * Using the pattern to parse [url] produces the arguments in [args]. |
| 82 * * Reversing the pattern with [args] produces the String [url]. |
| 83 * * None of the Strings in [nonMatches] match the pattern. |
| 84 */ |
| 85 checkPattern(String p, String url, List args, [List nonMatches]) { |
| 86 var pattern = new UrlPattern(p); |
| 87 expect(pattern.matches(url), true); |
| 88 expect(pattern.reverse(args), url); |
| 89 expect(pattern.parse(url), orderedEquals(args)); |
| 90 if (nonMatches != null) { |
| 91 for (var url in nonMatches) { |
| 92 expect(pattern.matches(url), false); |
| 93 } |
| 94 } |
| 95 } |
| OLD | NEW |