summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/api/PRESUBMIT_test.py
blob: 88d28e6c006f8f8dd6b5600f469a8657384b85f9 (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
#!/usr/bin/env python
# Copyright 2013 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 glob
import json
import os
import subprocess
import sys
import unittest

import PRESUBMIT


class MockInputApi(object):
  def __init__(self):
    self.json = json
    self.os_path = os.path
    self.subprocess = subprocess
    self.python_executable = sys.executable

  def PresubmitLocalPath(self):
    return os.path.dirname(__file__)

  def ReadFile(self, filename, mode='rU'):
    with open(filename, mode=mode) as f:
      return f.read()


class JSONParsingTest(unittest.TestCase):
  def testSuccess(self):
    input_api = MockInputApi()
    filename = 'test_presubmit/valid_json.json'
    self.assertEqual(None,
                     PRESUBMIT._GetJSONParseError(input_api, filename))

  def testFailure(self):
    input_api = MockInputApi()
    expected_errors = [
      'Expecting property name: line 8 column 3 (char 9)',
      'Invalid control character at: line 8 column 19 (char 25)',
      'Expecting property name: line 8 column 23 (char 29)',
      'Expecting , delimiter: line 8 column 12 (char 18)',
    ]
    actual_errors = [
      str(PRESUBMIT._GetJSONParseError(input_api, filename))
      for filename in sorted(glob.glob('test_presubmit/invalid_*.json'))
    ]
    self.assertEqual(expected_errors, actual_errors)


class IDLParsingTest(unittest.TestCase):
  def testSuccess(self):
    input_api = MockInputApi()
    filename = 'test_presubmit/valid_idl_basics.idl'
    self.assertEqual(None,
                     PRESUBMIT._GetIDLParseError(input_api, filename))

  def testFailure(self):
    input_api = MockInputApi()
    expected_errors = [
      'Unexpected "{" after keyword "dictionary".',
      'Unexpected symbol DOMString after symbol a.',
      'Unexpected symbol name2 after symbol name1.',
      'Trailing comma in block.',
      'Unexpected ";" after "(".',
      'Unexpected ")" after symbol long.',
      'Unexpected symbol Events after symbol interace.',
      'Did not process Interface Interface(NotEvent)',
      'Interface missing name.',
    ]
    actual_errors = [
      PRESUBMIT._GetIDLParseError(input_api, filename)
      for filename in sorted(glob.glob('test_presubmit/invalid_*.idl'))
    ]
    for (expected_error, actual_error) in zip(expected_errors, actual_errors):
      self.assertTrue(expected_error in actual_error)


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