OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import glob | |
6 import json | 7 import json |
7 import os | 8 import os |
8 import subprocess | 9 import subprocess |
9 import sys | 10 import sys |
10 import unittest | 11 import unittest |
11 | 12 |
12 import PRESUBMIT | 13 import PRESUBMIT |
13 | 14 |
14 | 15 |
15 class MockInputApi(object): | 16 class MockInputApi(object): |
16 def __init__(self): | 17 def __init__(self): |
17 self.json = json | 18 self.json = json |
18 self.os_path = os.path | 19 self.os_path = os.path |
19 self.subprocess = subprocess | 20 self.subprocess = subprocess |
20 self.python_executable = sys.executable | 21 self.python_executable = sys.executable |
21 | 22 |
22 def PresubmitLocalPath(self): | 23 def PresubmitLocalPath(self): |
23 return os.path.dirname(__file__) | 24 return os.path.dirname(__file__) |
24 | 25 |
26 def ReadFile(self, filename, mode='rU'): | |
27 with open(filename, mode=mode) as f: | |
28 return f.read() | |
29 | |
25 | 30 |
26 class JSONParsingTest(unittest.TestCase): | 31 class JSONParsingTest(unittest.TestCase): |
27 def testSuccess(self): | 32 def testSuccess(self): |
28 input_api = MockInputApi() | 33 input_api = MockInputApi() |
29 input_json = ''' | 34 filename = 'test_presubmit/valid_json.json' |
30 // This is a comment. | |
31 { | |
32 "key1": ["value1", "value2"], | |
33 "key2": 3 // This is an inline comment. | |
34 } | |
35 ''' | |
36 self.assertEqual(None, | 35 self.assertEqual(None, |
37 PRESUBMIT._GetJSONParseError(input_api, input_json)) | 36 PRESUBMIT._GetJSONParseError(input_api, filename)) |
38 | 37 |
39 def testFailure(self): | 38 def testFailure(self): |
40 input_api = MockInputApi() | 39 input_api = MockInputApi() |
41 input_json = '{ x }' | 40 expected_errors = [ |
42 self.assertEqual('Expecting property name: line 1 column 2 (char 2)', | 41 'Expecting property name: line 8 column 3 (char 9)', |
43 str(PRESUBMIT._GetJSONParseError(input_api, input_json))) | 42 'Invalid control character at: line 8 column 19 (char 25)', |
43 'Expecting property name: line 8 column 23 (char 29)', | |
44 'Expecting , delimiter: line 8 column 12 (char 18)', | |
Haojian Wu
2013/09/18 02:00:38
Here I find a strange problem: It looks like the e
| |
45 ] | |
46 actual_errors = [ | |
47 str(PRESUBMIT._GetJSONParseError(input_api, filename)) | |
48 for filename in sorted(glob.glob('test_presubmit/invalid_*.json')) | |
49 ] | |
50 self.assertEqual(expected_errors, actual_errors) | |
44 | 51 |
45 input_json = '{ "hello": "world }' | |
46 self.assertEqual( | |
47 'Unterminated string starting at: line 1 column 11 (char 11)', | |
48 str(PRESUBMIT._GetJSONParseError(input_api, input_json))) | |
49 | 52 |
50 input_json = '{ "a": "b", "c": "d", }' | 53 class IDLParsingTest(unittest.TestCase): |
51 self.assertEqual( | 54 def testSuccess(self): |
52 'Expecting property name: line 1 column 22 (char 22)', | 55 input_api = MockInputApi() |
53 str(PRESUBMIT._GetJSONParseError(input_api, input_json))) | 56 filename = 'test_presubmit/valid_idl_basics.idl' |
57 self.assertEqual(None, | |
58 PRESUBMIT._GetIDLParseError(input_api, filename)) | |
54 | 59 |
55 input_json = '{ "a": "b" "c": "d" }' | 60 def testFailure(self): |
56 self.assertEqual( | 61 input_api = MockInputApi() |
57 'Expecting , delimiter: line 1 column 11 (char 11)', | 62 expected_errors = [ |
58 str(PRESUBMIT._GetJSONParseError(input_api, input_json))) | 63 'Unexpected "{" after keyword "dictionary".', |
64 'Unexpected symbol DOMString after symbol a.', | |
65 'Unexpected symbol name2 after symbol name1.', | |
66 'Trailing comma in block.', | |
67 'Unexpected ";" after "(".', | |
68 'Unexpected ")" after symbol long.', | |
69 'Unexpected symbol Events after symbol interace.', | |
70 'Did not process Interface Interface(NotEvent)', | |
71 'Interface missing name.', | |
72 ] | |
73 actual_errors = [ | |
74 PRESUBMIT._GetIDLParseError(input_api, filename) | |
75 for filename in sorted(glob.glob('test_presubmit/invalid_*.idl')) | |
76 ] | |
77 for (expected_error, actual_error) in zip(expected_errors, actual_errors): | |
78 self.assertTrue(expected_error in actual_error) | |
59 | 79 |
60 | 80 |
61 if __name__ == "__main__": | 81 if __name__ == "__main__": |
62 unittest.main() | 82 unittest.main() |
OLD | NEW |