#!/usr/bin/env python # Copyright (c) 2012 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Unit tests for Web Development Style Guide checker.""" import os import re import sys import unittest test_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.extend([ os.path.normpath(os.path.join(test_dir, '..', '..', 'tools')), os.path.join(test_dir), ]) import find_depot_tools # pylint: disable=W0611 from testing_support.super_mox import SuperMoxTestBase from web_dev_style import css_checker, html_checker, js_checker, \ resource_checker # pylint: disable=F0401 def GetHighlight(line, error): """Returns the substring of |line| that is highlighted in |error|.""" error_lines = error.split('\n') highlight = error_lines[error_lines.index(line) + 1] return ''.join(ch1 for (ch1, ch2) in zip(line, highlight) if ch2 == '^') class HtmlStyleTest(SuperMoxTestBase): def setUp(self): SuperMoxTestBase.setUp(self) input_api = self.mox.CreateMockAnything() input_api.re = re output_api = self.mox.CreateMockAnything() self.checker = html_checker.HtmlChecker(input_api, output_api) def ShouldFailCheck(self, line, checker): """Checks that the |checker| flags |line| as a style error.""" error = checker(1, line) self.assertNotEqual('', error, 'Should be flagged as style error: ' + line) highlight = GetHighlight(line, error).strip() def ShouldPassCheck(self, line, checker): """Checks that the |checker| doesn't flag |line| as a style error.""" error = checker(1, line) self.assertEqual('', error, 'Should not be flagged as style error: ' + line) def testClassesUseDashFormCheckFails(self): lines = [ ' ', ' ', '', '
', ] for line in lines: self.ShouldFailCheck(line, self.checker.ClassesUseDashFormCheck) def testClassesUseDashFormCheckPasses(self): lines = [ ' class="abc" ', 'class="foo-bar"', '
", ' ', "
", "
", "
", ] for line in lines: self.ShouldFailCheck(line, self.checker.DoNotCloseSingleTagsCheck) def testDoNotCloseSingleTagsCheckPasses(self): lines = [ "", "", "
", '', ] for line in lines: self.ShouldPassCheck(line, self.checker.DoNotCloseSingleTagsCheck) def testDoNotUseBrElementCheckFails(self): lines = [ "
", "
", "", '
', ] for line in lines: self.ShouldFailCheck( line, self.checker.DoNotUseBrElementCheck) def testDoNotUseBrElementCheckPasses(self): lines = [ "br", "br>", "give me a break" ] for line in lines: self.ShouldPassCheck( line, self.checker.DoNotUseBrElementCheck) def testDoNotUseInputTypeButtonCheckFails(self): lines = [ '', ' ', ' ', ] for line in lines: self.ShouldFailCheck(line, self.checker.DoNotUseInputTypeButtonCheck) def testDoNotUseInputTypeButtonCheckPasses(self): lines = [ "", '', '', '', "