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 error_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'testing.dart'; |
| 9 import 'package:csslib/parser.dart'; |
| 10 import 'package:csslib/visitor.dart'; |
| 11 import 'package:csslib/src/messages.dart'; |
| 12 |
| 13 /** |
| 14 * Test for unsupported font-weights values of bolder, lighter and inherit. |
| 15 */ |
| 16 void testUnsupportedFontWeights() { |
| 17 var errors = []; |
| 18 |
| 19 // TODO(terry): Need to support bolder. |
| 20 // font-weight value bolder. |
| 21 var input = ".foobar { font-weight: bolder; }"; |
| 22 var stylesheet = parseCss(input, errors: errors); |
| 23 |
| 24 expect(errors.isEmpty, false); |
| 25 expect(errors[0].toString(), r''' |
| 26 error :1:24: Unknown property value bolder |
| 27 .foobar { font-weight: bolder; } |
| 28 ^^^^^^'''); |
| 29 expect(stylesheet != null, true); |
| 30 |
| 31 expect(prettyPrint(stylesheet), r''' |
| 32 .foobar { |
| 33 font-weight: bolder; |
| 34 }'''); |
| 35 |
| 36 // TODO(terry): Need to support lighter. |
| 37 // font-weight value lighter. |
| 38 input = ".foobar { font-weight: lighter; }"; |
| 39 stylesheet = parseCss(input, errors: errors..clear()); |
| 40 |
| 41 expect(errors.isEmpty, false); |
| 42 expect(errors[0].toString(), r''' |
| 43 error :1:24: Unknown property value lighter |
| 44 .foobar { font-weight: lighter; } |
| 45 ^^^^^^^'''); |
| 46 expect(stylesheet != null, true); |
| 47 expect(prettyPrint(stylesheet), r''' |
| 48 .foobar { |
| 49 font-weight: lighter; |
| 50 }'''); |
| 51 |
| 52 // TODO(terry): Need to support inherit. |
| 53 // font-weight value inherit. |
| 54 input = ".foobar { font-weight: inherit; }"; |
| 55 stylesheet = parseCss(input, errors: errors..clear()); |
| 56 |
| 57 expect(errors.isEmpty, false); |
| 58 expect(errors[0].toString(), r''' |
| 59 error :1:24: Unknown property value inherit |
| 60 .foobar { font-weight: inherit; } |
| 61 ^^^^^^^'''); |
| 62 expect(stylesheet != null, true); |
| 63 expect(prettyPrint(stylesheet), r''' |
| 64 .foobar { |
| 65 font-weight: inherit; |
| 66 }'''); |
| 67 } |
| 68 |
| 69 /** |
| 70 * Test for unsupported line-height values of units other than px, pt and |
| 71 * inherit. |
| 72 */ |
| 73 void testUnsupportedLineHeights() { |
| 74 var errors = []; |
| 75 |
| 76 // line-height value in percentge unit. |
| 77 var input = ".foobar { line-height: 120%; }"; |
| 78 var stylesheet = parseCss(input, errors: errors); |
| 79 |
| 80 expect(errors.isEmpty, false); |
| 81 expect(errors[0].toString(), r''' |
| 82 error :1:24: Unexpected value for line-height |
| 83 .foobar { line-height: 120%; } |
| 84 ^^^'''); |
| 85 expect(stylesheet != null, true); |
| 86 expect(prettyPrint(stylesheet), r''' |
| 87 .foobar { |
| 88 line-height: 120%; |
| 89 }'''); |
| 90 |
| 91 // TODO(terry): Need to support all units. |
| 92 // line-height value in cm unit. |
| 93 input = ".foobar { line-height: 20cm; }"; |
| 94 stylesheet = parseCss(input, errors: errors..clear()); |
| 95 |
| 96 expect(errors.isEmpty, false); |
| 97 expect(errors[0].toString(), r''' |
| 98 error :1:24: Unexpected unit for line-height |
| 99 .foobar { line-height: 20cm; } |
| 100 ^^'''); |
| 101 expect(stylesheet != null, true); |
| 102 expect(prettyPrint(stylesheet), r''' |
| 103 .foobar { |
| 104 line-height: 20cm; |
| 105 }'''); |
| 106 |
| 107 // TODO(terry): Need to support inherit. |
| 108 // line-height value inherit. |
| 109 input = ".foobar { line-height: inherit; }"; |
| 110 stylesheet = parseCss(input, errors: errors..clear()); |
| 111 |
| 112 expect(errors.isEmpty, false); |
| 113 expect(errors[0].toString(), r''' |
| 114 error :1:24: Unknown property value inherit |
| 115 .foobar { line-height: inherit; } |
| 116 ^^^^^^^'''); |
| 117 expect(stylesheet != null, true); |
| 118 expect(prettyPrint(stylesheet), r''' |
| 119 .foobar { |
| 120 line-height: inherit; |
| 121 }'''); |
| 122 } |
| 123 |
| 124 /** Test for bad selectors. */ |
| 125 void testBadSelectors() { |
| 126 var errors = []; |
| 127 |
| 128 // Invalid id selector. |
| 129 var input = "# foo { color: #ff00ff; }"; |
| 130 var stylesheet = parseCss(input, errors: errors); |
| 131 |
| 132 expect(errors.isEmpty, false); |
| 133 expect(errors[0].toString(), r''' |
| 134 error :1:1: Not a valid ID selector expected #id |
| 135 # foo { color: #ff00ff; } |
| 136 ^'''); |
| 137 expect(stylesheet != null, true); |
| 138 expect(prettyPrint(stylesheet), r''' |
| 139 # foo { |
| 140 color: #f0f; |
| 141 }'''); |
| 142 |
| 143 // Invalid class selector. |
| 144 input = ". foo { color: #ff00ff; }"; |
| 145 stylesheet = parseCss(input, errors: errors..clear()); |
| 146 |
| 147 expect(errors.isEmpty, false); |
| 148 expect(errors[0].toString(), r''' |
| 149 error :1:1: Not a valid class selector expected .className |
| 150 . foo { color: #ff00ff; } |
| 151 ^'''); |
| 152 expect(stylesheet != null, true); |
| 153 expect(prettyPrint(stylesheet), r''' |
| 154 . foo { |
| 155 color: #f0f; |
| 156 }'''); |
| 157 } |
| 158 |
| 159 /** Test for bad hex values. */ |
| 160 void testBadHexValues() { |
| 161 var errors = []; |
| 162 |
| 163 // Invalid hex value. |
| 164 var input = ".foobar { color: #AH787; }"; |
| 165 var stylesheet = parseCss(input, errors: errors); |
| 166 |
| 167 expect(errors.isEmpty, false); |
| 168 expect(errors[0].toString(), r''' |
| 169 error :1:18: Bad hex number |
| 170 .foobar { color: #AH787; } |
| 171 ^^^^^^'''); |
| 172 expect(stylesheet != null, true); |
| 173 expect(prettyPrint(stylesheet), r''' |
| 174 .foobar { |
| 175 color: #AH787; |
| 176 }'''); |
| 177 |
| 178 // Bad color constant. |
| 179 input = ".foobar { color: redder; }"; |
| 180 stylesheet = parseCss(input, errors: errors..clear()); |
| 181 |
| 182 expect(errors.isEmpty, false); |
| 183 expect(errors[0].toString(), r''' |
| 184 error :1:18: Unknown property value redder |
| 185 .foobar { color: redder; } |
| 186 ^^^^^^'''); |
| 187 |
| 188 expect(stylesheet != null, true); |
| 189 expect(prettyPrint(stylesheet), r''' |
| 190 .foobar { |
| 191 color: redder; |
| 192 }'''); |
| 193 |
| 194 // Bad hex color #<space>ffffff. |
| 195 input = ".foobar { color: # ffffff; }"; |
| 196 stylesheet = parseCss(input, errors: errors..clear()); |
| 197 |
| 198 expect(errors.isEmpty, false); |
| 199 expect(errors[0].toString(), r''' |
| 200 error :1:18: Expected hex number |
| 201 .foobar { color: # ffffff; } |
| 202 ^'''); |
| 203 |
| 204 expect(stylesheet != null, true); |
| 205 expect(prettyPrint(stylesheet), r''' |
| 206 .foobar { |
| 207 color: # ffffff; |
| 208 }'''); |
| 209 |
| 210 // Bad hex color #<space>123fff. |
| 211 input = ".foobar { color: # 123fff; }"; |
| 212 stylesheet = parseCss(input, errors: errors..clear()); |
| 213 |
| 214 expect(errors.isEmpty, false); |
| 215 expect(errors[0].toString(), r''' |
| 216 error :1:18: Expected hex number |
| 217 .foobar { color: # 123fff; } |
| 218 ^'''); |
| 219 |
| 220 expect(stylesheet != null, true); |
| 221 |
| 222 // Formating is off with an extra space. However, the entire value is bad |
| 223 // and isn't processed anyway. |
| 224 expect(prettyPrint(stylesheet), r''' |
| 225 .foobar { |
| 226 color: # 123 fff; |
| 227 }'''); |
| 228 |
| 229 } |
| 230 |
| 231 void testBadUnicode() { |
| 232 var errors = []; |
| 233 final String input = ''' |
| 234 @font-face { |
| 235 src: url(fonts/BBCBengali.ttf) format("opentype"); |
| 236 unicode-range: U+400-200; |
| 237 }'''; |
| 238 |
| 239 var stylesheet = parseCss(input, errors: errors); |
| 240 |
| 241 expect(errors.isEmpty, false); |
| 242 expect(errors[0].toString(), |
| 243 'error :3:20: unicode first range can not be greater than last\n' |
| 244 ' unicode-range: U+400-200;\n' |
| 245 ' ^^^^^^^'); |
| 246 |
| 247 final String input2 = ''' |
| 248 @font-face { |
| 249 src: url(fonts/BBCBengali.ttf) format("opentype"); |
| 250 unicode-range: U+12FFFF; |
| 251 }'''; |
| 252 |
| 253 stylesheet = parseCss(input2, errors: errors..clear()); |
| 254 |
| 255 expect(errors.isEmpty, false); |
| 256 expect(errors[0].toString(), |
| 257 'error :3:20: unicode range must be less than 10FFFF\n' |
| 258 ' unicode-range: U+12FFFF;\n' |
| 259 ' ^^^^^^'); |
| 260 } |
| 261 |
| 262 void testBadNesting() { |
| 263 var errors = []; |
| 264 |
| 265 // Test for bad declaration in a nested rule. |
| 266 final String input = ''' |
| 267 div { |
| 268 width: 20px; |
| 269 span + ul { color: blue; } |
| 270 span + ul > #aaaa { |
| 271 color: #ffghghgh; |
| 272 } |
| 273 background-color: red; |
| 274 } |
| 275 '''; |
| 276 |
| 277 var stylesheet = parseCss(input, errors: errors); |
| 278 expect(errors.length, 1); |
| 279 var errorMessage = messages.messages[0]; |
| 280 expect(errorMessage.message, contains('Bad hex number')); |
| 281 expect(errorMessage.span, isNotNull); |
| 282 expect(errorMessage.span.start.line, 4); |
| 283 expect(errorMessage.span.start.column, 11); |
| 284 expect(errorMessage.span.text, '#ffghghgh'); |
| 285 |
| 286 // Test for bad selector syntax. |
| 287 final String input2 = ''' |
| 288 div { |
| 289 span + ul #aaaa > (3333) { |
| 290 color: #ffghghgh; |
| 291 } |
| 292 } |
| 293 '''; |
| 294 var stylesheet2 = parseCss(input2, errors: errors..clear()); |
| 295 expect(errors.length, 4); |
| 296 errorMessage = messages.messages[0]; |
| 297 expect(errorMessage.message, contains(':, but found +')); |
| 298 expect(errorMessage.span, isNotNull); |
| 299 expect(errorMessage.span.start.line, 1); |
| 300 expect(errorMessage.span.start.column, 7); |
| 301 expect(errorMessage.span.text, '+'); |
| 302 |
| 303 errorMessage = messages.messages[1]; |
| 304 expect(errorMessage.message, contains('Unknown property value ul')); |
| 305 expect(errorMessage.span, isNotNull); |
| 306 expect(errorMessage.span.start.line, 1); |
| 307 expect(errorMessage.span.start.column, 9); |
| 308 expect(errorMessage.span.text, 'ul'); |
| 309 |
| 310 errorMessage = messages.messages[2]; |
| 311 expect(errorMessage.message, contains('expected }, but found >')); |
| 312 expect(errorMessage.span, isNotNull); |
| 313 expect(errorMessage.span.start.line, 1); |
| 314 expect(errorMessage.span.start.column, 18); |
| 315 expect(errorMessage.span.text, '>'); |
| 316 |
| 317 errorMessage = messages.messages[3]; |
| 318 expect(errorMessage.message, contains('premature end of file unknown CSS')); |
| 319 expect(errorMessage.span, isNotNull); |
| 320 expect(errorMessage.span.start.line, 1); |
| 321 expect(errorMessage.span.start.column, 20); |
| 322 expect(errorMessage.span.text, '('); |
| 323 |
| 324 // Test for missing close braces and bad declaration. |
| 325 final String input3 = ''' |
| 326 div { |
| 327 span { |
| 328 color: #green; |
| 329 } |
| 330 '''; |
| 331 var stylesheet3 = parseCss(input3, errors: errors..clear()); |
| 332 expect(errors.length, 2); |
| 333 errorMessage = messages.messages[0]; |
| 334 expect(errorMessage.message, contains('Bad hex number')); |
| 335 expect(errorMessage.span, isNotNull); |
| 336 expect(errorMessage.span.start.line, 2); |
| 337 expect(errorMessage.span.start.column, 11); |
| 338 expect(errorMessage.span.text, '#green'); |
| 339 |
| 340 errorMessage = messages.messages[1]; |
| 341 expect(errorMessage.message, contains('expected }, but found end of file')); |
| 342 expect(errorMessage.span, isNotNull); |
| 343 expect(errorMessage.span.start.line, 3); |
| 344 expect(errorMessage.span.start.column, 1); |
| 345 expect(errorMessage.span.text, '\n'); |
| 346 } |
| 347 |
| 348 main() { |
| 349 test('font-weight value errors', testUnsupportedFontWeights); |
| 350 test('line-height value errors', testUnsupportedLineHeights); |
| 351 test('bad selectors', testBadSelectors); |
| 352 test('bad Hex values', testBadHexValues); |
| 353 test('bad unicode ranges', testBadUnicode); |
| 354 test('nested rules', testBadNesting); |
| 355 } |
OLD | NEW |