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 '''Unit tests for grit.format.data_pack''' | 6 '''Unit tests for grit.format.data_pack''' |
7 | 7 |
8 | 8 |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 if __name__ == '__main__': | 11 if __name__ == '__main__': |
12 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | 12 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) |
13 | 13 |
14 import unittest | 14 import unittest |
15 | 15 |
16 from grit.format import data_pack | 16 from grit.format import data_pack |
17 | 17 |
18 | 18 |
19 class FormatDataPackUnittest(unittest.TestCase): | 19 class FormatDataPackUnittest(unittest.TestCase): |
20 def testWriteDataPack(self): | 20 def testReadDataPackV4(self): |
21 expected = ( | 21 expected_data = ( |
22 '\x04\x00\x00\x00' # header(version | 22 '\x04\x00\x00\x00' # header(version |
23 '\x04\x00\x00\x00' # no. entries, | 23 '\x04\x00\x00\x00' # no. entries, |
24 '\x01' # encoding) | 24 '\x01' # encoding) |
25 '\x01\x00\x27\x00\x00\x00' # index entry 1 | 25 '\x01\x00\x27\x00\x00\x00' # index entry 1 |
26 '\x04\x00\x27\x00\x00\x00' # index entry 4 | 26 '\x04\x00\x27\x00\x00\x00' # index entry 4 |
27 '\x06\x00\x33\x00\x00\x00' # index entry 6 | 27 '\x06\x00\x33\x00\x00\x00' # index entry 6 |
28 '\x0a\x00\x3f\x00\x00\x00' # index entry 10 | 28 '\x0a\x00\x3f\x00\x00\x00' # index entry 10 |
29 '\x00\x00\x3f\x00\x00\x00' # extra entry for the size of last | 29 '\x00\x00\x3f\x00\x00\x00' # extra entry for the size of last |
30 'this is id 4this is id 6') # data | 30 'this is id 4this is id 6') # data |
31 input = {1: '', 4: 'this is id 4', 6: 'this is id 6', 10: ''} | 31 expected_resources = { |
32 output = data_pack.WriteDataPackToString(input, data_pack.UTF8) | 32 1: '', |
33 self.failUnless(output == expected) | 33 4: 'this is id 4', |
34 6: 'this is id 6', | |
35 10: '', | |
36 } | |
37 expected_data_pack = data_pack.DataPackContents( | |
38 expected_resources, data_pack.UTF8) | |
39 loaded = data_pack.ReadDataPackFromString(expected_data) | |
40 self.assertEquals(loaded, expected_data_pack) | |
41 | |
42 def testReadWriteDataPackV5(self): | |
43 expected_data = ( | |
44 '\x05\x00\x01\x00' # version, encoding | |
45 '\x03\x00\x01\x00' # resource_count, alias_count, | |
flackr
2017/07/07 18:54:12
nit: I think the test will read more cleanly with
agrieve
2017/07/07 20:47:08
Done.
| |
46 '\x01\x00\x24\x00\x00\x00' # index entry 1 | |
47 '\x04\x00\x24\x00\x00\x00' # index entry 4 | |
48 '\x06\x00\x30\x00\x00\x00' # index entry 6 | |
49 '\x00\x00\x3c\x00\x00\x00' # extra entry for the size of last | |
50 '\x0a\x00\x01\x00' # alias table | |
51 'this is id 4this is id 6') # data | |
52 expected_resources = { | |
53 1: '', | |
54 4: 'this is id 4', | |
55 6: 'this is id 6', | |
56 10: 'this is id 4', | |
57 } | |
58 data = data_pack.WriteDataPackToString(expected_resources, data_pack.UTF8) | |
59 self.assertEquals(data, expected_data) | |
60 | |
61 expected_data_pack = data_pack.DataPackContents( | |
62 expected_resources, data_pack.UTF8) | |
63 loaded = data_pack.ReadDataPackFromString(expected_data) | |
64 self.assertEquals(loaded, expected_data_pack) | |
34 | 65 |
35 def testRePackUnittest(self): | 66 def testRePackUnittest(self): |
36 expected_with_whitelist = { | 67 expected_with_whitelist = { |
37 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', | 68 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', |
38 30: 'you down', 40: 'Never', 50: 'gonna run around and', | 69 30: 'you down', 40: 'Never', 50: 'gonna run around and', |
39 60: 'desert you'} | 70 60: 'desert you'} |
40 expected_without_whitelist = { | 71 expected_without_whitelist = { |
41 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', 65: 'Close', | 72 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', 65: 'Close', |
42 30: 'you down', 40: 'Never', 50: 'gonna run around and', 4: 'click', | 73 30: 'you down', 40: 'Never', 50: 'gonna run around and', 4: 'click', |
43 60: 'desert you', 6: 'chirr', 32: 'oops, try again', 70: 'Awww, snap!'} | 74 60: 'desert you', 6: 'chirr', 32: 'oops, try again', 70: 'Awww, snap!'} |
44 inputs = [{1: 'Never gonna', 4: 'click', 6: 'chirr', 10: 'give you up'}, | 75 inputs = [{1: 'Never gonna', 4: 'click', 6: 'chirr', 10: 'give you up'}, |
45 {20: 'Never gonna let', 30: 'you down', 32: 'oops, try again'}, | 76 {20: 'Never gonna let', 30: 'you down', 32: 'oops, try again'}, |
46 {40: 'Never', 50: 'gonna run around and', 60: 'desert you'}, | 77 {40: 'Never', 50: 'gonna run around and', 60: 'desert you'}, |
47 {65: 'Close', 70: 'Awww, snap!'}] | 78 {65: 'Close', 70: 'Awww, snap!'}] |
48 whitelist = [1, 10, 20, 30, 40, 50, 60] | 79 whitelist = [1, 10, 20, 30, 40, 50, 60] |
49 inputs = [data_pack.DataPackContents(input, data_pack.UTF8) for input | 80 inputs = [data_pack.DataPackContents(input, data_pack.UTF8) for input |
50 in inputs] | 81 in inputs] |
51 | 82 |
52 # RePack using whitelist | 83 # RePack using whitelist |
53 output, _ = data_pack.RePackFromDataPackStrings(inputs, whitelist) | 84 output, _ = data_pack.RePackFromDataPackStrings( |
85 inputs, whitelist, suppress_removed_key_output=True) | |
54 self.assertDictEqual(expected_with_whitelist, output, | 86 self.assertDictEqual(expected_with_whitelist, output, |
55 'Incorrect resource output') | 87 'Incorrect resource output') |
56 | 88 |
57 # RePack a None whitelist | 89 # RePack a None whitelist |
58 output, _ = data_pack.RePackFromDataPackStrings(inputs, None) | 90 output, _ = data_pack.RePackFromDataPackStrings( |
91 inputs, None, suppress_removed_key_output=True) | |
59 self.assertDictEqual(expected_without_whitelist, output, | 92 self.assertDictEqual(expected_without_whitelist, output, |
60 'Incorrect resource output') | 93 'Incorrect resource output') |
61 | 94 |
62 | 95 |
63 if __name__ == '__main__': | 96 if __name__ == '__main__': |
64 unittest.main() | 97 unittest.main() |
OLD | NEW |