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 file_system import FileNotFoundError | 11 from file_system import FileNotFoundError |
12 from reference_resolver import ReferenceResolver | 12 from reference_resolver import ReferenceResolver |
13 from test_object_store import TestObjectStore | 13 from test_object_store import TestObjectStore |
14 | 14 |
| 15 |
15 class FakeAPIDataSource(object): | 16 class FakeAPIDataSource(object): |
16 def __init__(self, json_data): | 17 def __init__(self, json_data): |
17 self._json = json_data | 18 self._json = json_data |
18 | 19 |
19 def get(self, key, disable_refs=False): | 20 def get(self, key, disable_refs=False): |
20 assert disable_refs, 'ReferenceResolve should be disabling refs' | 21 assert disable_refs, 'ReferenceResolve should be disabling refs' |
21 if key not in self._json: | 22 if key not in self._json: |
22 raise FileNotFoundError(key) | 23 raise FileNotFoundError(key) |
23 return self._json[key] | 24 return self._json[key] |
24 | 25 |
25 def GetAllNames(self): | 26 |
26 return self._json.keys() | 27 class FakeAPIModels(object): |
| 28 def __init__(self, names): |
| 29 self._names = names |
| 30 |
| 31 def GetNames(self): |
| 32 return self._names |
| 33 |
27 | 34 |
28 class APIDataSourceTest(unittest.TestCase): | 35 class APIDataSourceTest(unittest.TestCase): |
29 def setUp(self): | 36 def setUp(self): |
30 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 37 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
31 | 38 |
32 def _ReadLocalFile(self, filename): | 39 def _ReadLocalFile(self, filename): |
33 with open(os.path.join(self._base_path, filename), 'r') as f: | 40 with open(os.path.join(self._base_path, filename), 'r') as f: |
34 return f.read() | 41 return f.read() |
35 | 42 |
36 def testGetLink(self): | 43 def testGetLink(self): |
37 data_source = FakeAPIDataSource( | 44 test_data = json.loads(self._ReadLocalFile('fake_data_source.json')) |
38 json.loads(self._ReadLocalFile('fake_data_source.json'))) | 45 resolver = ReferenceResolver(FakeAPIDataSource(test_data), |
39 resolver = ReferenceResolver(data_source, | 46 FakeAPIModels(test_data.keys()), |
40 data_source, | |
41 TestObjectStore('test')) | 47 TestObjectStore('test')) |
42 self.assertEqual({ | 48 self.assertEqual({ |
43 'href': 'foo.html', | 49 'href': 'foo.html', |
44 'text': 'foo', | 50 'text': 'foo', |
45 'name': 'foo' | 51 'name': 'foo' |
46 }, resolver.GetLink('foo', namespace='baz')) | 52 }, resolver.GetLink('foo', namespace='baz')) |
47 self.assertEqual({ | 53 self.assertEqual({ |
48 'href': 'foo.html#type-foo_t1', | 54 'href': 'foo.html#type-foo_t1', |
49 'text': 'foo.foo_t1', | 55 'text': 'foo.foo_t1', |
50 'name': 'foo_t1' | 56 'name': 'foo_t1' |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 namespace='baz')) | 165 namespace='baz')) |
160 | 166 |
161 # Allow bare "$ref:foo.bar." at the end of a string. | 167 # Allow bare "$ref:foo.bar." at the end of a string. |
162 self.assertEqual( | 168 self.assertEqual( |
163 '<a href="bar.html#type-bon">bar.bon</a>.', | 169 '<a href="bar.html#type-bon">bar.bon</a>.', |
164 resolver.ResolveAllLinks('$ref:bar.bon.', | 170 resolver.ResolveAllLinks('$ref:bar.bon.', |
165 namespace='baz')) | 171 namespace='baz')) |
166 | 172 |
167 if __name__ == '__main__': | 173 if __name__ == '__main__': |
168 unittest.main() | 174 unittest.main() |
OLD | NEW |