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 copy | 6 from copy import deepcopy |
7 import json | 7 import json |
8 import os | 8 import os |
9 import sys | 9 import sys |
10 import unittest | 10 import unittest |
11 | 11 |
12 from api_data_source import (APIDataSource, | 12 from api_data_source import (APIDataSource, |
13 _JSCModel, | 13 _JSCModel, |
14 _FormatValue, | 14 _FormatValue, |
15 _RemoveNoDocs, | 15 _RemoveNoDocs, |
16 _InlineDocs) | 16 _InlineDocs) |
17 from compiled_file_system import CompiledFileSystem | 17 from compiled_file_system import CompiledFileSystem |
18 from file_system import FileNotFoundError | 18 from file_system import FileNotFoundError |
19 from local_file_system import LocalFileSystem | 19 from local_file_system import LocalFileSystem |
20 from object_store_creator import ObjectStoreCreator | 20 from object_store_creator import ObjectStoreCreator |
21 from reference_resolver import ReferenceResolver | 21 from reference_resolver import ReferenceResolver |
| 22 from test_file_system import TestFileSystem |
22 | 23 |
23 def _MakeLink(href, text): | 24 def _MakeLink(href, text): |
24 return '<a href="%s">%s</a>' % (href, text) | 25 return '<a href="%s">%s</a>' % (href, text) |
25 | 26 |
26 def _GetType(dict_, name): | 27 def _GetType(dict_, name): |
27 for type_ in dict_['types']: | 28 for type_ in dict_['types']: |
28 if type_['name'] == name: | 29 if type_['name'] == name: |
29 return type_ | 30 return type_ |
30 | 31 |
| 32 class FakeAvailabilityDataSource(object): |
| 33 def GetAvailability(self, version): |
| 34 return None |
| 35 |
31 class FakeSamplesDataSource(object): | 36 class FakeSamplesDataSource(object): |
32 def Create(self, request): | 37 def Create(self, request): |
33 return {} | 38 return {} |
34 | 39 |
35 class FakeAPIAndListDataSource(object): | 40 class FakeAPIAndListDataSource(object): |
36 def __init__(self, json_data): | 41 def __init__(self, json_data): |
37 self._json = json_data | 42 self._json = json_data |
38 | 43 |
39 def Create(self, *args, **kwargs): | 44 def Create(self, *args, **kwargs): |
40 return self | 45 return self |
41 | 46 |
42 def get(self, key): | 47 def get(self, key): |
43 if key not in self._json: | 48 if key not in self._json: |
44 raise FileNotFoundError(key) | 49 raise FileNotFoundError(key) |
45 return self._json[key] | 50 return self._json[key] |
46 | 51 |
47 def GetAllNames(self): | 52 def GetAllNames(self): |
48 return self._json.keys() | 53 return self._json.keys() |
49 | 54 |
50 class APIDataSourceTest(unittest.TestCase): | 55 class APIDataSourceTest(unittest.TestCase): |
51 def setUp(self): | 56 def setUp(self): |
52 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 57 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 58 def test_file_system_creator(branch): |
| 59 return TestFileSystem(self._LoadJSON('basic_docs_filesystem.json')) |
| 60 self._test_file_system_creator = test_file_system_creator |
53 | 61 |
54 def _ReadLocalFile(self, filename): | 62 def _ReadLocalFile(self, filename): |
55 with open(os.path.join(self._base_path, filename), 'r') as f: | 63 with open(os.path.join(self._base_path, filename), 'r') as f: |
56 return f.read() | 64 return f.read() |
57 | 65 |
58 def _CreateRefResolver(self, filename): | 66 def _CreateRefResolver(self, filename): |
59 data_source = FakeAPIAndListDataSource( | 67 data_source = FakeAPIAndListDataSource( |
60 self._LoadJSON(filename)) | 68 self._LoadJSON(filename)) |
61 return ReferenceResolver.Factory(data_source, | 69 return ReferenceResolver.Factory(data_source, |
62 data_source, | 70 data_source, |
63 ObjectStoreCreator.ForTest()).Create() | 71 ObjectStoreCreator.ForTest()).Create() |
64 | 72 |
65 def _LoadJSON(self, filename): | 73 def _LoadJSON(self, filename): |
66 return json.loads(self._ReadLocalFile(filename)) | 74 return json.loads(self._ReadLocalFile(filename)) |
67 | 75 |
68 def testCreateId(self): | 76 def testCreateId(self): |
69 data_source = FakeAPIAndListDataSource( | 77 data_source = FakeAPIAndListDataSource( |
70 self._LoadJSON('test_file_data_source.json')) | 78 self._LoadJSON('test_file_data_source.json')) |
71 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 79 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
72 self._CreateRefResolver('test_file_data_source.json'), | 80 self._CreateRefResolver('test_file_data_source.json'), |
73 False).ToDict() | 81 False, |
| 82 FakeAvailabilityDataSource(), |
| 83 self._test_file_system_creator).ToDict() |
74 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 84 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
75 self.assertEquals('property-TypeA-b', | 85 self.assertEquals('property-TypeA-b', |
76 dict_['types'][0]['properties'][0]['id']) | 86 dict_['types'][0]['properties'][0]['id']) |
77 self.assertEquals('method-get', dict_['functions'][0]['id']) | 87 self.assertEquals('method-get', dict_['functions'][0]['id']) |
78 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 88 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
79 | 89 |
80 # TODO(kalman): re-enable this when we have a rebase option. | 90 # TODO(kalman): re-enable this when we have a rebase option. |
81 def DISABLED_testToDict(self): | 91 def DISABLED_testToDict(self): |
82 filename = 'test_file.json' | 92 filename = 'test_file.json' |
83 expected_json = self._LoadJSON('expected_' + filename) | 93 expected_json = self._LoadJSON('expected_' + filename) |
84 data_source = FakeAPIAndListDataSource( | 94 data_source = FakeAPIAndListDataSource( |
85 self._LoadJSON('test_file_data_source.json')) | 95 self._LoadJSON('test_file_data_source.json')) |
86 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 96 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
87 self._CreateRefResolver('test_file_data_source.json'), | 97 self._CreateRefResolver('test_file_data_source.json'), |
88 False).ToDict() | 98 False, |
| 99 FakeAvailabilityDataSource(), |
| 100 self._test_file_system_creator).ToDict() |
89 self.assertEquals(expected_json, dict_) | 101 self.assertEquals(expected_json, dict_) |
90 | 102 |
91 def testFormatValue(self): | 103 def testFormatValue(self): |
92 self.assertEquals('1,234,567', _FormatValue(1234567)) | 104 self.assertEquals('1,234,567', _FormatValue(1234567)) |
93 self.assertEquals('67', _FormatValue(67)) | 105 self.assertEquals('67', _FormatValue(67)) |
94 self.assertEquals('234,567', _FormatValue(234567)) | 106 self.assertEquals('234,567', _FormatValue(234567)) |
95 | 107 |
96 def testFormatDescription(self): | 108 def testFormatDescription(self): |
97 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 109 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
98 self._CreateRefResolver('ref_test_data_source.json'), | 110 self._CreateRefResolver('ref_test_data_source.json'), |
99 False).ToDict() | 111 False, |
| 112 FakeAvailabilityDataSource(), |
| 113 self._test_file_system_creator).ToDict() |
100 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 114 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
101 _GetType(dict_, 'type1')['description']) | 115 _GetType(dict_, 'type1')['description']) |
102 self.assertEquals( | 116 self.assertEquals( |
103 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 117 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
104 _MakeLink('ref_test.html#type-type2', 'type2')), | 118 _MakeLink('ref_test.html#type-type2', 'type2')), |
105 _GetType(dict_, 'type2')['description']) | 119 _GetType(dict_, 'type2')['description']) |
106 self.assertEquals( | 120 self.assertEquals( |
107 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 121 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
108 _MakeLink('ref_test.html#type-type2', 'type2')), | 122 _MakeLink('ref_test.html#type-type2', 'type2')), |
109 _GetType(dict_, 'type3')['description']) | 123 _GetType(dict_, 'type3')['description']) |
110 | 124 |
111 def testRemoveNoDocs(self): | 125 def testRemoveNoDocs(self): |
112 d = self._LoadJSON('nodoc_test.json') | 126 d = self._LoadJSON('nodoc_test.json') |
113 _RemoveNoDocs(d) | 127 _RemoveNoDocs(d) |
114 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 128 self.assertEquals(self._LoadJSON('expected_nodoc.json'), d) |
| 129 |
| 130 def testGetPermissions(self): |
| 131 model = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 132 self._CreateRefResolver('test_file_data_source.json'), |
| 133 False, |
| 134 FakeAvailabilityDataSource(), |
| 135 self._test_file_system_creator) |
| 136 self.assertEquals(model._GetPermissions(), |
| 137 self._LoadJSON('api_permissions_expected.json')) |
| 138 |
| 139 def testGetMoreLearning(self): |
| 140 model = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 141 self._CreateRefResolver('test_file_data_source.json'), |
| 142 False, |
| 143 FakeAvailabilityDataSource(), |
| 144 self._test_file_system_creator) |
| 145 self.assertEquals(model._GetMoreLearning(), |
| 146 self._LoadJSON('api_learn_more_expected.json')) |
115 | 147 |
116 def testInlineDocs(self): | 148 def testInlineDocs(self): |
117 schema = { | 149 schema = { |
118 "namespace": "storage", | 150 "namespace": "storage", |
119 "properties": { | 151 "properties": { |
120 "key2": { | 152 "key2": { |
121 "description": "second key", | 153 "description": "second key", |
122 "$ref": "Key" | 154 "$ref": "Key" |
123 }, | 155 }, |
124 "key1": { | 156 "key1": { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 "marker": True, | 198 "marker": True, |
167 "type": "string" | 199 "type": "string" |
168 }, | 200 }, |
169 "type": "array", | 201 "type": "array", |
170 "id": "KeyList", | 202 "id": "KeyList", |
171 "description": "A list of keys" | 203 "description": "A list of keys" |
172 } | 204 } |
173 ] | 205 ] |
174 } | 206 } |
175 | 207 |
176 inlined_schema = copy.deepcopy(schema) | 208 inlined_schema = deepcopy(schema) |
177 _InlineDocs(inlined_schema) | 209 _InlineDocs(inlined_schema) |
178 self.assertEqual(expected_schema, inlined_schema) | 210 self.assertEqual(expected_schema, inlined_schema) |
179 | 211 |
180 | 212 |
181 if __name__ == '__main__': | 213 if __name__ == '__main__': |
182 unittest.main() | 214 unittest.main() |
OLD | NEW |