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 unittest | 8 import unittest |
9 | 9 |
10 from handlebar_dict_generator import HandlebarDictGenerator | 10 from handlebar_dict_generator import HandlebarDictGenerator |
11 from handlebar_dict_generator import _GetLinkToRefType | 11 from handlebar_dict_generator import _GetLinkToRefType |
12 from handlebar_dict_generator import _FormatValue | 12 from handlebar_dict_generator import _FormatValue |
13 import third_party.json_schema_compiler.json_comment_eater as comment_eater | 13 import third_party.json_schema_compiler.json_comment_eater as comment_eater |
14 import third_party.json_schema_compiler.model as model | 14 import third_party.json_schema_compiler.model as model |
15 | 15 |
| 16 def _MakeLink(href, text): |
| 17 return '<a href="%s">%s</a>' % (href, text) |
| 18 |
| 19 def _GetType(dict_, name): |
| 20 for type_ in dict_['types']: |
| 21 if type_['name'] == name: |
| 22 return type_ |
| 23 |
16 class DictGeneratorTest(unittest.TestCase): | 24 class DictGeneratorTest(unittest.TestCase): |
17 def setUp(self): | 25 def setUp(self): |
18 self._base_path = os.path.join('test_data', 'test_json') | 26 self._base_path = os.path.join('test_data', 'test_json') |
19 | 27 |
20 def _ReadLocalFile(self, filename): | 28 def _ReadLocalFile(self, filename): |
21 with open(os.path.join(self._base_path, filename), 'r') as f: | 29 with open(os.path.join(self._base_path, filename), 'r') as f: |
22 return f.read() | 30 return f.read() |
23 | 31 |
| 32 def _LoadJSON(self, filename): |
| 33 return json.loads(comment_eater.Nom(self._ReadLocalFile(filename)))[0] |
| 34 |
24 def _GenerateTest(self, filename): | 35 def _GenerateTest(self, filename): |
25 expected_json = json.loads(self._ReadLocalFile('expected_' + filename)) | 36 expected_json = json.loads(self._ReadLocalFile('expected_' + filename)) |
26 gen = HandlebarDictGenerator( | 37 gen = HandlebarDictGenerator(self._LoadJSON(filename)) |
27 json.loads(comment_eater.Nom(self._ReadLocalFile(filename)))[0]) | |
28 self.assertEquals(expected_json, gen.Generate()) | 38 self.assertEquals(expected_json, gen.Generate()) |
29 | 39 |
30 def testGenerate(self): | 40 def testGenerate(self): |
31 self._GenerateTest('test_file.json') | 41 self._GenerateTest('test_file.json') |
32 | 42 |
33 def testGetLinkToRefType(self): | 43 def testGetLinkToRefType(self): |
34 link = _GetLinkToRefType('truthTeller', 'liar.Tab') | 44 link = _GetLinkToRefType('truthTeller', 'liar.Tab') |
35 self.assertEquals(link['href'], 'liar.html#type-Tab') | 45 self.assertEquals('liar.html#type-Tab', link['href']) |
36 self.assertEquals(link['text'], 'Tab') | 46 self.assertEquals('liar.Tab', link['text']) |
37 link = _GetLinkToRefType('truthTeller', 'Tab') | 47 link = _GetLinkToRefType('truthTeller', 'Tab') |
38 self.assertEquals(link['href'], 'truthTeller.html#type-Tab') | 48 self.assertEquals('#type-Tab', link['href']) |
39 self.assertEquals(link['text'], 'Tab') | 49 self.assertEquals('Tab', link['text']) |
40 link = _GetLinkToRefType('nay', 'lies.chrome.bookmarks.Tab') | 50 link = _GetLinkToRefType('nay', 'lies.chrome.bookmarks.Tab') |
41 self.assertEquals(link['href'], 'lies.html#type-chrome.bookmarks.Tab') | 51 self.assertEquals('lies.chrome.bookmarks.html#type-Tab', link['href']) |
42 self.assertEquals(link['text'], 'chrome.bookmarks.Tab') | 52 self.assertEquals('lies.chrome.bookmarks.Tab', link['text']) |
43 | 53 |
44 def testFormatValue(self): | 54 def testFormatValue(self): |
45 self.assertEquals('1,234,567', _FormatValue(1234567)) | 55 self.assertEquals('1,234,567', _FormatValue(1234567)) |
46 self.assertEquals('67', _FormatValue(67)) | 56 self.assertEquals('67', _FormatValue(67)) |
47 self.assertEquals('234,567', _FormatValue(234567)) | 57 self.assertEquals('234,567', _FormatValue(234567)) |
48 | 58 |
| 59 def testFormatDescription(self): |
| 60 dict_ = HandlebarDictGenerator(self._LoadJSON('ref_test.json')).Generate() |
| 61 self.assertEquals(_MakeLink('#type-type2', 'type2'), |
| 62 _GetType(dict_, 'type1')['description']) |
| 63 self.assertEquals( |
| 64 'A %s, or %s' % (_MakeLink('#type-type3', 'type3'), |
| 65 _MakeLink('#type-type2', 'type2')), |
| 66 _GetType(dict_, 'type2')['description']) |
| 67 self.assertEquals( |
| 68 '%s != %s' % (_MakeLink('other.html#type-type2', 'other.type2'), |
| 69 _MakeLink('#type-type2', 'type2')), |
| 70 _GetType(dict_, 'type3')['description']) |
| 71 |
49 if __name__ == '__main__': | 72 if __name__ == '__main__': |
50 unittest.main() | 73 unittest.main() |
OLD | NEW |