| OLD | NEW |
| (Empty) | |
| 1 library url_template_test; |
| 2 |
| 3 import 'package:unittest/unittest.dart'; |
| 4 import 'package:route_hierarchical/url_template.dart'; |
| 5 import 'package:route_hierarchical/url_matcher.dart'; |
| 6 |
| 7 main() { |
| 8 group('UrlTemplate', () { |
| 9 test('should work with simple templates', () { |
| 10 var tmpl = new UrlTemplate('/foo/bar:baz/aux'); |
| 11 expect(tmpl.match('/foo/bar123/aux'), |
| 12 new UrlMatch('/foo/bar123/aux', '', { |
| 13 'baz': '123' |
| 14 })); |
| 15 |
| 16 tmpl = new UrlTemplate('/foo/:bar'); |
| 17 expect(tmpl.match('/foo/123'), new UrlMatch('/foo/123', '', { |
| 18 'bar': '123' |
| 19 })); |
| 20 |
| 21 tmpl = new UrlTemplate('/:foo/bar'); |
| 22 expect(tmpl.match('/123/bar'), new UrlMatch('/123/bar', '', { |
| 23 'foo': '123' |
| 24 })); |
| 25 |
| 26 tmpl = new UrlTemplate('/user/:userId/article/:articleId/view'); |
| 27 UrlMatch params = |
| 28 tmpl.match('/user/jsmith/article/1234/view/someotherstuff'); |
| 29 expect(params, new UrlMatch('/user/jsmith/article/1234/view', |
| 30 '/someotherstuff', { |
| 31 'userId': 'jsmith', |
| 32 'articleId': '1234' |
| 33 })); |
| 34 |
| 35 params = tmpl.match('/user/jsmith/article/1234/edit'); |
| 36 expect(params, isNull); |
| 37 |
| 38 tmpl = new UrlTemplate(r'/foo/:bar$123/aux'); |
| 39 expect(tmpl.match(r'/foo/123$123/aux'), |
| 40 new UrlMatch(r'/foo/123$123/aux', '', { |
| 41 'bar': '123' |
| 42 })); |
| 43 }); |
| 44 |
| 45 test('should work with special characters', () { |
| 46 var tmpl = new UrlTemplate(r'\^\|+[]{}()'); |
| 47 expect(tmpl.match(r'\^\|+[]{}()'), new UrlMatch(r'\^\|+[]{}()', '', {})); |
| 48 |
| 49 tmpl = new UrlTemplate(r'/:foo/^\|+[]{}()'); |
| 50 expect(tmpl.match(r'/123/^\|+[]{}()'), |
| 51 new UrlMatch(r'/123/^\|+[]{}()', '', { |
| 52 'foo': '123' |
| 53 })); |
| 54 }); |
| 55 |
| 56 test('should only match prefix', () { |
| 57 var tmpl = new UrlTemplate(r'/foo'); |
| 58 expect(tmpl.match(r'/foo/foo/bar'), |
| 59 new UrlMatch(r'/foo', '/foo/bar', {})); |
| 60 }); |
| 61 |
| 62 test('should reverse', () { |
| 63 var tmpl = new UrlTemplate('/:a/:b/:c'); |
| 64 expect(tmpl.reverse(), '/null/null/null'); |
| 65 expect(tmpl.reverse(parameters: { |
| 66 'a': 'foo', |
| 67 'b': 'bar', |
| 68 'c': 'baz' |
| 69 }), '/foo/bar/baz'); |
| 70 |
| 71 tmpl = new UrlTemplate(':a/bar/baz'); |
| 72 expect(tmpl.reverse(), 'null/bar/baz'); |
| 73 expect(tmpl.reverse(parameters: { |
| 74 'a': '/foo', |
| 75 }), '/foo/bar/baz'); |
| 76 |
| 77 tmpl = new UrlTemplate('/foo/bar/:c'); |
| 78 expect(tmpl.reverse(), '/foo/bar/null'); |
| 79 expect(tmpl.reverse(parameters: { |
| 80 'c': 'baz', |
| 81 }), '/foo/bar/baz'); |
| 82 |
| 83 tmpl = new UrlTemplate('/foo/bar/:c'); |
| 84 expect(tmpl.reverse(tail: '/tail', parameters: { |
| 85 'c': 'baz', |
| 86 }), '/foo/bar/baz/tail'); |
| 87 }); |
| 88 }); |
| 89 } |
| OLD | NEW |