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 '''Unit tests for the rc_header formatter''' | |
7 | |
8 # GRD samples exceed the 80 character limit. | |
9 # pylint: disable-msg=C6310 | |
10 | |
11 import os | |
12 import sys | |
13 if __name__ == '__main__': | |
14 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | |
15 | |
16 import StringIO | |
17 import unittest | |
18 | |
19 from grit import exception | |
20 from grit import grd_reader | |
21 from grit import util | |
22 from grit.format import rc_header | |
23 | |
24 | |
25 class RcHeaderFormatterUnittest(unittest.TestCase): | |
26 def FormatAll(self, grd): | |
27 output = rc_header.FormatDefines(grd, grd.ShouldOutputAllResourceDefines()) | |
28 return ''.join(output).replace(' ', '') | |
29 | |
30 def testFormatter(self): | |
31 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UT
F-8"?> | |
32 <grit latest_public_release="2" source_lang_id="en" current_release="3" ba
se_dir="."> | |
33 <release seq="3"> | |
34 <includes first_id="300" comment="bingo"> | |
35 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> | |
36 </includes> | |
37 <messages first_id="10000"> | |
38 <message name="IDS_GREETING" desc="Printed to greet the currently lo
gged in user"> | |
39 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing t
oday? | |
40 </message> | |
41 <message name="IDS_BONGO"> | |
42 Bongo! | |
43 </message> | |
44 </messages> | |
45 <structures> | |
46 <structure type="dialog" name="IDD_NARROW_DIALOG" file="rc_files/dia
logs.rc" /> | |
47 <structure type="version" name="VS_VERSION_INFO" file="rc_files/vers
ion.rc" /> | |
48 </structures> | |
49 </release> | |
50 </grit>'''), '.') | |
51 output = self.FormatAll(grd) | |
52 self.failUnless(output.count('IDS_GREETING10000')) | |
53 self.failUnless(output.count('ID_LOGO300')) | |
54 | |
55 def testOnlyDefineResourcesThatSatisfyOutputCondition(self): | |
56 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UT
F-8"?> | |
57 <grit latest_public_release="2" source_lang_id="en" current_release="3" | |
58 base_dir="." output_all_resource_defines="false"> | |
59 <release seq="3"> | |
60 <includes first_id="300" comment="bingo"> | |
61 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> | |
62 </includes> | |
63 <messages first_id="10000"> | |
64 <message name="IDS_FIRSTPRESENTSTRING" desc="Present in .rc file."> | |
65 I will appear in the .rc file. | |
66 </message> | |
67 <if expr="False"> <!--Do not include in the .rc files until used.--> | |
68 <message name="IDS_MISSINGSTRING" desc="Not present in .rc file."> | |
69 I will not appear in the .rc file. | |
70 </message> | |
71 </if> | |
72 <if expr="lang != 'es'"> | |
73 <message name="IDS_LANGUAGESPECIFICSTRING" desc="Present in .rc fi
le."> | |
74 Hello. | |
75 </message> | |
76 </if> | |
77 <if expr="lang == 'es'"> | |
78 <message name="IDS_LANGUAGESPECIFICSTRING" desc="Present in .rc fi
le."> | |
79 Hola. | |
80 </message> | |
81 </if> | |
82 <message name="IDS_THIRDPRESENTSTRING" desc="Present in .rc file."> | |
83 I will also appear in the .rc file. | |
84 </message> | |
85 </messages> | |
86 </release> | |
87 </grit>'''), '.') | |
88 output = self.FormatAll(grd) | |
89 self.failUnless(output.count('IDS_FIRSTPRESENTSTRING10000')) | |
90 self.failIf(output.count('IDS_MISSINGSTRING')) | |
91 self.failIf(output.count('10001')) # IDS_MISSINGSTRING should get this ID | |
92 self.failUnless(output.count('IDS_LANGUAGESPECIFICSTRING10002')) | |
93 self.failUnless(output.count('IDS_THIRDPRESENTSTRING10003')) | |
94 | |
95 def testExplicitFirstIdOverlaps(self): | |
96 # second first_id will overlap preexisting range | |
97 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UT
F-8"?> | |
98 <grit latest_public_release="2" source_lang_id="en" current_release="3" ba
se_dir="."> | |
99 <release seq="3"> | |
100 <includes first_id="300" comment="bingo"> | |
101 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> | |
102 <include type="gif" name="ID_LOGO2" file="images/logo2.gif" /> | |
103 </includes> | |
104 <messages first_id="301"> | |
105 <message name="IDS_GREETING" desc="Printed to greet the currently lo
gged in user"> | |
106 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing t
oday? | |
107 </message> | |
108 <message name="IDS_SMURFGEBURF">Frubegfrums</message> | |
109 </messages> | |
110 </release> | |
111 </grit>'''), '.') | |
112 self.assertRaises(exception.IdRangeOverlap, self.FormatAll, grd) | |
113 | |
114 def testImplicitOverlapsPreexisting(self): | |
115 # second message in <messages> will overlap preexisting range | |
116 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UT
F-8"?> | |
117 <grit latest_public_release="2" source_lang_id="en" current_release="3" ba
se_dir="."> | |
118 <release seq="3"> | |
119 <includes first_id="301" comment="bingo"> | |
120 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> | |
121 <include type="gif" name="ID_LOGO2" file="images/logo2.gif" /> | |
122 </includes> | |
123 <messages first_id="300"> | |
124 <message name="IDS_GREETING" desc="Printed to greet the currently lo
gged in user"> | |
125 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing t
oday? | |
126 </message> | |
127 <message name="IDS_SMURFGEBURF">Frubegfrums</message> | |
128 </messages> | |
129 </release> | |
130 </grit>'''), '.') | |
131 self.assertRaises(exception.IdRangeOverlap, self.FormatAll, grd) | |
132 | |
133 def testEmit(self): | |
134 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UT
F-8"?> | |
135 <grit latest_public_release="2" source_lang_id="en" current_release="3" ba
se_dir="."> | |
136 <outputs> | |
137 <output type="rc_all" filename="dummy"> | |
138 <emit emit_type="prepend">Wrong</emit> | |
139 </output> | |
140 <if expr="False"> | |
141 <output type="rc_header" filename="dummy"> | |
142 <emit emit_type="prepend">No</emit> | |
143 </output> | |
144 </if> | |
145 <output type="rc_header" filename="dummy"> | |
146 <emit emit_type="append">Error</emit> | |
147 </output> | |
148 <output type="rc_header" filename="dummy"> | |
149 <emit emit_type="prepend">Bingo</emit> | |
150 </output> | |
151 </outputs> | |
152 </grit>'''), '.') | |
153 output = ''.join(rc_header.Format(grd, 'en', '.')) | |
154 output = util.StripBlankLinesAndComments(output) | |
155 self.assertEqual('#pragma once\nBingo', output) | |
156 | |
157 def testRcHeaderFormat(self): | |
158 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UT
F-8"?> | |
159 <grit latest_public_release="2" source_lang_id="en" current_release="3" ba
se_dir="."> | |
160 <release seq="3"> | |
161 <includes first_id="300" comment="bingo"> | |
162 <include type="gif" name="IDR_LOGO" file="images/logo.gif" /> | |
163 </includes> | |
164 <messages first_id="10000"> | |
165 <message name="IDS_GREETING" desc="Printed to greet the currently lo
gged in user"> | |
166 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing t
oday? | |
167 </message> | |
168 <message name="IDS_BONGO"> | |
169 Bongo! | |
170 </message> | |
171 </messages> | |
172 </release> | |
173 </grit>'''), '.') | |
174 | |
175 # Using the default rc_header format string. | |
176 output = rc_header.FormatDefines(grd, grd.ShouldOutputAllResourceDefines(), | |
177 grd.GetRcHeaderFormat()) | |
178 self.assertEqual(('#define IDR_LOGO 300\n' | |
179 '#define IDS_GREETING 10000\n' | |
180 '#define IDS_BONGO 10001\n'), ''.join(output)) | |
181 | |
182 # Using a custom rc_header format string. | |
183 grd.AssignRcHeaderFormat( | |
184 '#define {textual_id} _Pragma("{textual_id}") {numeric_id}') | |
185 output = rc_header.FormatDefines(grd, grd.ShouldOutputAllResourceDefines(), | |
186 grd.GetRcHeaderFormat()) | |
187 self.assertEqual(('#define IDR_LOGO _Pragma("IDR_LOGO") 300\n' | |
188 '#define IDS_GREETING _Pragma("IDS_GREETING") 10000\n' | |
189 '#define IDS_BONGO _Pragma("IDS_BONGO") 10001\n'), | |
190 ''.join(output)) | |
191 | |
192 if __name__ == '__main__': | |
193 unittest.main() | |
OLD | NEW |