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 import "dart:collection"; | |
6 | |
7 import "package:expect/expect.dart"; | |
8 | |
9 void testInvalidArguments() {} | |
10 | |
11 void testPath() { | |
12 void test(s, uri) { | |
13 Expect.equals(s, uri.toString()); | |
14 Expect.equals(uri, Uri.parse(s)); | |
15 } | |
16 | |
17 test("http:", new Uri(scheme: "http")); | |
18 test("http://host/xxx", new Uri(scheme: "http", host: "host", path: "xxx")); | |
19 test("http://host/xxx", new Uri(scheme: "http", host: "host", path: "/xxx")); | |
20 test("http://host/xxx", | |
21 new Uri(scheme: "http", host: "host", pathSegments: ["xxx"])); | |
22 test("http://host/xxx/yyy", | |
23 new Uri(scheme: "http", host: "host", path: "xxx/yyy")); | |
24 test("http://host/xxx/yyy", | |
25 new Uri(scheme: "http", host: "host", path: "/xxx/yyy")); | |
26 test("http://host/xxx/yyy", | |
27 new Uri(scheme: "http", host: "host", pathSegments: ["xxx", "yyy"])); | |
28 | |
29 test("urn:", new Uri(scheme: "urn")); | |
30 test("urn:xxx", new Uri(scheme: "urn", path: "xxx")); | |
31 test("urn:xxx:yyy", new Uri(scheme: "urn", path: "xxx:yyy")); | |
32 | |
33 Expect.equals(3, new Uri(path: "xxx/yyy/zzz").pathSegments.length); | |
34 Expect.equals(3, new Uri(path: "/xxx/yyy/zzz").pathSegments.length); | |
35 Expect.equals(3, Uri.parse("http://host/xxx/yyy/zzz").pathSegments.length); | |
36 Expect.equals(3, Uri.parse("file:///xxx/yyy/zzz").pathSegments.length); | |
37 } | |
38 | |
39 void testPathSegments() { | |
40 void test(String path, List<String> segments) { | |
41 void check(uri) { | |
42 Expect.equals(path, uri.path); | |
43 Expect.equals(path, uri.toString()); | |
44 Expect.listEquals(segments, uri.pathSegments); | |
45 } | |
46 | |
47 var uri1 = new Uri(pathSegments: segments); | |
48 var uri2 = new Uri(path: path); | |
49 check(uri1); | |
50 check(uri2); | |
51 Expect.equals(uri1, uri2); | |
52 } | |
53 | |
54 test("", []); | |
55 test("%20", [" "]); | |
56 test("%20/%20%20", [" ", " "]); | |
57 test("A", ["A"]); | |
58 test("%C3%B8", ["ø"]); | |
59 test("%C3%B8/%C3%A5", ["ø", "å"]); | |
60 test("%C8%A4/%E5%B9%B3%E4%BB%AE%E5%90%8D", ["Ȥ", "平仮名"]); | |
61 test("A/b", ["A", "b"]); | |
62 test("A/%25", ["A", "%"]); | |
63 test("%2F/a%2Fb", ["/", "a/b"]); | |
64 test("name;v=1.1", ["name;v=1.1"]); | |
65 test("name,v=1.1", ["name,v=1.1"]); | |
66 test("name;v=1.1/name,v=1.1", ["name;v=1.1", "name,v=1.1"]); | |
67 | |
68 var unreserved = "-._~0123456789" | |
69 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
70 "abcdefghijklmnopqrstuvwxyz"; | |
71 var subDelimiters = r"!$&'()*+,;="; | |
72 var additionalPathChars = ":@"; | |
73 var pchar = unreserved + subDelimiters + additionalPathChars; | |
74 | |
75 var encoded = new StringBuffer(); | |
76 var unencoded = new StringBuffer(); | |
77 for (int i = 32; i < 128; i++) { | |
78 if (pchar.indexOf(new String.fromCharCode(i)) != -1) { | |
79 encoded.writeCharCode(i); | |
80 } else { | |
81 encoded.write("%"); | |
82 encoded.write(i.toRadixString(16).toUpperCase()); | |
83 } | |
84 unencoded.writeCharCode(i); | |
85 } | |
86 encoded = encoded.toString(); | |
87 unencoded = unencoded.toString(); | |
88 test(encoded, [unencoded]); | |
89 test(encoded + "/" + encoded, [unencoded, unencoded]); | |
90 | |
91 Uri uri; | |
92 List pathSegments = ["xxx", "yyy", "zzz"]; | |
93 | |
94 uri = new Uri(pathSegments: pathSegments); | |
95 Expect.equals(3, uri.pathSegments.length); | |
96 uri = new Uri(pathSegments: pathSegments.where((_) => true)); | |
97 Expect.equals(3, uri.pathSegments.length); | |
98 uri = new Uri(pathSegments: new DoubleLinkedQueue.from(pathSegments)); | |
99 Expect.equals(3, uri.pathSegments.length); | |
100 | |
101 uri = new Uri(scheme: "http", host: "host", pathSegments: pathSegments); | |
102 Expect.equals(3, uri.pathSegments.length); | |
103 uri = new Uri( | |
104 scheme: "http", | |
105 host: "host", | |
106 pathSegments: pathSegments.where((_) => true)); | |
107 Expect.equals(3, uri.pathSegments.length); | |
108 uri = new Uri( | |
109 scheme: "http", | |
110 host: "host", | |
111 pathSegments: new DoubleLinkedQueue.from(pathSegments)); | |
112 Expect.equals(3, uri.pathSegments.length); | |
113 | |
114 uri = new Uri(scheme: "file", pathSegments: pathSegments); | |
115 Expect.equals(3, uri.pathSegments.length); | |
116 uri = new Uri(scheme: "file", pathSegments: pathSegments.where((_) => true)); | |
117 Expect.equals(3, uri.pathSegments.length); | |
118 uri = new Uri( | |
119 scheme: "file", pathSegments: new DoubleLinkedQueue.from(pathSegments)); | |
120 Expect.equals(3, uri.pathSegments.length); | |
121 } | |
122 | |
123 void testPathCompare() { | |
124 void test(Uri uri1, Uri uri2) { | |
125 Expect.equals(uri1, uri2); | |
126 Expect.equals(uri2, uri1); | |
127 } | |
128 | |
129 test(new Uri(scheme: "http", host: "host", path: "xxx"), | |
130 new Uri(scheme: "http", host: "host", path: "/xxx")); | |
131 test(new Uri(scheme: "http", host: "host", pathSegments: ["xxx"]), | |
132 new Uri(scheme: "http", host: "host", path: "/xxx")); | |
133 test(new Uri(scheme: "http", host: "host", pathSegments: ["xxx"]), | |
134 new Uri(scheme: "http", host: "host", path: "xxx")); | |
135 test(new Uri(scheme: "file", path: "xxx"), | |
136 new Uri(scheme: "file", path: "/xxx")); | |
137 test(new Uri(scheme: "file", pathSegments: ["xxx"]), | |
138 new Uri(scheme: "file", path: "/xxx")); | |
139 test(new Uri(scheme: "file", pathSegments: ["xxx"]), | |
140 new Uri(scheme: "file", path: "xxx")); | |
141 } | |
142 | |
143 testPathSegmentsUnmodifiableList() { | |
144 void test(list) { | |
145 bool isUnsupported(e) => e is UnsupportedError; | |
146 | |
147 Expect.equals("a", list[0]); | |
148 Expect.throws(() => list[0] = "c", isUnsupported); | |
149 Expect.equals(2, list.length); | |
150 Expect.throws(() => list.length = 1, isUnsupported); | |
151 Expect.throws(() => list.add("c"), isUnsupported); | |
152 Expect.throws(() => list.addAll(["c", "d"]), isUnsupported); | |
153 Expect.listEquals(["b", "a"], list.reversed.toList()); | |
154 Expect.throws(() => list.sort(), isUnsupported); | |
155 Expect.equals(0, list.indexOf("a")); | |
156 Expect.equals(0, list.lastIndexOf("a")); | |
157 Expect.throws(() => list.clear(), isUnsupported); | |
158 Expect.throws(() => list.insert(1, "c"), isUnsupported); | |
159 Expect.throws(() => list.insertAll(1, ["c", "d"]), isUnsupported); | |
160 Expect.throws(() => list.setAll(1, ["c", "d"]), isUnsupported); | |
161 Expect.throws(() => list.remove("a"), isUnsupported); | |
162 Expect.throws(() => list.removeAt(0), isUnsupported); | |
163 Expect.throws(() => list.removeLast(), isUnsupported); | |
164 Expect.throws(() => list.removeWhere((e) => true), isUnsupported); | |
165 Expect.throws(() => list.retainWhere((e) => false), isUnsupported); | |
166 Expect.listEquals(["a"], list.sublist(0, 1)); | |
167 Expect.listEquals(["a"], list.getRange(0, 1).toList()); | |
168 Expect.throws(() => list.setRange(0, 1, ["c"]), isUnsupported); | |
169 Expect.throws(() => list.removeRange(0, 1), isUnsupported); | |
170 Expect.throws(() => list.fillRange(0, 1, "c"), isUnsupported); | |
171 Expect.throws(() => list.replaceRange(0, 1, ["c"]), isUnsupported); | |
172 Map map = new Map(); | |
173 map[0] = "a"; | |
174 map[1] = "b"; | |
175 Expect.mapEquals(list.asMap(), map); | |
176 } | |
177 | |
178 test(Uri.parse("a/b").pathSegments); | |
179 test(new Uri(pathSegments: ["a", "b"]).pathSegments); | |
180 } | |
181 | |
182 main() { | |
183 testInvalidArguments(); | |
184 testPath(); | |
185 testPathSegments(); | |
186 testPathCompare(); | |
187 testPathSegmentsUnmodifiableList(); | |
188 } | |
OLD | NEW |