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