Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 json | 6 import json |
| 7 import os | 7 import os |
| 8 import sys | 8 import sys |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 from api_data_source import (APIDataSource, | 11 from api_data_source import (APIDataSource, |
| 12 _JSCModel, | 12 _JSCModel, |
| 13 _FormatValue, | 13 _FormatValue, |
| 14 _RemoveNoDocs) | 14 _RemoveNoDocs) |
| 15 from compiled_file_system import CompiledFileSystem | 15 from compiled_file_system import CompiledFileSystem |
| 16 from file_system import FileNotFoundError | 16 from file_system import FileNotFoundError |
| 17 from local_file_system import LocalFileSystem | 17 from local_file_system import LocalFileSystem |
| 18 from object_store_creator import ObjectStoreCreator | 18 from object_store_creator import ObjectStoreCreator |
| 19 from reference_resolver import ReferenceResolver | 19 from reference_resolver import ReferenceResolver |
| 20 import third_party.json_schema_compiler.idl_schema as idl_schema | |
| 21 import third_party.json_schema_compiler.idl_parser as idl_parser | |
| 20 import third_party.json_schema_compiler.model as model | 22 import third_party.json_schema_compiler.model as model |
| 21 | 23 |
| 22 def _MakeLink(href, text): | 24 def _MakeLink(href, text): |
| 23 return '<a href="%s">%s</a>' % (href, text) | 25 return '<a href="%s">%s</a>' % (href, text) |
| 24 | 26 |
| 25 def _GetType(dict_, name): | 27 def _GetType(dict_, name): |
| 26 for type_ in dict_['types']: | 28 for type_ in dict_['types']: |
| 27 if type_['name'] == name: | 29 if type_['name'] == name: |
| 28 return type_ | 30 return type_ |
| 29 | 31 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 41 def get(self, key): | 43 def get(self, key): |
| 42 if key not in self._json: | 44 if key not in self._json: |
| 43 raise FileNotFoundError(key) | 45 raise FileNotFoundError(key) |
| 44 return self._json[key] | 46 return self._json[key] |
| 45 | 47 |
| 46 def GetAllNames(self): | 48 def GetAllNames(self): |
| 47 return self._json.keys() | 49 return self._json.keys() |
| 48 | 50 |
| 49 class APIDataSourceTest(unittest.TestCase): | 51 class APIDataSourceTest(unittest.TestCase): |
| 50 def setUp(self): | 52 def setUp(self): |
| 51 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 53 self._base_path = os.path.join(sys.path[0], 'test_data') |
| 52 | 54 |
| 53 def _ReadLocalFile(self, filename): | 55 def _ReadLocalFile(self, filename, test_dir='test_json'): |
| 54 with open(os.path.join(self._base_path, filename), 'r') as f: | 56 with open(os.path.join(self._base_path, test_dir, filename), 'r') as f: |
| 55 return f.read() | 57 return f.read() |
| 56 | 58 |
| 57 def _CreateRefResolver(self, filename): | 59 def _CreateRefResolver(self, filename): |
| 58 data_source = FakeAPIAndListDataSource( | 60 data_source = FakeAPIAndListDataSource( |
| 59 self._LoadJSON(filename)) | 61 self._LoadJSON(filename)) |
| 60 return ReferenceResolver.Factory(data_source, | 62 return ReferenceResolver.Factory(data_source, |
| 61 data_source, | 63 data_source, |
| 62 ObjectStoreCreator.Factory()).Create() | 64 ObjectStoreCreator.Factory()).Create() |
| 63 | 65 |
| 64 def _LoadJSON(self, filename): | 66 def _LoadJSON(self, filename): |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 self.assertEquals( | 107 self.assertEquals( |
| 106 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 108 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 107 _MakeLink('ref_test.html#type-type2', 'type2')), | 109 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 108 _GetType(dict_, 'type3')['description']) | 110 _GetType(dict_, 'type3')['description']) |
| 109 | 111 |
| 110 def testRemoveNoDocs(self): | 112 def testRemoveNoDocs(self): |
| 111 d = self._LoadJSON('nodoc_test.json') | 113 d = self._LoadJSON('nodoc_test.json') |
| 112 _RemoveNoDocs(d) | 114 _RemoveNoDocs(d) |
| 113 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 115 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) |
| 114 | 116 |
| 117 def testInlineDocs(self): | |
| 118 raw_idl = self._ReadLocalFile('inline.idl', test_dir='test_idl') | |
| 119 idl = idl_parser.IDLParser().ParseData(raw_idl) | |
| 120 | |
| 121 jsc = _JSCModel( | |
| 122 idl_schema.IDLSchema(idl).process()[0], | |
| 123 None, True).ToDict() | |
| 124 | |
| 125 try: | |
| 126 # Make sure that inlining worked by attempting to access the inner objects | |
| 127 # inline. | |
| 128 self.assertNotEqual( | |
| 129 jsc['functions'][0]['parameters'][0].get('enum_values'), | |
| 130 None) | |
| 131 self.assertNotEqual(jsc['functions'][2]['parameters'][0].get( | |
| 132 'properties')[3].get('enum_values'), | |
| 133 None) | |
| 134 except KeyError: | |
| 135 self.assertEqual(True, False); | |
|
not at google - send to devlin
2013/04/24 19:36:11
Throwing an error will fail the test anyway...
jshumway
2013/04/25 00:06:58
not really sure what I was thinking there, fixed.
| |
| 136 | |
|
not at google - send to devlin
2013/04/24 19:36:11
I think it would be more valuable to test a JSON s
jshumway
2013/04/25 00:06:58
I have made it so
| |
| 115 if __name__ == '__main__': | 137 if __name__ == '__main__': |
| 116 unittest.main() | 138 unittest.main() |
| OLD | NEW |