| 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 samples_data_source import SamplesDataSource | 11 from samples_data_source import SamplesDataSource, _MakeAPILink |
| 12 | 12 |
| 13 class SamplesDataSourceTest(unittest.TestCase): | 13 class SamplesDataSourceTest(unittest.TestCase): |
| 14 def setUp(self): | 14 def setUp(self): |
| 15 self._base_path = os.path.join(sys.path[0], | 15 self._base_path = os.path.join(sys.path[0], |
| 16 'test_data', | 16 'test_data', |
| 17 'samples_data_source') | 17 'samples_data_source') |
| 18 | 18 |
| 19 def _ReadLocalFile(self, filename): | 19 def _ReadLocalFile(self, filename): |
| 20 with open(os.path.join(self._base_path, filename), 'r') as f: | 20 with open(os.path.join(self._base_path, filename), 'r') as f: |
| 21 return f.read() | 21 return f.read() |
| 22 | 22 |
| 23 def _FakeGet(self, key): | 23 def _FakeGet(self, key): |
| 24 return json.loads(self._ReadLocalFile(key)) | 24 return json.loads(self._ReadLocalFile(key)) |
| 25 | 25 |
| 26 def testFilterSamples(self): | 26 def testFilterSamples(self): |
| 27 sds = SamplesDataSource({}, {}, 'fake_path', None) | 27 sds = SamplesDataSource({}, {}, 'fake_path', None) |
| 28 sds.get = self._FakeGet | 28 sds.get = self._FakeGet |
| 29 self.assertEquals(json.loads(self._ReadLocalFile('expected.json')), | 29 self.assertEquals(json.loads(self._ReadLocalFile('expected.json')), |
| 30 sds.FilterSamples('samples.json', 'bobaloo')) | 30 sds.FilterSamples('samples.json', 'bobaloo')) |
| 31 | 31 |
| 32 def testMakeAPILink(self): |
| 33 api_list = [ |
| 34 'foo', |
| 35 'bar', |
| 36 'baz', |
| 37 'jim.bob', |
| 38 'jim.bif', |
| 39 'joe.bob.bif' |
| 40 ] |
| 41 self.assertEquals('foo.html#type-baz', |
| 42 _MakeAPILink('type', 'chrome.foo.baz', api_list)) |
| 43 self.assertEquals('jim.bob.html#type-joe', |
| 44 _MakeAPILink('type', 'chrome.jim.bob.joe', api_list)) |
| 45 self.assertEquals('joe.bob.bif.html#event-lenny', |
| 46 _MakeAPILink('event', |
| 47 'chrome.joe.bob.bif.lenny', |
| 48 api_list)) |
| 49 self.assertEquals('baz.html#floop-lox', |
| 50 _MakeAPILink('floop', 'chrome.baz.lox', api_list)) |
| 51 self.assertEquals(None, _MakeAPILink('type', 'chrome.jim.foo', api_list)) |
| 52 self.assertEquals(None, _MakeAPILink('type', 'chrome.joe.bob', api_list)) |
| 53 self.assertEquals(None, _MakeAPILink('type', 'chrome.barn.foo', api_list)) |
| 54 |
| 32 if __name__ == '__main__': | 55 if __name__ == '__main__': |
| 33 unittest.main() | 56 unittest.main() |
| OLD | NEW |