summaryrefslogtreecommitdiffstats
path: root/chrome/browser/web_dev_style/closure_lint_test.py
blob: b9a7382ca8103308dd1caabf3d028463353b7a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# Copyright 2015 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.

import js_checker
from os import path as os_path
import re
from sys import path as sys_path
import unittest

_HERE = os_path.dirname(os_path.abspath(__file__))
sys_path.append(os_path.join(_HERE, '..', '..', '..', 'tools'))

import find_depot_tools  # pylint: disable=W0611
from testing_support.super_mox import SuperMoxTestBase


class ClosureLintTest(SuperMoxTestBase):
  def setUp(self):
    SuperMoxTestBase.setUp(self)

    input_api = self.mox.CreateMockAnything()
    input_api.os_path = os_path
    input_api.re = re

    input_api.change = self.mox.CreateMockAnything()
    self.mox.StubOutWithMock(input_api.change, 'RepositoryRoot')
    src_root = os_path.join(os_path.dirname(__file__), '..', '..', '..')
    input_api.change.RepositoryRoot().MultipleTimes().AndReturn(src_root)

    output_api = self.mox.CreateMockAnything()

    self.mox.ReplayAll()

    self.checker = js_checker.JSChecker(input_api, output_api)

  def ShouldPassClosureLint(self, source):
    errors = self.checker.ClosureLint('', source=source)

    for error in errors:
      print 'Error: ' + error.message

    self.assertListEqual([], errors)

  def testBindFalsePositives(self):
    sources = [
      [
        'var addOne = function(prop) {\n',
        '  this[prop] += 1;\n',
        '}.bind(counter, timer);\n',
        '\n',
        'setInterval(addOne, 1000);\n',
        '\n',
      ],
      [
        '/** Da clickz. */\n',
        'button.onclick = function() { this.add_(this.total_); }.bind(this);\n',
      ],
    ]
    for source in sources:
      self.ShouldPassClosureLint(source)

  def testPromiseFalsePositives(self):
    sources = [
      [
        'Promise.reject(1).catch(function(error) {\n',
        '  alert(error);\n',
        '});\n',
      ],
      [
        'var loaded = new Promise();\n',
        'loaded.then(runAwesomeApp);\n',
        'loaded.catch(showSadFace);\n',
        '\n',
        '/** Da loadz. */\n',
        'document.onload = function() { loaded.resolve(); };\n',
        '\n',
        '/** Da errorz. */\n',
        'document.onerror = function() { loaded.reject(); };\n',
        '\n',
        "if (document.readystate == 'complete') loaded.resolve();\n",
      ],
    ]
    for source in sources:
      self.ShouldPassClosureLint(source)


if __name__ == '__main__':
  unittest.main()