| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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 FakeAvailabilityDataSourceFactory(object): |
| 32 def Create(self): |
| 33 return {} |
| 34 |
| 35 def _FakeGetAvailability(self): |
| 36 return 'Not Currently Available' |
| 37 |
| 31 class FakeSamplesDataSource(object): | 38 class FakeSamplesDataSource(object): |
| 32 def Create(self, request): | 39 def Create(self, request): |
| 33 return {} | 40 return {} |
| 34 | 41 |
| 35 class FakeAPIAndListDataSource(object): | 42 class FakeAPIAndListDataSource(object): |
| 36 def __init__(self, json_data): | 43 def __init__(self, json_data): |
| 37 self._json = json_data | 44 self._json = json_data |
| 38 | 45 |
| 39 def Create(self, *args, **kwargs): | 46 def Create(self, *args, **kwargs): |
| 40 return self | 47 return self |
| 41 | 48 |
| 42 def get(self, key): | 49 def get(self, key): |
| 43 if key not in self._json: | 50 if key not in self._json: |
| 44 raise FileNotFoundError(key) | 51 raise FileNotFoundError(key) |
| 45 return self._json[key] | 52 return self._json[key] |
| 46 | 53 |
| 47 def GetAllNames(self): | 54 def GetAllNames(self): |
| 48 return self._json.keys() | 55 return self._json.keys() |
| 49 | 56 |
| 50 class APIDataSourceTest(unittest.TestCase): | 57 class APIDataSourceTest(unittest.TestCase): |
| 51 def setUp(self): | 58 def setUp(self): |
| 52 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 59 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') |
| 60 _JSCModel._GetAvailability = _FakeGetAvailability |
| 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, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 84 test3.pop('samples') | 92 test3.pop('samples') |
| 85 self.assertEqual(expected, test3) | 93 self.assertEqual(expected, test3) |
| 86 self.assertRaises(FileNotFoundError, data_source.get, 'junk') | 94 self.assertRaises(FileNotFoundError, data_source.get, 'junk') |
| 87 | 95 |
| 88 def _LoadJSON(self, filename): | 96 def _LoadJSON(self, filename): |
| 89 return json.loads(self._ReadLocalFile(filename)) | 97 return json.loads(self._ReadLocalFile(filename)) |
| 90 | 98 |
| 91 def testCreateId(self): | 99 def testCreateId(self): |
| 92 data_source = FakeAPIAndListDataSource( | 100 data_source = FakeAPIAndListDataSource( |
| 93 self._LoadJSON('test_file_data_source.json')) | 101 self._LoadJSON('test_file_data_source.json')) |
| 102 fake_avail_factory = FakeAvailabilityDataSourceFactory() |
| 94 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], | 103 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0], |
| 95 self._CreateRefResolver('test_file_data_source.json'), | 104 self._CreateRefResolver('test_file_data_source.json'), |
| 96 False).ToDict() | 105 False, |
| 106 fake_avail_factory).ToDict() |
| 97 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 107 self.assertEquals('type-TypeA', dict_['types'][0]['id']) |
| 98 self.assertEquals('property-TypeA-b', | 108 self.assertEquals('property-TypeA-b', |
| 99 dict_['types'][0]['properties'][0]['id']) | 109 dict_['types'][0]['properties'][0]['id']) |
| 100 self.assertEquals('method-get', dict_['functions'][0]['id']) | 110 self.assertEquals('method-get', dict_['functions'][0]['id']) |
| 101 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 111 self.assertEquals('event-EventA', dict_['events'][0]['id']) |
| 102 | 112 |
| 103 # TODO(kalman): re-enable this when we have a rebase option. | 113 # TODO(kalman): re-enable this when we have a rebase option. |
| 104 def DISABLED_testToDict(self): | 114 def DISABLED_testToDict(self): |
| 105 filename = 'test_file.json' | 115 filename = 'test_file.json' |
| 106 expected_json = self._LoadJSON('expected_' + filename) | 116 expected_json = self._LoadJSON('expected_' + filename) |
| 107 data_source = FakeAPIAndListDataSource( | 117 data_source = FakeAPIAndListDataSource( |
| 108 self._LoadJSON('test_file_data_source.json')) | 118 self._LoadJSON('test_file_data_source.json')) |
| 109 dict_ = _JSCModel(self._LoadJSON(filename)[0], | 119 dict_ = _JSCModel(self._LoadJSON(filename)[0], |
| 110 self._CreateRefResolver('test_file_data_source.json'), | 120 self._CreateRefResolver('test_file_data_source.json'), |
| 111 False).ToDict() | 121 False).ToDict() |
| 112 self.assertEquals(expected_json, dict_) | 122 self.assertEquals(expected_json, dict_) |
| 113 | 123 |
| 114 def testFormatValue(self): | 124 def testFormatValue(self): |
| 115 self.assertEquals('1,234,567', _FormatValue(1234567)) | 125 self.assertEquals('1,234,567', _FormatValue(1234567)) |
| 116 self.assertEquals('67', _FormatValue(67)) | 126 self.assertEquals('67', _FormatValue(67)) |
| 117 self.assertEquals('234,567', _FormatValue(234567)) | 127 self.assertEquals('234,567', _FormatValue(234567)) |
| 118 | 128 |
| 119 def testFormatDescription(self): | 129 def testFormatDescription(self): |
| 130 fake_avail_factory = FakeAvailabilityDataSourceFactory() |
| 120 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], | 131 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0], |
| 121 self._CreateRefResolver('ref_test_data_source.json'), | 132 self._CreateRefResolver('ref_test_data_source.json'), |
| 122 False).ToDict() | 133 False, |
| 134 fake_avail_factory).ToDict() |
| 123 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), | 135 self.assertEquals(_MakeLink('ref_test.html#type-type2', 'type2'), |
| 124 _GetType(dict_, 'type1')['description']) | 136 _GetType(dict_, 'type1')['description']) |
| 125 self.assertEquals( | 137 self.assertEquals( |
| 126 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), | 138 'A %s, or %s' % (_MakeLink('ref_test.html#type-type3', 'type3'), |
| 127 _MakeLink('ref_test.html#type-type2', 'type2')), | 139 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 128 _GetType(dict_, 'type2')['description']) | 140 _GetType(dict_, 'type2')['description']) |
| 129 self.assertEquals( | 141 self.assertEquals( |
| 130 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 142 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 131 _MakeLink('ref_test.html#type-type2', 'type2')), | 143 _MakeLink('ref_test.html#type-type2', 'type2')), |
| 132 _GetType(dict_, 'type3')['description']) | 144 _GetType(dict_, 'type3')['description']) |
| 133 | 145 |
| 134 def testRemoveNoDocs(self): | 146 def testRemoveNoDocs(self): |
| 135 d = self._LoadJSON('nodoc_test.json') | 147 d = self._LoadJSON('nodoc_test.json') |
| 136 _RemoveNoDocs(d) | 148 _RemoveNoDocs(d) |
| 137 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 149 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) |
| 138 | 150 |
| 139 if __name__ == '__main__': | 151 if __name__ == '__main__': |
| 140 unittest.main() | 152 unittest.main() |
| OLD | NEW |