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