Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1499)

Side by Side Diff: utils/tests/css/src/SelectorLiteralTest.dart

Issue 8498020: Beginning of CSS parser using frog parsering infrastructure. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Incorporated Jim's CR Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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("../../../css/css.dart");
6
7 class SelectorLiteralTest {
8 static final String ERROR = 'CompilerException: <buffer>:';
9
10 static testMain() {
11 initCssWorld();
12
13 testSimpleClassSelectorSuccesses();
14 testSimpleClassSelectorFailures();
15 testPrivateNameFailures();
16 }
17
18 static void testSimpleClassSelectorSuccesses() {
19 List<String> knownClasses = ['foobar', 'xyzzy', 'a-story', 'b-story'];
20 List<String> knownIds = ['id1', 'id2', 'id-number-3'];
21
22 CssWorld cssWorld = new CssWorld(knownClasses, knownIds);
23
24 try {
25 // Valid selectors for class names.
26 cssParseAndValidate('@{.foobar}', cssWorld);
27 cssParseAndValidate('@{.foobar .xyzzy}', cssWorld);
28 cssParseAndValidate('@{.foobar .a-story .xyzzy}', cssWorld);
29 cssParseAndValidate('@{.foobar .xyzzy .a-story .b-story}', cssWorld);
30
31 // Valid selectors for element IDs.
32 cssParseAndValidate('@{#id1}', cssWorld);
33 cssParseAndValidate('@{#id-number-3}', cssWorld);
34 cssParseAndValidate('@{#_privateId}', cssWorld);
35
36 // Valid selectors for private class names (leading underscore).
37 cssParseAndValidate('@{.foobar ._privateClass}', cssWorld);
38 cssParseAndValidate('@{.foobar ._privateClass .xyzzy}', cssWorld);
39 cssParseAndValidate('@{.foobar ._private1 .xyzzy ._private2}', cssWorld);
40
41 // Valid selectors for private element IDs (leading underscore).
42 cssParseAndValidate('@{._privateClass}', cssWorld);
43 } catch (var e) {
44 // CSS Expressions failed
45 Expect.fail(e.toString());
46 }
47 }
48
49 static void testSimpleClassSelectorFailures() {
50 List<String> knownClasses = ['foobar', 'xyzzy', 'a-story', 'b-story'];
51 List<String> knownIds = ['id1', 'id2', 'id-number-3'];
52
53 CssWorld cssWorld = new CssWorld(knownClasses, knownIds);
54
55 // Invalid class name.
56 String css = '@{.-foobar}';
57 try {
58 cssParseAndValidate('${css}', cssWorld);
59 Expect.fail("${css} should not succeed.");
60 } catch (var e) {
61 Expect.equals("${ERROR}1:4: fatal: expected identifier\n${css}\n ^",
62 e.toString());
63 }
64
65 // Error this class name is not known.
66 css = '@{.foobar1}';
67 try {
68 cssParseAndValidate('${css}', cssWorld);
69 Expect.fail("${css} should not succeed.");
70 } catch (var e) {
71 Expect.equals("CssSelectorException: Unknown selector name .foobar1",
72 e.toString());
73 }
74
75 // Error if any class name is not known.
76 css = '@{.foobar .xyzzy1}';
77 try {
78 cssParseAndValidate('${css}', cssWorld);
79 Expect.fail("${css} should not succeed.");
80 } catch (var e) {
81 Expect.equals("CssSelectorException: Unknown selector name .xyzzy1",
82 e.toString());
83 }
84
85 // Test for invalid class name (can't start with number).
86 css = '@{.foobar .1a-story .xyzzy}';
87 try {
88 cssParseAndValidate('${css}', cssWorld);
89 Expect.fail("${css} should not succeed.");
90 } catch (var e) {
91 Expect.equals(
92 "${ERROR}1:12: fatal: expected identifier\n${css}\n ^^",
93 e.toString());
94 }
95
96 // element id must be single selector.
97 css = '@{#id1 #id2}';
98 try {
99 cssParseAndValidate('${css}', cssWorld);
100 Expect.fail("${css} should not succeed.");
101 } catch (var e) {
102 Expect.equals("CssSelectorException: Use of Id selector must be " +
103 "singleton starting at #id2", e.toString());
104 }
105
106 // element id must be single selector.
107 css = '@{#id-number-3 .foobar}';
108 try {
109 cssParseAndValidate('${css}', cssWorld);
110 Expect.fail("@{#id-number-3 .foobar} should not succeed.");
111 } catch (var e) {
112 // CSS Expressions failed
113 Expect.equals("CssSelectorException: Can not mix Id selector with "+
114 "class selector(s). Id selector must be singleton too many " +
115 "starting at .foobar", e.toString(), '');
116 }
117
118 // element id must be alone and only one element id.
119 css = '@{.foobar #id-number-3 #id1}';
120 try {
121 cssParseAndValidate('${css}', cssWorld);
122 Expect.fail("${css} should not succeed.");
123 } catch (var e) {
124 // CSS Expressions failed
125 Expect.equals("CssSelectorException: Use of Id selector must be " +
126 "singleton starting at #id-number-3", e.toString());
127 }
128
129 // Namespace selector not valid in @{css_expression}
130 css = '@{foo|div}';
131 try {
132 cssParseAndValidate('${css}', cssWorld);
133 Expect.fail("${css} should not succeed.");
134 } catch (var e) {
135 Expect.equals("CssSelectorException: Invalid selector foo|div",
136 e.toString());
137 }
138
139 // class and element id not allowed together.
140 css = '@{.foobar foo|div}';
141 try {
142 cssParseAndValidate('${css}', cssWorld);
143 Expect.fail("$css} should not succeed.");
144 } catch (var e) {
145 Expect.equals("CssSelectorException: Invalid selector foo|div",
146 e.toString());
147 }
148
149 // Element id and namespace not allowed together.
150 css = '@{#id1 foo|div}';
151 try {
152 cssParseAndValidate('${css}', cssWorld);
153 Expect.fail("${css} should not succeed.");
154 } catch (var e) {
155 Expect.equals("CssSelectorException: Invalid selector foo|div",
156 e.toString());
157 }
158
159 // namespace and element id not allowed together.
160 css = '@{foo|div #id1}';
161 try {
162 cssParseAndValidate('${css}', cssWorld);
163 Expect.fail("${css} should not succeed.");
164 } catch (var e) {
165 Expect.equals("CssSelectorException: Invalid selector foo|div",
166 e.toString());
167 }
168
169 // namespace / element not allowed.
170 css = '@{foo|div .foobar}';
171 try {
172 cssParseAndValidate('${css}', cssWorld);
173 Expect.fail("${css} should not succeed.");
174 } catch (var e) {
175 Expect.equals("CssSelectorException: Invalid selector foo|div",
176 e.toString());
177 }
178
179 // Combinators not allowed.
180 css = '@{.foobar > .xyzzy}';
181 try {
182 cssParseAndValidate('${css}', cssWorld);
183 Expect.fail("${css} should not succeed.");
184 } catch (var e) {
185 Expect.equals("CssSelectorException: Selectors can not have combinators (> , +, or ~) before .xyzzy",
186 e.toString());
187 }
188 }
189
190 static void testPrivateNameFailures() {
191 List<String> knownClasses = ['foobar', 'xyzzy', 'a-story', 'b-story'];
192 List<String> knownIds = ['id1', 'id2', 'id-number-3'];
193
194 CssWorld cssWorld = new CssWorld(knownClasses, knownIds);
195
196 // Too many.
197 String css = '@{._private #id2}';
198 try {
199 cssParseAndValidate('${css}', cssWorld);
200 Expect.fail("${css} should not succeed.");
201 } catch (var e) {
202 Expect.equals("CssSelectorException: Use of Id selector must be " +
203 "singleton starting at #id2", e.toString());
204 }
205
206 // Unknown class foobar2.
207 css = '@{._private .foobar2}';
208 try {
209 cssParseAndValidate('${css}', cssWorld);
210 Expect.fail("${css} should not succeed.");
211 } catch (var e) {
212 Expect.equals("CssSelectorException: Unknown selector name .foobar2",
213 e.toString());
214 }
215
216 // Too many element IDs.
217 css = '@{#_privateId #id2}';
218 try {
219 cssParseAndValidate('${css}', cssWorld);
220 Expect.fail("${css} should not succeed.");
221 } catch (var e) {
222 Expect.equals("CssSelectorException: Use of Id selector must be " +
223 "singleton starting at #id2", e.toString());
224 }
225
226 // Too many element IDs.
227 css = '@{#_privateId1 #_privateId2}';
228 try {
229 cssParseAndValidate('${css}', cssWorld);
230 Expect.fail("${css} should not succeed.");
231 } catch (var e) {
232 Expect.equals("CssSelectorException: Use of Id selector must be " +
233 "singleton starting at #_privateId2", e.toString());
234 }
235 }
236
237 }
238
239 main() {
240 SelectorLiteralTest.testMain();
241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698