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

Side by Side Diff: grit/node/base_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/node/base.py ('k') | grit/node/custom/__init__.py » ('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) 2006-2008 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 base.Node functionality (as used in various subclasses)'''
7
8
9 import os
10 import sys
11 if __name__ == '__main__':
12 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
13
14 import unittest
15
16 from grit.node import base
17 from grit.node import message
18 from grit.node import structure
19 from grit.node import variant
20
21 def MakePlaceholder(phname='BINGO'):
22 ph = message.PhNode()
23 ph.StartParsing(u'ph', None)
24 ph.HandleAttribute(u'name', phname)
25 ph.AppendContent(u'bongo')
26 ph.EndParsing()
27 return ph
28
29
30 class NodeUnittest(unittest.TestCase):
31 def testWhitespaceHandling(self):
32 # We test using the Message node type.
33 node = message.MessageNode()
34 node.StartParsing(u'hello', None)
35 node.HandleAttribute(u'name', u'bla')
36 node.AppendContent(u" ''' two spaces ")
37 node.EndParsing()
38 self.failUnless(node.GetCdata() == u' two spaces')
39
40 node = message.MessageNode()
41 node.StartParsing(u'message', None)
42 node.HandleAttribute(u'name', u'bla')
43 node.AppendContent(u" two spaces ''' ")
44 node.EndParsing()
45 self.failUnless(node.GetCdata() == u'two spaces ')
46
47 def testWhitespaceHandlingWithChildren(self):
48 # We test using the Message node type.
49 node = message.MessageNode()
50 node.StartParsing(u'message', None)
51 node.HandleAttribute(u'name', u'bla')
52 node.AppendContent(u" ''' two spaces ")
53 node.AddChild(MakePlaceholder())
54 node.AppendContent(u' space before and after ')
55 node.AddChild(MakePlaceholder('BONGO'))
56 node.AppendContent(u" space before two after '''")
57 node.EndParsing()
58 self.failUnless(node.mixed_content[0] == u' two spaces ')
59 self.failUnless(node.mixed_content[2] == u' space before and after ')
60 self.failUnless(node.mixed_content[-1] == u' space before two after ')
61
62 def testXmlFormatMixedContent(self):
63 # Again test using the Message node type, because it is the only mixed
64 # content node.
65 node = message.MessageNode()
66 node.StartParsing(u'message', None)
67 node.HandleAttribute(u'name', u'name')
68 node.AppendContent(u'Hello <young> ')
69
70 ph = message.PhNode()
71 ph.StartParsing(u'ph', None)
72 ph.HandleAttribute(u'name', u'USERNAME')
73 ph.AppendContent(u'$1')
74 ex = message.ExNode()
75 ex.StartParsing(u'ex', None)
76 ex.AppendContent(u'Joi')
77 ex.EndParsing()
78 ph.AddChild(ex)
79 ph.EndParsing()
80
81 node.AddChild(ph)
82 node.EndParsing()
83
84 non_indented_xml = node.Format(node)
85 self.failUnless(non_indented_xml == u'<message name="name">\n Hello '
86 u'&lt;young&gt; <ph name="USERNAME">$1<ex>Joi</ex></ph>'
87 u'\n</message>')
88
89 indented_xml = node.FormatXml(u' ')
90 self.failUnless(indented_xml == u' <message name="name">\n Hello '
91 u'&lt;young&gt; <ph name="USERNAME">$1<ex>Joi</ex></ph>'
92 u'\n </message>')
93
94 def testXmlFormatMixedContentWithLeadingWhitespace(self):
95 # Again test using the Message node type, because it is the only mixed
96 # content node.
97 node = message.MessageNode()
98 node.StartParsing(u'message', None)
99 node.HandleAttribute(u'name', u'name')
100 node.AppendContent(u"''' Hello <young> ")
101
102 ph = message.PhNode()
103 ph.StartParsing(u'ph', None)
104 ph.HandleAttribute(u'name', u'USERNAME')
105 ph.AppendContent(u'$1')
106 ex = message.ExNode()
107 ex.StartParsing(u'ex', None)
108 ex.AppendContent(u'Joi')
109 ex.EndParsing()
110 ph.AddChild(ex)
111 ph.EndParsing()
112
113 node.AddChild(ph)
114 node.AppendContent(u" yessiree '''")
115 node.EndParsing()
116
117 non_indented_xml = node.Format(node)
118 self.failUnless(non_indented_xml ==
119 u"<message name=\"name\">\n ''' Hello"
120 u' &lt;young&gt; <ph name="USERNAME">$1<ex>Joi</ex></ph>'
121 u" yessiree '''\n</message>")
122
123 indented_xml = node.FormatXml(u' ')
124 self.failUnless(indented_xml ==
125 u" <message name=\"name\">\n ''' Hello"
126 u' &lt;young&gt; <ph name="USERNAME">$1<ex>Joi</ex></ph>'
127 u" yessiree '''\n </message>")
128
129 self.failUnless(node.GetNodeById('name'))
130
131 def testXmlFormatContentWithEntities(self):
132 '''Tests a bug where &nbsp; would not be escaped correctly.'''
133 from grit import tclib
134 msg_node = message.MessageNode.Construct(None, tclib.Message(
135 text = 'BEGIN_BOLDHelloWHITESPACEthere!END_BOLD Bingo!',
136 placeholders = [
137 tclib.Placeholder('BEGIN_BOLD', '<b>', 'bla'),
138 tclib.Placeholder('WHITESPACE', '&nbsp;', 'bla'),
139 tclib.Placeholder('END_BOLD', '</b>', 'bla')]),
140 'BINGOBONGO')
141 xml = msg_node.FormatXml()
142 self.failUnless(xml.find('&nbsp;') == -1, 'should have no entities')
143
144 def testIter(self):
145 # First build a little tree of message and ph nodes.
146 node = message.MessageNode()
147 node.StartParsing(u'message', None)
148 node.HandleAttribute(u'name', u'bla')
149 node.AppendContent(u" ''' two spaces ")
150 node.AppendContent(u' space before and after ')
151 ph = message.PhNode()
152 ph.StartParsing(u'ph', None)
153 ph.AddChild(message.ExNode())
154 ph.HandleAttribute(u'name', u'BINGO')
155 ph.AppendContent(u'bongo')
156 node.AddChild(ph)
157 node.AddChild(message.PhNode())
158 node.AppendContent(u" space before two after '''")
159
160 order = [message.MessageNode, message.PhNode, message.ExNode, message.PhNode ]
161 for n in node:
162 self.failUnless(type(n) == order[0])
163 order = order[1:]
164 self.failUnless(len(order) == 0)
165
166
167 if __name__ == '__main__':
168 unittest.main()
169
OLDNEW
« no previous file with comments | « grit/node/base.py ('k') | grit/node/custom/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698