| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 |
| 7 """Unit tests for Web Development Style Guide checker.""" |
| 8 |
| 9 |
| 10 import os |
| 11 import re |
| 12 import sys |
| 13 import unittest |
| 14 |
| 15 print __file__ |
| 16 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 17 |
| 18 from testing_support.super_mox import SuperMoxTestBase |
| 19 from web_dev_style import css_checker |
| 20 |
| 21 |
| 22 class WebDevStyleGuideTest(SuperMoxTestBase): |
| 23 def setUp(self): |
| 24 SuperMoxTestBase.setUp(self) |
| 25 |
| 26 self.fake_file_name = 'fake.css' |
| 27 |
| 28 self.fake_file = self.mox.CreateMockAnything() |
| 29 self.mox.StubOutWithMock(self.fake_file, 'LocalPath') |
| 30 self.fake_file.LocalPath().AndReturn(self.fake_file_name) |
| 31 # Actual calls to NewContents() are defined in each test. |
| 32 self.mox.StubOutWithMock(self.fake_file, 'NewContents') |
| 33 |
| 34 self.input_api = self.mox.CreateMockAnything() |
| 35 self.input_api.re = re |
| 36 self.mox.StubOutWithMock(self.input_api, 'AffectedSourceFiles') |
| 37 self.input_api.AffectedFiles( |
| 38 include_deletes=False, file_filter=None).AndReturn([self.fake_file]) |
| 39 |
| 40 # Actual creations of PresubmitNotifyResult are defined in each test. |
| 41 self.output_api = self.mox.CreateMockAnything() |
| 42 self.mox.StubOutWithMock(self.output_api, 'PresubmitNotifyResult', |
| 43 use_mock_anything=True) |
| 44 |
| 45 def VerifyContentsProducesOutput(self, contents, output): |
| 46 self.fake_file.NewContents().AndReturn(contents.splitlines()) |
| 47 self.output_api.PresubmitNotifyResult( |
| 48 self.fake_file_name + ':\n' + output.strip()).AndReturn(None) |
| 49 self.mox.ReplayAll() |
| 50 css_checker.CSSChecker(self.input_api, self.output_api).RunChecks() |
| 51 |
| 52 def testCssAlphaWithAtBlock(self): |
| 53 self.VerifyContentsProducesOutput(""" |
| 54 /* A hopefully safely ignored comment and @media statement. /**/ |
| 55 @media print { |
| 56 div { |
| 57 display: block; |
| 58 color: red; |
| 59 } |
| 60 }""", """ |
| 61 - Alphabetize properties and list vendor specific (i.e. -webkit) above standard. |
| 62 display: block; |
| 63 color: red;""") |
| 64 |
| 65 def testCssAlphaWithNonStandard(self): |
| 66 self.VerifyContentsProducesOutput(""" |
| 67 div { |
| 68 /* A hopefully safely ignored comment and @media statement. /**/ |
| 69 color: red; |
| 70 -webkit-margin-start: 5px; |
| 71 }""", """ |
| 72 - Alphabetize properties and list vendor specific (i.e. -webkit) above standard. |
| 73 color: red; |
| 74 -webkit-margin-start: 5px;""") |
| 75 |
| 76 def testCssAlphaWithLongerDashedProps(self): |
| 77 self.VerifyContentsProducesOutput(""" |
| 78 div { |
| 79 border-left: 5px; /* A hopefully removed comment. */ |
| 80 border: 5px solid red; |
| 81 }""", """ |
| 82 - Alphabetize properties and list vendor specific (i.e. -webkit) above standard. |
| 83 border-left: 5px; |
| 84 border: 5px solid red;""") |
| 85 |
| 86 def testCssBracesHaveSpaceBeforeAndNothingAfter(self): |
| 87 self.VerifyContentsProducesOutput(""" |
| 88 /* Hello! */div/* Comment here*/{ |
| 89 display: block; |
| 90 } |
| 91 |
| 92 blah /* hey! */ |
| 93 { |
| 94 rule: value; |
| 95 } |
| 96 |
| 97 .this.is { /* allowed */ |
| 98 rule: value; |
| 99 }""", """ |
| 100 - Start braces ({) end a selector, have a space before them and no rules after. |
| 101 div{ |
| 102 {""") |
| 103 |
| 104 def testCssClassesUseDashes(self): |
| 105 self.VerifyContentsProducesOutput(""" |
| 106 .className, |
| 107 .ClassName, |
| 108 .class-name /* We should not catch this. */, |
| 109 .class_name { |
| 110 display: block; |
| 111 }""", """ |
| 112 - Classes use .dash-form. |
| 113 .className, |
| 114 .ClassName, |
| 115 .class_name {""") |
| 116 |
| 117 def testCssCloseBraceOnNewLine(self): |
| 118 self.VerifyContentsProducesOutput(""" |
| 119 @media { /* TODO(dbeam) Fix this case. |
| 120 .rule { |
| 121 display: block; |
| 122 }} |
| 123 |
| 124 #rule { |
| 125 rule: value; }""", """ |
| 126 - Always put a rule closing brace (}) on a new line. |
| 127 rule: value; }""") |
| 128 |
| 129 def testCssColonsHaveSpaceAfter(self): |
| 130 self.VerifyContentsProducesOutput(""" |
| 131 div:not(.class):not([attr]) /* We should not catch this. */ { |
| 132 display:block; |
| 133 }""", """ |
| 134 - Colons (:) should have a space after them. |
| 135 display:block;""") |
| 136 |
| 137 def testCssFavorSingleQuotes(self): |
| 138 self.VerifyContentsProducesOutput(""" |
| 139 html[dir="rtl"] body, |
| 140 html[dir=ltr] body /* TODO(dbeam): Require '' around rtl in future? */ { |
| 141 background: url("chrome://resources/BLAH"); |
| 142 font-family: "Open Sans"; |
| 143 }""", """ |
| 144 - Use single quotes (') instead of double quotes (") in strings. |
| 145 html[dir="rtl"] body, |
| 146 background: url("chrome://resources/BLAH"); |
| 147 font-family: "Open Sans";""") |
| 148 |
| 149 def testCssHexCouldBeShorter(self): |
| 150 self.VerifyContentsProducesOutput(""" |
| 151 #abc, |
| 152 #abc-, |
| 153 #abc-ghij, |
| 154 #abcdef-, |
| 155 #abcdef-ghij, |
| 156 #aaaaaa, |
| 157 #bbaacc { |
| 158 color: #999999; |
| 159 color: #666; |
| 160 }""", """ |
| 161 - Use abbreviated hex (#rgb) when in form #rrggbb. |
| 162 color: #999999; (replace with #99)""") |
| 163 |
| 164 def testCssUseMillisecondsForSmallTimes(self): |
| 165 self.VerifyContentsProducesOutput(""" |
| 166 .transition-0s /* This is gross but may happen. */ { |
| 167 transform: one 0.2s; |
| 168 transform: two .1s; |
| 169 transform: tree 1s; |
| 170 transform: four 300ms; |
| 171 }""", """ |
| 172 - Use milliseconds for time measurements under 1 second. |
| 173 transform: one 0.2s; (replace with 200ms) |
| 174 transform: two .1s; (replace with 100ms)""") |
| 175 |
| 176 def testCssOneRulePerLine(self): |
| 177 self.VerifyContentsProducesOutput(""" |
| 178 div { |
| 179 rule: value; /* rule: value; */ |
| 180 rule: value; rule: value; |
| 181 }""", """ |
| 182 - One rule per line (what not to do: color: red; margin: 0;). |
| 183 rule: value; rule: value;""") |
| 184 |
| 185 def testCssOneSelectorPerLine(self): |
| 186 self.VerifyContentsProducesOutput(""" |
| 187 a, |
| 188 div,a, |
| 189 div,/* Hello! */ span, |
| 190 #id.class([dir=rtl):not(.class):any(a, b, d) { |
| 191 rule: value; |
| 192 }""", """ |
| 193 - One selector per line (what not to do: a, b {}). |
| 194 div,a, |
| 195 div, span,""") |
| 196 |
| 197 |
| 198 def testCssRgbIfNotGray(self): |
| 199 self.VerifyContentsProducesOutput(""" |
| 200 #abc, |
| 201 #aaa, |
| 202 #aabbcc { |
| 203 background: -webkit-linear-gradient(left, from(#abc), to(#def)); |
| 204 color: #bad; |
| 205 color: #bada55; |
| 206 }""", """ |
| 207 - Use rgb() over #hex when not a shade of gray (like #333). |
| 208 background: -webkit-linear-gradient(left, from(#abc), to(#def)); """ |
| 209 """(replace with rgb(170, 187, 204), rgb(221, 238, 255)) |
| 210 color: #bad; (replace with rgb(187, 170, 221)) |
| 211 color: #bada55; (replace with rgb(186, 218, 85))""") |
| 212 |
| 213 def testCssZeroLengthTerms(self): |
| 214 self.VerifyContentsProducesOutput(""" |
| 215 @-webkit-keyframe anim { |
| 216 0% { /* Ignore key frames */ |
| 217 width: 0px; |
| 218 } |
| 219 10% { |
| 220 width: 10px; |
| 221 } |
| 222 100% { |
| 223 width: 100px; |
| 224 } |
| 225 } |
| 226 .animating { |
| 227 -webkit-animation: anim 0s; |
| 228 -webkit-animation-duration: anim 0ms; |
| 229 -webkit-transform: scale(0%), |
| 230 translateX(0deg), |
| 231 translateY(0rad), |
| 232 translateZ(0grad); |
| 233 background-position-x: 0em; |
| 234 background-position-y: 0ex; |
| 235 border-width: 0em; |
| 236 color: hsl(0, 0%, 85%); /* Shouldn't trigger error. */ |
| 237 } |
| 238 |
| 239 @page { |
| 240 border-width: 0mm; |
| 241 height: 0cm; |
| 242 width: 0in; |
| 243 }""",""" |
| 244 - Make all zero length terms (i.e. 0px) 0 unless inside of hsl() or part of""" |
| 245 """ @keyframe. |
| 246 width: 0px; |
| 247 -webkit-animation: anim 0s; |
| 248 -webkit-animation-duration: anim 0ms; |
| 249 -webkit-transform: scale(0%), |
| 250 translateX(0deg), |
| 251 translateY(0rad), |
| 252 translateZ(0grad); |
| 253 background-position-x: 0em; |
| 254 background-position-y: 0ex; |
| 255 border-width: 0em; |
| 256 border-width: 0mm; |
| 257 height: 0cm; |
| 258 width: 0in; |
| 259 """) |
| 260 |
| 261 if __name__ == '__main__': |
| 262 unittest.main() |
| OLD | NEW |