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