Chromium Code Reviews| 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 __getitem__(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 | |
| 47 | |
| 
 
not at google - send to devlin
2012/11/02 17:26:48
nit: extra \n
 
cduvall
2012/11/03 01:30:05
Done.
 
 | |
| 35 class APIDataSourceTest(unittest.TestCase): | 48 class APIDataSourceTest(unittest.TestCase): | 
| 36 def setUp(self): | 49 def setUp(self): | 
| 37 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 50 self._base_path = os.path.join(sys.path[0], 'test_data', 'test_json') | 
| 38 | 51 | 
| 39 def _ReadLocalFile(self, filename): | 52 def _ReadLocalFile(self, filename): | 
| 40 with open(os.path.join(self._base_path, filename), 'r') as f: | 53 with open(os.path.join(self._base_path, filename), 'r') as f: | 
| 41 return f.read() | 54 return f.read() | 
| 42 | 55 | 
| 43 def DISABLED_testSimple(self): | 56 def DISABLED_testSimple(self): | 
| 44 cache_factory = CompiledFileSystem.Factory( | 57 cache_factory = CompiledFileSystem.Factory( | 
| (...skipping 12 matching lines...) Expand all Loading... | |
| 57 self.assertEqual(expected, test1) | 70 self.assertEqual(expected, test1) | 
| 58 test2 = data_source['testFile'] | 71 test2 = data_source['testFile'] | 
| 59 test2.pop('samples') | 72 test2.pop('samples') | 
| 60 self.assertEqual(expected, test2) | 73 self.assertEqual(expected, test2) | 
| 61 test3 = data_source['testFile.html'] | 74 test3 = data_source['testFile.html'] | 
| 62 test3.pop('samples') | 75 test3.pop('samples') | 
| 63 self.assertEqual(expected, test3) | 76 self.assertEqual(expected, test3) | 
| 64 self.assertRaises(FileNotFoundError, data_source.get, 'junk') | 77 self.assertRaises(FileNotFoundError, data_source.get, 'junk') | 
| 65 | 78 | 
| 66 def _LoadJSON(self, filename): | 79 def _LoadJSON(self, filename): | 
| 67 return json.loads(comment_eater.Nom(self._ReadLocalFile(filename)))[0] | 80 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 | 81 | 
| 74 def testCreateId(self): | 82 def testCreateId(self): | 
| 75 dict_ = _JscModel(self._LoadJSON('test_file.json')).ToDict() | 83 data_source = FakeAPIAndListDataSource( | 
| 84 self._LoadJSON('test_file_data_source.json')) | |
| 85 dict_ = _JSCModel(self._LoadJSON('test_file.json')[0]).ToDict( | |
| 86 ReferenceResolver(data_source, data_source)) | |
| 76 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 87 self.assertEquals('type-TypeA', dict_['types'][0]['id']) | 
| 77 self.assertEquals('property-TypeA-b', | 88 self.assertEquals('property-TypeA-b', | 
| 78 dict_['types'][0]['properties'][0]['id']) | 89 dict_['types'][0]['properties'][0]['id']) | 
| 79 self.assertEquals('method-get', dict_['functions'][0]['id']) | 90 self.assertEquals('method-get', dict_['functions'][0]['id']) | 
| 80 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 91 self.assertEquals('event-EventA', dict_['events'][0]['id']) | 
| 81 | 92 | 
| 82 def testToDict(self): | 93 def testToDict(self): | 
| 83 self._ToDictTest('test_file.json') | 94 filename = 'test_file.json' | 
| 84 | 95 expected_json = self._LoadJSON('expected_' + filename) | 
| 85 def testGetLinkToRefType(self): | 96 data_source = FakeAPIAndListDataSource( | 
| 86 link = GetLinkToRefType('truthTeller', 'liar.Tab') | 97 self._LoadJSON('test_file_data_source.json')) | 
| 87 self.assertEquals('liar.html#type-Tab', link['href']) | 98 gen = _JSCModel(self._LoadJSON(filename)[0]) | 
| 88 self.assertEquals('liar.Tab', link['text']) | 99 self.assertEquals(expected_json, gen.ToDict( | 
| 89 link = GetLinkToRefType('truthTeller', 'Tab') | 100 ReferenceResolver(data_source, data_source))) | 
| 90 self.assertEquals('#type-Tab', link['href']) | |
| 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 | 101 | 
| 96 def testFormatValue(self): | 102 def testFormatValue(self): | 
| 97 self.assertEquals('1,234,567', _FormatValue(1234567)) | 103 self.assertEquals('1,234,567', _FormatValue(1234567)) | 
| 98 self.assertEquals('67', _FormatValue(67)) | 104 self.assertEquals('67', _FormatValue(67)) | 
| 99 self.assertEquals('234,567', _FormatValue(234567)) | 105 self.assertEquals('234,567', _FormatValue(234567)) | 
| 100 | 106 | 
| 101 def testFormatDescription(self): | 107 def testFormatDescription(self): | 
| 102 dict_ = _JscModel(self._LoadJSON('ref_test.json')).ToDict() | 108 data_source = FakeAPIAndListDataSource( | 
| 109 self._LoadJSON('ref_test_data_source.json')) | |
| 110 dict_ = _JSCModel(self._LoadJSON('ref_test.json')[0]).ToDict( | |
| 111 ReferenceResolver(data_source, data_source)) | |
| 103 self.assertEquals(_MakeLink('#type-type2', 'type2'), | 112 self.assertEquals(_MakeLink('#type-type2', 'type2'), | 
| 104 _GetType(dict_, 'type1')['description']) | 113 _GetType(dict_, 'type1')['description']) | 
| 105 self.assertEquals( | 114 self.assertEquals( | 
| 106 'A %s, or %s' % (_MakeLink('#type-type3', 'type3'), | 115 'A %s, or %s' % (_MakeLink('#type-type3', 'type3'), | 
| 107 _MakeLink('#type-type2', 'type2')), | 116 _MakeLink('#type-type2', 'type2')), | 
| 108 _GetType(dict_, 'type2')['description']) | 117 _GetType(dict_, 'type2')['description']) | 
| 109 self.assertEquals( | 118 self.assertEquals( | 
| 110 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 119 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), | 
| 111 _MakeLink('#type-type2', 'type2')), | 120 _MakeLink('#type-type2', 'type2')), | 
| 112 _GetType(dict_, 'type3')['description']) | 121 _GetType(dict_, 'type3')['description']) | 
| 113 | 122 | 
| 114 def testRemoveNoDocs(self): | 123 def testRemoveNoDocs(self): | 
| 115 d = json.loads(self._ReadLocalFile('nodoc_test.json')) | 124 d = self._LoadJSON('nodoc_test.json') | 
| 116 _RemoveNoDocs(d) | 125 _RemoveNoDocs(d) | 
| 117 self.assertEqual(json.loads(self._ReadLocalFile('expected_nodoc.json')), d) | 126 self.assertEqual(self._LoadJSON('expected_nodoc.json'), d) | 
| 118 | 127 | 
| 119 if __name__ == '__main__': | 128 if __name__ == '__main__': | 
| 120 unittest.main() | 129 unittest.main() | 
| OLD | NEW |