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