| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for Web Development Style Guide checker.""" | 6 """Unit tests for Web Development Style Guide checker.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 import sys | 10 import sys |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 self.fake_file.LocalPath().AndReturn(self.fake_file_name) | 181 self.fake_file.LocalPath().AndReturn(self.fake_file_name) |
| 182 # Actual calls to NewContents() are defined in each test. | 182 # Actual calls to NewContents() are defined in each test. |
| 183 self.mox.StubOutWithMock(self.fake_file, 'NewContents') | 183 self.mox.StubOutWithMock(self.fake_file, 'NewContents') |
| 184 | 184 |
| 185 self.input_api = self.mox.CreateMockAnything() | 185 self.input_api = self.mox.CreateMockAnything() |
| 186 self.input_api.re = re | 186 self.input_api.re = re |
| 187 self.mox.StubOutWithMock(self.input_api, 'AffectedSourceFiles') | 187 self.mox.StubOutWithMock(self.input_api, 'AffectedSourceFiles') |
| 188 self.input_api.AffectedFiles( | 188 self.input_api.AffectedFiles( |
| 189 include_deletes=False, file_filter=None).AndReturn([self.fake_file]) | 189 include_deletes=False, file_filter=None).AndReturn([self.fake_file]) |
| 190 | 190 |
| 191 # Actual creations of PresubmitError are defined in each test. | 191 # Actual creations of PresubmitPromptWarning are defined in each test. |
| 192 self.output_api = self.mox.CreateMockAnything() | 192 self.output_api = self.mox.CreateMockAnything() |
| 193 self.mox.StubOutWithMock(self.output_api, 'PresubmitError', | 193 self.mox.StubOutWithMock(self.output_api, 'PresubmitPromptWarning', |
| 194 use_mock_anything=True) | 194 use_mock_anything=True) |
| 195 | 195 |
| 196 author_msg = ('Was the CSS checker useful? ' | 196 author_msg = ('Was the CSS checker useful? ' |
| 197 'Send feedback or hate mail to dbeam@chromium.org.') | 197 'Send feedback or hate mail to dbeam@chromium.org.') |
| 198 self.output_api = self.mox.CreateMockAnything() | 198 self.output_api = self.mox.CreateMockAnything() |
| 199 self.mox.StubOutWithMock(self.output_api, 'PresubmitNotifyResult', | 199 self.mox.StubOutWithMock(self.output_api, 'PresubmitNotifyResult', |
| 200 use_mock_anything=True) | 200 use_mock_anything=True) |
| 201 self.output_api.PresubmitNotifyResult(author_msg).AndReturn(None) | 201 self.output_api.PresubmitNotifyResult(author_msg).AndReturn(None) |
| 202 | 202 |
| 203 def VerifyContentsProducesOutput(self, contents, output): | 203 def VerifyContentsProducesOutput(self, contents, output): |
| 204 self.fake_file.NewContents().AndReturn(contents.splitlines()) | 204 self.fake_file.NewContents().AndReturn(contents.splitlines()) |
| 205 self.output_api.PresubmitError( | 205 self.output_api.PresubmitPromptWarning( |
| 206 self.fake_file_name + ':\n' + output.strip()).AndReturn(None) | 206 self.fake_file_name + ':\n' + output.strip()).AndReturn(None) |
| 207 self.mox.ReplayAll() | 207 self.mox.ReplayAll() |
| 208 css_checker.CSSChecker(self.input_api, self.output_api).RunChecks() | 208 css_checker.CSSChecker(self.input_api, self.output_api).RunChecks() |
| 209 | 209 |
| 210 def testCssAlphaWithAtBlock(self): | 210 def testCssAlphaWithAtBlock(self): |
| 211 self.VerifyContentsProducesOutput(""" | 211 self.VerifyContentsProducesOutput(""" |
| 212 <include src="../shared/css/cr/ui/overlay.css"> | 212 <include src="../shared/css/cr/ui/overlay.css"> |
| 213 <include src="chrome://resources/totally-cool.css" /> | 213 <include src="chrome://resources/totally-cool.css" /> |
| 214 | 214 |
| 215 /* A hopefully safely ignored comment and @media statement. /**/ | 215 /* A hopefully safely ignored comment and @media statement. /**/ |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 .ClassName, | 298 .ClassName, |
| 299 .class_name {""") | 299 .class_name {""") |
| 300 | 300 |
| 301 def testCssCloseBraceOnNewLine(self): | 301 def testCssCloseBraceOnNewLine(self): |
| 302 self.VerifyContentsProducesOutput(""" | 302 self.VerifyContentsProducesOutput(""" |
| 303 @media { /* TODO(dbeam) Fix this case. */ | 303 @media { /* TODO(dbeam) Fix this case. */ |
| 304 .rule { | 304 .rule { |
| 305 display: block; | 305 display: block; |
| 306 }} | 306 }} |
| 307 | 307 |
| 308 @-webkit-keyframe blah { |
| 309 100% { height: -500px 0; } |
| 310 } |
| 311 |
| 308 #rule { | 312 #rule { |
| 309 rule: value; }""", """ | 313 rule: value; }""", """ |
| 310 - Always put a rule closing brace (}) on a new line. | 314 - Always put a rule closing brace (}) on a new line. |
| 311 rule: value; }""") | 315 rule: value; }""") |
| 312 | 316 |
| 313 def testCssColonsHaveSpaceAfter(self): | 317 def testCssColonsHaveSpaceAfter(self): |
| 314 self.VerifyContentsProducesOutput(""" | 318 self.VerifyContentsProducesOutput(""" |
| 315 div:not(.class):not([attr=5]), /* We should not catch this. */ | 319 div:not(.class):not([attr=5]), /* We should not catch this. */ |
| 316 div:not(.class):not([attr]) /* Nor this. */ { | 320 div:not(.class):not([attr]) /* Nor this. */ { |
| 317 background: -webkit-linear-gradient(left, red, | 321 background: -webkit-linear-gradient(left, red, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 339 | 343 |
| 340 def testCssHexCouldBeShorter(self): | 344 def testCssHexCouldBeShorter(self): |
| 341 self.VerifyContentsProducesOutput(""" | 345 self.VerifyContentsProducesOutput(""" |
| 342 #abc, | 346 #abc, |
| 343 #abc-, | 347 #abc-, |
| 344 #abc-ghij, | 348 #abc-ghij, |
| 345 #abcdef-, | 349 #abcdef-, |
| 346 #abcdef-ghij, | 350 #abcdef-ghij, |
| 347 #aaaaaa, | 351 #aaaaaa, |
| 348 #bbaacc { | 352 #bbaacc { |
| 353 background-color: #336699; /* Ignore short hex rule if not gray. */ |
| 349 color: #999999; | 354 color: #999999; |
| 350 color: #666; | 355 color: #666; |
| 351 }""", """ | 356 }""", """ |
| 352 - Use abbreviated hex (#rgb) when in form #rrggbb. | 357 - Use abbreviated hex (#rgb) when in form #rrggbb. |
| 353 color: #999999; (replace with #999)""") | 358 color: #999999; (replace with #999) |
| 359 |
| 360 - Use rgb() over #hex when not a shade of gray (like #333). |
| 361 background-color: #336699; (replace with rgb(51, 102, 153))""") |
| 354 | 362 |
| 355 def testCssUseMillisecondsForSmallTimes(self): | 363 def testCssUseMillisecondsForSmallTimes(self): |
| 356 self.VerifyContentsProducesOutput(""" | 364 self.VerifyContentsProducesOutput(""" |
| 357 .transition-0s /* This is gross but may happen. */ { | 365 .transition-0s /* This is gross but may happen. */ { |
| 358 transform: one 0.2s; | 366 transform: one 0.2s; |
| 359 transform: two .1s; | 367 transform: two .1s; |
| 360 transform: tree 1s; | 368 transform: tree 1s; |
| 361 transform: four 300ms; | 369 transform: four 300ms; |
| 362 }""", """ | 370 }""", """ |
| 363 - Use milliseconds for time measurements under 1 second. | 371 - Use milliseconds for time measurements under 1 second. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 377 - One rule per line (what not to do: color: red; margin: 0;). | 385 - One rule per line (what not to do: color: red; margin: 0;). |
| 378 rule: value; rule: value;""") | 386 rule: value; rule: value;""") |
| 379 | 387 |
| 380 def testCssOneSelectorPerLine(self): | 388 def testCssOneSelectorPerLine(self): |
| 381 self.VerifyContentsProducesOutput(""" | 389 self.VerifyContentsProducesOutput(""" |
| 382 a, | 390 a, |
| 383 div,a, | 391 div,a, |
| 384 div,/* Hello! */ span, | 392 div,/* Hello! */ span, |
| 385 #id.class([dir=rtl):not(.class):any(a, b, d) { | 393 #id.class([dir=rtl):not(.class):any(a, b, d) { |
| 386 rule: value; | 394 rule: value; |
| 395 } |
| 396 |
| 397 a, |
| 398 div,a { |
| 399 some-other: rule here; |
| 387 }""", """ | 400 }""", """ |
| 388 - One selector per line (what not to do: a, b {}). | 401 - One selector per line (what not to do: a, b {}). |
| 389 div,a, | 402 div,a, |
| 390 div, span,""") | 403 div, span, |
| 391 | 404 div,a {""") |
| 392 | 405 |
| 393 def testCssRgbIfNotGray(self): | 406 def testCssRgbIfNotGray(self): |
| 394 self.VerifyContentsProducesOutput(""" | 407 self.VerifyContentsProducesOutput(""" |
| 395 #abc, | 408 #abc, |
| 396 #aaa, | 409 #aaa, |
| 397 #aabbcc { | 410 #aabbcc { |
| 398 background: -webkit-linear-gradient(left, from(#abc), to(#def)); | 411 background: -webkit-linear-gradient(left, from(#abc), to(#def)); |
| 399 color: #bad; | 412 color: #bad; |
| 400 color: #bada55; | 413 color: #bada55; |
| 401 }""", """ | 414 }""", """ |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 background-position-x: 0em; | 461 background-position-x: 0em; |
| 449 background-position-y: 0ex; | 462 background-position-y: 0ex; |
| 450 border-width: 0em; | 463 border-width: 0em; |
| 451 border-width: 0mm; | 464 border-width: 0mm; |
| 452 height: 0cm; | 465 height: 0cm; |
| 453 width: 0in; | 466 width: 0in; |
| 454 """) | 467 """) |
| 455 | 468 |
| 456 if __name__ == '__main__': | 469 if __name__ == '__main__': |
| 457 unittest.main() | 470 unittest.main() |
| OLD | NEW |