Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(520)

Side by Side Diff: grit/grd_reader_unittest.py

Issue 7994004: Initial source commit to grit-i18n project. (Closed) Base URL: http://grit-i18n.googlecode.com/svn/trunk/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « grit/grd_reader.py ('k') | grit/grit-todo.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 #!/usr/bin/python2.4
2 # Copyright (c) 2011 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 grd_reader package'''
7
8 import os
9 import sys
10 if __name__ == '__main__':
11 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..'))
12
13 import unittest
14 import StringIO
15
16 from grit import grd_reader
17 from grit import constants
18 from grit import util
19 from grit.node import empty
20
21
22 class GrdReaderUnittest(unittest.TestCase):
23 def testParsingAndXmlOutput(self):
24 input = u'''<?xml version="1.0" encoding="UTF-8"?>
25 <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_ dir=".">
26 <release seq="3">
27 <includes>
28 <include name="ID_LOGO" file="images/logo.gif" type="gif" />
29 </includes>
30 <messages>
31 <if expr="True">
32 <message name="IDS_GREETING" desc="Printed to greet the currently logged in user">
33 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing today ?
34 </message>
35 </if>
36 </messages>
37 <structures>
38 <structure name="IDD_NARROW_DIALOG" file="rc_files/dialogs.rc" type="dialo g">
39 <skeleton variant_of_revision="3" expr="lang == 'fr-FR'" file="bla.rc" / >
40 </structure>
41 <structure name="VS_VERSION_INFO" file="rc_files/version.rc" type="version " />
42 </structures>
43 </release>
44 <translations>
45 <file lang="nl" path="nl_translations.xtb" />
46 </translations>
47 <outputs>
48 <output type="rc_header" filename="resource.h" />
49 <output lang="en-US" type="rc_all" filename="resource.rc" />
50 </outputs>
51 </grit>'''
52 pseudo_file = StringIO.StringIO(input)
53 tree = grd_reader.Parse(pseudo_file, '.')
54 output = unicode(tree)
55 # All but first two lines are the same (sans enc_check)
56 self.failUnless('\n'.join(input.split('\n')[2:]) ==
57 '\n'.join(output.split('\n')[2:]))
58 self.failUnless(tree.GetNodeById('IDS_GREETING'))
59
60
61 def testStopAfter(self):
62 input = u'''<?xml version="1.0" encoding="UTF-8"?>
63 <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_ dir=".">
64 <outputs>
65 <output filename="resource.h" type="rc_header" />
66 <output filename="resource.rc" lang="en-US" type="rc_all" />
67 </outputs>
68 <release seq="3">
69 <includes>
70 <include type="gif" name="ID_LOGO" file="images/logo.gif"/>
71 </includes>
72 </release>
73 </grit>'''
74 pseudo_file = util.WrapInputStream(StringIO.StringIO(input))
75 tree = grd_reader.Parse(pseudo_file, '.', stop_after='outputs')
76 # only an <outputs> child
77 self.failUnless(len(tree.children) == 1)
78 self.failUnless(tree.children[0].name == 'outputs')
79
80 def testLongLinesWithComments(self):
81 input = u'''<?xml version="1.0" encoding="UTF-8"?>
82 <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_ dir=".">
83 <release seq="3">
84 <messages>
85 <message name="IDS_GREETING" desc="Printed to greet the currently logged i n user">
86 This is a very long line with no linebreaks yes yes it stretches on <!--
87 -->and on <!--
88 -->and on!
89 </message>
90 </messages>
91 </release>
92 </grit>'''
93 pseudo_file = StringIO.StringIO(input)
94 tree = grd_reader.Parse(pseudo_file, '.')
95
96 greeting = tree.GetNodeById('IDS_GREETING')
97 self.failUnless(greeting.GetCliques()[0].GetMessage().GetRealContent() ==
98 'This is a very long line with no linebreaks yes yes it '
99 'stretches on and on and on!')
100
101 def testAssignFirstIds(self):
102 input = u'''<?xml version="1.0" encoding="UTF-8"?>
103 <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_ dir=".">
104 <release seq="3">
105 <messages>
106 <message name="IDS_TEST" desc="test">
107 test
108 </message>
109 </messages>
110 </release>
111 </grit>'''
112 pseudo_file = StringIO.StringIO(input)
113 root = grd_reader.Parse(pseudo_file, '.')
114 grit_root_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
115 '..')
116 root.AssignFirstIds(
117 os.path.join(grit_root_dir,
118 "grit/testdata/chrome/app/generated_resources.grd"),
119 os.path.join(grit_root_dir, "grit/testdata/tools/grit/resource_ids"),
120 {})
121 messages_node = root.children[0].children[0]
122 self.failUnless(isinstance(messages_node, empty.MessagesNode))
123 self.failUnless(messages_node.attrs["first_id"] !=
124 empty.MessagesNode().DefaultAttributes()["first_id"])
125
126 def testAssignFirstIdsMultipleMessages(self):
127 """If there are multiple messages sections, the resource_ids file
128 needs to list multiple first_id values."""
129 input = u'''<?xml version="1.0" encoding="UTF-8"?>
130 <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_ dir=".">
131 <release seq="3">
132 <messages>
133 <message name="IDS_TEST" desc="test">
134 test
135 </message>
136 </messages>
137 <messages>
138 <message name="IDS_TEST2" desc="test">
139 test2
140 </message>
141 </messages>
142 </release>
143 </grit>'''
144 pseudo_file = StringIO.StringIO(input)
145 root = grd_reader.Parse(pseudo_file, '.')
146 grit_root_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
147 '..')
148 root.AssignFirstIds(
149 # This file does not actually exist, but the path must match
150 # the path of the resource_ids file as it has SRCDIR set to "."
151 os.path.join(grit_root_dir, "grit/testdata/test.grd"),
152 os.path.join(grit_root_dir, "grit/testdata/resource_ids"),
153 {})
154 messages_node = root.children[0].children[0]
155 self.assertTrue(isinstance(messages_node, empty.MessagesNode))
156 self.assertEqual('100', messages_node.attrs["first_id"])
157 messages_node = root.children[0].children[1]
158 self.assertTrue(isinstance(messages_node, empty.MessagesNode))
159 self.assertEqual('10000', messages_node.attrs["first_id"])
160
161 def testUseNameForIdAndPpIfdef(self):
162 input = u'''<?xml version="1.0" encoding="UTF-8"?>
163 <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_ dir=".">
164 <release seq="3">
165 <messages>
166 <if expr="pp_ifdef('hello')">
167 <message name="IDS_HELLO" use_name_for_id="true">
168 Hello!
169 </message>
170 </if>
171 </messages>
172 </release>
173 </grit>'''
174 pseudo_file = StringIO.StringIO(input)
175 root = grd_reader.Parse(pseudo_file, '.', defines={'hello': '1'})
176
177 # Check if the ID is set to the name. In the past, there was a bug
178 # that caused the ID to be a generated number.
179 hello = root.GetNodeById('IDS_HELLO')
180 self.failUnless(hello.GetCliques()[0].GetId() == 'IDS_HELLO')
181
182 if __name__ == '__main__':
183 unittest.main()
OLDNEW
« no previous file with comments | « grit/grd_reader.py ('k') | grit/grit-todo.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698