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 s = f.read() | |
29 return s | |
not at google - send to devlin
2013/09/10 22:51:21
just return f.read()?
Haojian Wu
2013/09/11 01:10:22
Done.
| |
30 | |
25 | 31 |
26 class JSONParsingTest(unittest.TestCase): | 32 class JSONParsingTest(unittest.TestCase): |
27 def testSuccess(self): | 33 def testSuccess(self): |
28 input_api = MockInputApi() | 34 input_api = MockInputApi() |
29 input_json = ''' | 35 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, | 36 self.assertEqual(None, |
37 PRESUBMIT._GetJSONParseError(input_api, input_json)) | 37 PRESUBMIT._GetJSONParseError(input_api, filename)) |
38 | 38 |
39 def testFailure(self): | 39 def testFailure(self): |
40 input_api = MockInputApi() | 40 input_api = MockInputApi() |
41 input_json = '{ x }' | 41 expected_errors = [ |
42 self.assertEqual('Expecting property name: line 1 column 2 (char 2)', | 42 'Expecting property name: line 8 column 3 (char 16)', |
43 str(PRESUBMIT._GetJSONParseError(input_api, input_json))) | 43 'Invalid control character at: line 8 column 19 (char 32)', |
44 'Expecting property name: line 8 column 23 (char 36)', | |
45 'Expecting , delimiter: line 8 column 12 (char 25)', | |
46 ] | |
47 for (offset, filename) in enumerate( | |
not at google - send to devlin
2013/09/10 22:51:21
you don't need the parens around this. however, a
Haojian Wu
2013/09/11 01:10:22
Done.
| |
48 sorted(glob.glob('test_presubmit/invalid_*.json'))): | |
49 error = PRESUBMIT._GetJSONParseError(input_api, filename) | |
50 self.assertEqual(expected_errors[offset], str(error)) | |
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 | |
74 for (offset, filename) in enumerate( | |
75 sorted(glob.glob('test_presubmit/invalid_*.idl'))): | |
76 error = PRESUBMIT._GetIDLParseError(input_api, filename) | |
77 self.assertTrue(expected_errors[offset] in error) | |
not at google - send to devlin
2013/09/10 22:51:21
ditto
Haojian Wu
2013/09/11 01:10:22
Done.
| |
59 | 78 |
60 | 79 |
61 if __name__ == "__main__": | 80 if __name__ == "__main__": |
62 unittest.main() | 81 unittest.main() |
OLD | NEW |