| 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 from copy import deepcopy |
| 6 import json | 7 import json |
| 7 import os | 8 import os |
| 8 import sys | 9 import sys |
| 9 import unittest | 10 import unittest |
| 10 | 11 |
| 11 from api_data_source import (APIDataSource, | 12 from api_data_source import (APIDataSource, |
| 12 _JSCModel, | 13 _JSCModel, |
| 13 _FormatValue, | 14 _FormatValue, |
| 14 _RemoveNoDocs) | 15 _RemoveNoDocs) |
| 15 from compiled_file_system import CompiledFileSystem | 16 from compiled_file_system import CompiledFileSystem |
| 16 from file_system import FileNotFoundError | 17 from file_system import FileNotFoundError |
| 17 from local_file_system import LocalFileSystem | 18 from local_file_system import LocalFileSystem |
| 18 from object_store_creator import ObjectStoreCreator | 19 from object_store_creator import ObjectStoreCreator |
| 19 from reference_resolver import ReferenceResolver | 20 from reference_resolver import ReferenceResolver |
| 21 from test_file_system import TestFileSystem |
| 20 import third_party.json_schema_compiler.model as model | 22 import third_party.json_schema_compiler.model as model |
| 21 | 23 |
| 24 _INTRO_TABLES_DATA = { |
| 25 'docs': { |
| 26 'templates': { |
| 27 'json': { |
| 28 'intro_tables.json': |
| 29 '{"tester": [{"Permissions": "<code>tester</code>"}]}' |
| 30 } |
| 31 } |
| 32 } |
| 33 } |
| 34 |
| 22 def _MakeLink(href, text): | 35 def _MakeLink(href, text): |
| 23 return '<a href="%s">%s</a>' % (href, text) | 36 return '<a href="%s">%s</a>' % (href, text) |
| 24 | 37 |
| 25 def _GetType(dict_, name): | 38 def _GetType(dict_, name): |
| 26 for type_ in dict_['types']: | 39 for type_ in dict_['types']: |
| 27 if type_['name'] == name: | 40 if type_['name'] == name: |
| 28 return type_ | 41 return type_ |
| 29 | 42 |
| 43 class FakeAvailabilityDataSourceFactory(object): |
| 44 def Create(self): |
| 45 return {} |
| 46 |
| 47 def _FakeGetAvailability(self): |
| 48 return 'Not Currently Available' |
| 49 |
| 30 class FakeSamplesDataSource(object): | 50 class FakeSamplesDataSource(object): |
| 31 def Create(self, request): | 51 def Create(self, request): |
| 32 return {} | 52 return {} |
| 33 | 53 |
| 34 class FakeAPIAndListDataSource(object): | 54 class FakeAPIAndListDataSource(object): |
| 35 def __init__(self, json_data): | 55 def __init__(self, json_data): |
| 36 self._json = json_data | 56 self._json = json_data |
| 37 | 57 |
| 38 def Create(self, *args, **kwargs): | 58 def Create(self, *args, **kwargs): |
| 39 return self | 59 return self |
| 40 | 60 |
| 41 def get(self, key): | 61 def get(self, key): |
| 42 if key not in self._json: | 62 if key not in self._json: |
| 43 raise FileNotFoundError(key) | 63 raise FileNotFoundError(key) |
| 44 return self._json[key] | 64 return self._json[key] |
| 45 | 65 |
| 46 def GetAllNames(self): | 66 def GetAllNames(self): |
| 47 return self._json.keys() | 67 return self._json.keys() |
| 48 | 68 |
| 49 class APIDataSourceTest(unittest.TestCase): | 69 class APIDataSourceTest(unittest.TestCase): |
| 50 def setUp(self): | 70 def setUp(self): |
| 51 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 71 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 72 _JSCModel._GetAvailability = _FakeGetAvailability |
| 52 | 73 |
| 53 def _ReadLocalFile(self, filename): | 74 def _ReadLocalFile(self, filename): |
| 54 with open(os.path.join(self._base_path, filename), 'r') as f: | 75 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 55 return f.read() | 76 return f.read() |
| 56 | 77 |
| 57 def _CreateRefResolver(self, filename): | 78 def _CreateRefResolver(self, filename): |
| 58 data_source = FakeAPIAndListDataSource( | 79 data_source = FakeAPIAndListDataSource( |
| 59 self._LoadJSON(filename)) | 80 self._LoadJSON(filename)) |
| 60 return ReferenceResolver.Factory(data_source, | 81 return ReferenceResolver.Factory(data_source, |
| 61 data_source, | 82 data_source, |
| 62 ObjectStoreCreator.TestFactory()).Create() | 83 ObjectStoreCreator.TestFactory()).Create() |
| 63 | 84 |
| 64 def _LoadJSON(self, filename): | 85 def _LoadJSON(self, filename): |
| 65 return json.loads(self._ReadLocalFile(filename)) | 86 return json.loads(self._ReadLocalFile(filename)) |
| 66 | 87 |
| 67 def testCreateId(self): | 88 def testCreateId(self): |
| 68 data_source = FakeAPIAndListDataSource( | 89 data_source = FakeAPIAndListDataSource( |
| 69 self._LoadJSON('test_file_data_source.json')) | 90 self._LoadJSON('test_file_data_source.json')) |
| 91 fake_avail_factory = FakeAvailabilityDataSourceFactory() |
| 70 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 92 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 71 self._CreateRefResolver('test_file_data_source.json'), | 93 self._CreateRefResolver('test_file_data_source.json'), |
| 72 False).ToDict() | 94 False, |
| 95 fake_avail_factory, |
| 96 TestFileSystem(deepcopy(_INTRO_TABLES_DATA))).ToDict() |
| 73 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 97 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
| 74 self.assertEquals('property-TypeA-b', | 98 self.assertEquals('property-TypeA-b', |
| 75 dict_['types'][0]['properties'][0]['id']) | 99 dict_['types'][0]['properties'][0]['id']) |
| 76 self.assertEquals('method-get', dict_['functions'][0]['id']) | 100 self.assertEquals('method-get', dict_['functions'][0]['id']) |
| 77 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 101 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
| 78 | 102 |
| 79 # TODO(kalman): re-enable this when we have a rebase option. | 103 # TODO(kalman): re-enable this when we have a rebase option. |
| 80 def DISABLED_testToDict(self): | 104 def DISABLED_testToDict(self): |
| 81 filename = 'test_file.json' | 105 filename = 'test_file.json' |
| 82 expected_json = self._LoadJSON('expected_' + filename) | 106 expected_json = self._LoadJSON('expected_' + filename) |
| 83 data_source = FakeAPIAndListDataSource( | 107 data_source = FakeAPIAndListDataSource( |
| 84 self._LoadJSON('test_file_data_source.json')) | 108 self._LoadJSON('test_file_data_source.json')) |
| 85 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 109 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
| 86 self._CreateRefResolver('test_file_data_source.json'), | 110 self._CreateRefResolver('test_file_data_source.json'), |
| 87 False).ToDict() | 111 False, |
| 112 fake_avail_factory, |
| 113 TestFileSystem(deepcopy(_INTRO_TABLES_DATA))).ToDict() |
| 88 self.assertEquals(expected_json, dict_) | 114 self.assertEquals(expected_json, dict_) |
| 89 | 115 |
| 90 def testFormatValue(self): | 116 def testFormatValue(self): |
| 91 self.assertEquals('1,234,567', _FormatValue(1234567)) | 117 self.assertEquals('1,234,567', _FormatValue(1234567)) |
| 92 self.assertEquals('67', _FormatValue(67)) | 118 self.assertEquals('67', _FormatValue(67)) |
| 93 self.assertEquals('234,567', _FormatValue(234567)) | 119 self.assertEquals('234,567', _FormatValue(234567)) |
| 94 | 120 |
| 95 def testFormatDescription(self): | 121 def testFormatDescription(self): |
| 122 fake_avail_factory = FakeAvailabilityDataSourceFactory() |
| 96 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 123 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
| 97 self._CreateRefResolver('ref_test_data_source.json'), | 124 self._CreateRefResolver('ref_test_data_source.json'), |
| 98 False).ToDict() | 125 False, |
| 126 fake_avail_factory, |
| 127 TestFileSystem(deepcopy(_INTRO_TABLES_DATA))).ToDict() |
| 99 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 128 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
| 100 _GetType(dict_, 'type1')['description']) | 129 _GetType(dict_, 'type1')['description']) |
| 101 self.assertEquals( | 130 self.assertEquals( |
| 102 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 131 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
| 103 _MakeLink('ref_test.html#type-type2', 'type2')), | 132 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 104 _GetType(dict_, 'type2')['description']) | 133 _GetType(dict_, 'type2')['description']) |
| 105 self.assertEquals( | 134 self.assertEquals( |
| 106 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 135 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 107 _MakeLink('ref_test.html#type-type2', 'type2')), | 136 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 108 _GetType(dict_, 'type3')['description']) | 137 _GetType(dict_, 'type3')['description']) |
| 109 | 138 |
| 110 def testRemoveNoDocs(self): | 139 def testRemoveNoDocs(self): |
| 111 d = self._LoadJSON('nodoc_test.json') | 140 d = self._LoadJSON('nodoc_test.json') |
| 112 _RemoveNoDocs(d) | 141 _RemoveNoDocs(d) |
| 113 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 142 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) |
| 114 | 143 |
| 115 if __name__ == '__main__': | 144 if __name__ == '__main__': |
| 116 unittest.main() | 145 unittest.main() |
| OLD | NEW |