OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 """Unittest for chrome_messages_json.py. | |
7 """ | |
8 | |
9 import os | |
10 import sys | |
11 if __name__ == '__main__': | |
12 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | |
13 | |
14 import unittest | |
15 import StringIO | |
16 | |
17 from grit import grd_reader | |
18 from grit import util | |
19 from grit.tool import build | |
20 | |
21 class ChromeMessagesJsonFormatUnittest(unittest.TestCase): | |
22 | |
23 def testMessages(self): | |
24 root = util.ParseGrdForUnittest(u""" | |
25 <messages> | |
26 <message name="IDS_SIMPLE_MESSAGE"> | |
27 Simple message. | |
28 </message> | |
29 <message name="IDS_QUOTES"> | |
30 element\u2019s \u201c<ph name="NAME">%s<ex>name</ex></ph>\u201d at
tribute | |
31 </message> | |
32 <message name="IDS_PLACEHOLDERS"> | |
33 <ph name="ERROR_COUNT">%1$d<ex>1</ex></ph> error, <ph name="WARNIN
G_COUNT">%2$d<ex>1</ex></ph> warning | |
34 </message> | |
35 <message name="IDS_STARTS_WITH_SPACE"> | |
36 ''' (<ph name="COUNT">%d<ex>2</ex></ph>) | |
37 </message> | |
38 <message name="IDS_ENDS_WITH_SPACE"> | |
39 (<ph name="COUNT">%d<ex>2</ex></ph>) ''' | |
40 </message> | |
41 <message name="IDS_SPACE_AT_BOTH_ENDS"> | |
42 ''' (<ph name="COUNT">%d<ex>2</ex></ph>) ''' | |
43 </message> | |
44 <message name="IDS_DOUBLE_QUOTES"> | |
45 A "double quoted" message. | |
46 </message> | |
47 <message name="IDS_BACKSLASH"> | |
48 \\ | |
49 </message> | |
50 </messages> | |
51 """) | |
52 | |
53 buf = StringIO.StringIO() | |
54 build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'en'),
buf) | |
55 output = buf.getvalue() | |
56 test = u""" | |
57 { | |
58 "SIMPLE_MESSAGE": { | |
59 "message": "Simple message." | |
60 }, | |
61 "QUOTES": { | |
62 "message": "element\\u2019s \\u201c%s\\u201d attribute" | |
63 }, | |
64 "PLACEHOLDERS": { | |
65 "message": "%1$d error, %2$d warning" | |
66 }, | |
67 "STARTS_WITH_SPACE": { | |
68 "message": " (%d)" | |
69 }, | |
70 "ENDS_WITH_SPACE": { | |
71 "message": "(%d) " | |
72 }, | |
73 "SPACE_AT_BOTH_ENDS": { | |
74 "message": " (%d) " | |
75 }, | |
76 "DOUBLE_QUOTES": { | |
77 "message": "A \\"double quoted\\" message." | |
78 }, | |
79 "BACKSLASH": { | |
80 "message": "\\\\" | |
81 } | |
82 } | |
83 """ | |
84 self.assertEqual(test.strip(), output.strip()) | |
85 | |
86 def testTranslations(self): | |
87 root = util.ParseGrdForUnittest(""" | |
88 <messages> | |
89 <message name="ID_HELLO">Hello!</message> | |
90 <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex> | |
91 Joi</ex></ph></message> | |
92 </messages> | |
93 """) | |
94 | |
95 buf = StringIO.StringIO() | |
96 build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'fr'),
buf) | |
97 output = buf.getvalue() | |
98 test = u""" | |
99 { | |
100 "ID_HELLO": { | |
101 "message": "H\\u00e9P\\u00e9ll\\u00f4P\\u00f4!" | |
102 }, | |
103 "ID_HELLO_USER": { | |
104 "message": "H\\u00e9P\\u00e9ll\\u00f4P\\u00f4 %s" | |
105 } | |
106 } | |
107 """ | |
108 self.assertEqual(test.strip(), output.strip()) | |
109 | |
110 | |
111 class DummyOutput(object): | |
112 | |
113 def __init__(self, type, language): | |
114 self.type = type | |
115 self.language = language | |
116 | |
117 def GetType(self): | |
118 return self.type | |
119 | |
120 def GetLanguage(self): | |
121 return self.language | |
122 | |
123 def GetOutputFilename(self): | |
124 return 'hello.gif' | |
125 | |
126 | |
127 if __name__ == '__main__': | |
128 unittest.main() | |
OLD | NEW |