| 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 docs_server_utils import GetLinkToRefType | |
| 17 from file_system import FileNotFoundError | 16 from file_system import FileNotFoundError |
| 18 from in_memory_object_store import InMemoryObjectStore | 17 from in_memory_object_store import InMemoryObjectStore |
| 19 from local_file_system import LocalFileSystem | 18 from local_file_system import LocalFileSystem |
| 19 from reference_resolver import ReferenceResolver |
| 20 import third_party.json_schema_compiler.json_comment_eater as comment_eater | 20 import third_party.json_schema_compiler.json_comment_eater as comment_eater |
| 21 import third_party.json_schema_compiler.model as model | 21 import third_party.json_schema_compiler.model as model |
| 22 | 22 |
| 23 def _MakeLink(href, text): | 23 def _MakeLink(href, text): |
| 24 return '<a href="%s">%s</a>' % (href, text) | 24 return '<a href="%s">%s</a>' % (href, text) |
| 25 | 25 |
| 26 def _GetType(dict_, name): | 26 def _GetType(dict_, name): |
| 27 for type_ in dict_['types']: | 27 for type_ in dict_['types']: |
| 28 if type_['name'] == name: | 28 if type_['name'] == name: |
| 29 return type_ | 29 return type_ |
| 30 | 30 |
| 31 class FakeSamplesDataSource: | 31 class FakeSamplesDataSource(object): |
| 32 def Create(self, request): | 32 def Create(self, request): |
| 33 return {} | 33 return {} |
| 34 | 34 |
| 35 class FakeAPIAndListDataSource(object): |
| 36 def __init__(self, json_data): |
| 37 self._json = json_data |
| 38 |
| 39 def get(self, key): |
| 40 if key not in self._json: |
| 41 raise FileNotFoundError(key) |
| 42 return self._json[key] |
| 43 |
| 44 def GetAllNames(self): |
| 45 return self._json.keys() |
| 46 |
| 35 class APIDataSourceTest(unittest.TestCase): | 47 class APIDataSourceTest(unittest.TestCase): |
| 36 def setUp(self): | 48 def setUp(self): |
| 37 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 49 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 38 | 50 |
| 39 def _ReadLocalFile(self, filename): | 51 def _ReadLocalFile(self, filename): |
| 40 with open(os.path.join(self._base_path, filename), 'r') as f: | 52 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 41 return f.read() | 53 return f.read() |
| 42 | 54 |
| 55 def _CreateRefResolver(self, filename): |
| 56 data_source = FakeAPIAndListDataSource( |
| 57 self._LoadJSON(filename)) |
| 58 return ReferenceResolver.Factory(data_source, data_source).Create() |
| 59 |
| 43 def DISABLED_testSimple(self): | 60 def DISABLED_testSimple(self): |
| 44 cache_factory = CompiledFileSystem.Factory( | 61 cache_factory = CompiledFileSystem.Factory( |
| 45 LocalFileSystem(self._base_path), | 62 LocalFileSystem(self._base_path), |
| 46 InMemoryObjectStore('fake_branch')) | 63 InMemoryObjectStore('fake_branch')) |
| 47 data_source_factory = APIDataSource.Factory(cache_factory, | 64 data_source_factory = APIDataSource.Factory(cache_factory, |
| 48 '.', | 65 '.') |
| 49 FakeSamplesDataSource()) | 66 data_source_factory.SetSamplesDataSourceFactory(FakeSamplesDataSource()) |
| 50 data_source = data_source_factory.Create({}) | 67 data_source = data_source_factory.Create({}, disable_refs=True) |
| 51 | 68 |
| 52 # Take the dict out of the list. | 69 # Take the dict out of the list. |
| 53 expected = json.loads(self._ReadLocalFile('expected_test_file.json')) | 70 expected = json.loads(self._ReadLocalFile('expected_test_file.json')) |
| 54 expected['permissions'] = None | 71 expected['permissions'] = None |
| 55 test1 = data_source['test_file'] | 72 test1 = data_source.get('test_file') |
| 56 test1.pop('samples') | 73 test1.pop('samples') |
| 57 self.assertEqual(expected, test1) | 74 self.assertEqual(expected, test1) |
| 58 test2 = data_source['testFile'] | 75 test2 = data_source.get('testFile') |
| 59 test2.pop('samples') | 76 test2.pop('samples') |
| 60 self.assertEqual(expected, test2) | 77 self.assertEqual(expected, test2) |
| 61 test3 = data_source['testFile.html'] | 78 test3 = data_source.get('testFile.html') |
| 62 test3.pop('samples') | 79 test3.pop('samples') |
| 63 self.assertEqual(expected, test3) | 80 self.assertEqual(expected, test3) |
| 64 self.assertRaises(FileNotFoundError, data_source.get, 'junk') | 81 self.assertRaises(FileNotFoundError, data_source.get, 'junk') |
| 65 | 82 |
| 66 def _LoadJSON(self, filename): | 83 def _LoadJSON(self, filename): |
| 67 return json.loads(comment_eater.Nom(self._ReadLocalFile(filename)))[0] | 84 return json.loads(comment_eater.Nom(self._ReadLocalFile(filename))) |
| 68 | |
| 69 def _ToDictTest(self, filename): | |
| 70 expected_json = json.loads(self._ReadLocalFile('expected_' + filename)) | |
| 71 gen = _JscModel(self._LoadJSON(filename)) | |
| 72 self.assertEquals(expected_json, gen.ToDict()) | |
| 73 | 85 |
| 74 def testCreateId(self): | 86 def testCreateId(self): |
| 75 dict_ = _JscModel(self._LoadJSON('test_file.json')).ToDict() | 87 data_source = FakeAPIAndListDataSource( |
| 88 self._LoadJSON('test_file_data_source.json')) |
| 89 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 90 self._CreateRefResolver('test_file_data_source.json'), |
| 91 False).ToDict() |
| 76 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 92 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
| 77 self.assertEquals('property-TypeA-b', | 93 self.assertEquals('property-TypeA-b', |
| 78 dict_['types'][0]['properties'][0]['id']) | 94 dict_['types'][0]['properties'][0]['id']) |
| 79 self.assertEquals('method-get', dict_['functions'][0]['id']) | 95 self.assertEquals('method-get', dict_['functions'][0]['id']) |
| 80 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 96 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
| 81 | 97 |
| 82 def testToDict(self): | 98 def testToDict(self): |
| 83 self._ToDictTest('test_file.json') | 99 filename = 'test_file.json' |
| 84 | 100 expected_json = self._LoadJSON('expected_' + filename) |
| 85 def testGetLinkToRefType(self): | 101 data_source = FakeAPIAndListDataSource( |
| 86 link = GetLinkToRefType('truthTeller', 'liar.Tab') | 102 self._LoadJSON('test_file_data_source.json')) |
| 87 self.assertEquals('liar.html#type-Tab', link['href']) | 103 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
| 88 self.assertEquals('liar.Tab', link['text']) | 104 self._CreateRefResolver('test_file_data_source.json'), |
| 89 link = GetLinkToRefType('truthTeller', 'Tab') | 105 False).ToDict() |
| 90 self.assertEquals('#type-Tab', link['href']) | 106 self.assertEquals(expected_json, dict_) |
| 91 self.assertEquals('Tab', link['text']) | |
| 92 link = GetLinkToRefType('nay', 'lies.chrome.bookmarks.Tab') | |
| 93 self.assertEquals('lies.chrome.bookmarks.html#type-Tab', link['href']) | |
| 94 self.assertEquals('lies.chrome.bookmarks.Tab', link['text']) | |
| 95 | 107 |
| 96 def testFormatValue(self): | 108 def testFormatValue(self): |
| 97 self.assertEquals('1,234,567', _FormatValue(1234567)) | 109 self.assertEquals('1,234,567', _FormatValue(1234567)) |
| 98 self.assertEquals('67', _FormatValue(67)) | 110 self.assertEquals('67', _FormatValue(67)) |
| 99 self.assertEquals('234,567', _FormatValue(234567)) | 111 self.assertEquals('234,567', _FormatValue(234567)) |
| 100 | 112 |
| 101 def testFormatDescription(self): | 113 def testFormatDescription(self): |
| 102 dict_ = _JscModel(self._LoadJSON('ref_test.json')).ToDict() | 114 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
| 115 self._CreateRefResolver('ref_test_data_source.json'), |
| 116 False).ToDict() |
| 103 self.assertEquals(_MakeLink('#type-type2', 'type2'), | 117 self.assertEquals(_MakeLink('#type-type2', 'type2'), |
| 104 _GetType(dict_, 'type1')['description']) | 118 _GetType(dict_, 'type1')['description']) |
| 105 self.assertEquals( | 119 self.assertEquals( |
| 106 'A %s, or %s' % (_MakeLink('#type-type3', 'type3'), | 120 'A %s, or %s' % (_MakeLink('#type-type3', 'type3'), |
| 107 _MakeLink('#type-type2', 'type2')), | 121 _MakeLink('#type-type2', 'type2')), |
| 108 _GetType(dict_, 'type2')['description']) | 122 _GetType(dict_, 'type2')['description']) |
| 109 self.assertEquals( | 123 self.assertEquals( |
| 110 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 124 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 111 _MakeLink('#type-type2', 'type2')), | 125 _MakeLink('#type-type2', 'type2')), |
| 112 _GetType(dict_, 'type3')['description']) | 126 _GetType(dict_, 'type3')['description']) |
| 113 | 127 |
| 114 def testRemoveNoDocs(self): | 128 def testRemoveNoDocs(self): |
| 115 d = json.loads(self._ReadLocalFile('nodoc_test.json')) | 129 d = self._LoadJSON('nodoc_test.json') |
| 116 _RemoveNoDocs(d) | 130 _RemoveNoDocs(d) |
| 117 self.assertEqual(json.loads(self._ReadLocalFile('expected_nodoc.json')), d) | 131 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) |
| 118 | 132 |
| 119 if __name__ == '__main__': | 133 if __name__ == '__main__': |
| 120 unittest.main() | 134 unittest.main() |
| OLD | NEW |