OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2014 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 import tempfile | |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Do you still need this?
yiyaoliu
2014/03/04 18:53:28
Done.
| |
7 import unittest | |
8 | |
9 import extract_actions | |
10 | |
11 # Empty value to be inserted to |ACTIONS_MOCK|. | |
12 NO_VALUE = '' | |
13 | |
14 ONE_OWNER = '<owner>name1@google.com</owner>\n' | |
15 TWO_OWNERS = """ | |
16 <owner>name1@google.com</owner>\n | |
17 <owner>name2@google.com</owner>\n | |
18 """ | |
19 | |
20 DESCRIPTION = '<description>Description.</description>\n' | |
21 TWO_DESCRIPTIONS = """ | |
22 <description>Description.</description>\n | |
23 <description>Description2.</description>\n | |
24 """ | |
25 | |
26 OBSOLETE = '<obsolete>Not used anymore. Replaced by action2.</obsolete>\n' | |
27 TWO_OBSOLETE = '<obsolete>Obsolete1.</obsolete><obsolete>Obsolete2.</obsolete>' | |
28 | |
29 COMMENT = '<!--comment-->' | |
30 | |
31 # A format string to mock the input action.xml file. | |
32 ACTIONS_XML = """ | |
33 {comment} | |
34 <actions> | |
35 | |
36 <action name="action1"> | |
37 {owners}{obsolete}{description} | |
38 </action> | |
39 | |
40 </actions>""" | |
41 | |
42 NO_OWNER_EXPECTED_XML = ( | |
43 '<actions>\n\n' | |
44 '<action name="action1">\n' | |
45 ' <owner>Please list the metric\'s owners. ' | |
46 'Add more owner tags as needed.</owner>\n' | |
47 ' <description>Description.</description>\n' | |
48 '</action>\n\n' | |
49 '</actions>\n' | |
50 ) | |
51 | |
52 ONE_OWNER_EXPECTED_XML = ( | |
53 '<actions>\n\n' | |
54 '<action name="action1">\n' | |
55 ' <owner>name1@google.com</owner>\n' | |
56 ' <description>Description.</description>\n' | |
57 '</action>\n\n' | |
58 '</actions>\n' | |
59 ) | |
60 | |
61 TWO_OWNERS_EXPECTED_XML = ( | |
62 '<actions>\n\n' | |
63 '<action name="action1">\n' | |
64 ' <owner>name1@google.com</owner>\n' | |
65 ' <owner>name2@google.com</owner>\n' | |
66 ' <description>Description.</description>\n' | |
67 '</action>\n\n' | |
68 '</actions>\n' | |
69 ) | |
70 | |
71 NO_DESCRIPTION_EXPECTED_XML = ( | |
72 '<actions>\n\n' | |
73 '<action name="action1">\n' | |
74 ' <owner>name1@google.com</owner>\n' | |
75 ' <owner>name2@google.com</owner>\n' | |
76 ' <description>Please enter the description of the metric.</description>\n' | |
77 '</action>\n\n' | |
78 '</actions>\n' | |
79 ) | |
80 | |
81 OBSOLETE_EXPECTED_XML = ( | |
82 '<actions>\n\n' | |
83 '<action name="action1">\n' | |
84 ' <owner>name1@google.com</owner>\n' | |
85 ' <owner>name2@google.com</owner>\n' | |
86 ' <description>Description.</description>\n' | |
87 ' <obsolete>Not used anymore. Replaced by action2.</obsolete>\n' | |
88 '</action>\n\n' | |
89 '</actions>\n' | |
90 ) | |
91 | |
92 ADD_ACTION_EXPECTED_XML = ( | |
93 '<actions>\n\n' | |
94 '<action name="action1">\n' | |
95 ' <owner>name1@google.com</owner>\n' | |
96 ' <owner>name2@google.com</owner>\n' | |
97 ' <description>Description.</description>\n' | |
98 '</action>\n\n' | |
99 '<action name="action2">\n' | |
100 ' <owner>Please list the metric\'s owners.' | |
101 ' Add more owner tags as needed.</owner>\n' | |
102 ' <description>Please enter the description of the metric.</description>\n' | |
103 '</action>\n\n' | |
104 '</actions>\n' | |
105 ) | |
106 | |
107 COMMENT_EXPECTED_XML = ( | |
108 '<!--comment-->\n\n' | |
109 '<actions>\n\n' | |
110 '<action name="action1">\n' | |
111 ' <owner>name1@google.com</owner>\n' | |
112 ' <owner>name2@google.com</owner>\n' | |
113 ' <description>Description.</description>\n' | |
114 '</action>\n\n' | |
115 '</actions>\n' | |
116 ) | |
117 | |
118 | |
119 class ActionXmlTest(unittest.TestCase): | |
120 | |
121 def _getProcessedAction(self, owner, description, obsolete, new_action=None, | |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Nit: capitalize, I think.
yiyaoliu
2014/03/04 18:53:28
Done.
| |
122 comment=NO_VALUE): | |
123 """Form an actions XML string and returns it after processing. | |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Nit: "Form" -> "Forms"
yiyaoliu
2014/03/04 18:53:28
Done.
| |
124 | |
125 It parses the original XML string, add new user actions (if there is any), | |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Nit: "add" -> "adds" and "pretty print" -> "pretty
yiyaoliu
2014/03/04 18:53:28
Done.
| |
126 and pretty print it. | |
127 | |
128 Args: | |
129 owner: the owner tag to be inserted in the original XML string. | |
130 description: the description tag to be inserted in the original XML | |
131 string. | |
132 obsolete: the obsolete tag to be inserted in the original XML string. | |
133 new_action: optional. List of new user actions' names to be inserted. | |
Alexei Svitkine (slow)
2014/03/04 18:40:22
If it's a list, use plural (new_actions)
yiyaoliu
2014/03/04 18:53:28
Done.
| |
134 comment: the comment tag to be inserted in the original XML string. | |
135 | |
136 Returns: | |
137 An updated and pretty-printed action XML string. | |
138 """ | |
139 # Form the actions.xml mock content based on the input parameters. | |
140 current_xml = ACTIONS_XML.format(owners=owner, description=description, | |
141 obsolete=obsolete, comment=comment) | |
142 actions, actions_dict, comments = extract_actions.ParseActionFile( | |
143 current_xml) | |
144 if new_action: | |
145 actions.add(new_action) | |
146 return extract_actions.PrettyPrint(actions, actions_dict, comments) | |
147 self.assertEqual(expected_result, pretty) | |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Delete line.
yiyaoliu
2014/03/04 18:53:28
Done.
| |
148 | |
149 def testNoOwner(self): | |
150 xml_result = self._getProcessedAction(NO_VALUE, DESCRIPTION, NO_VALUE) | |
151 self.assertEqual(NO_OWNER_EXPECTED_XML, xml_result) | |
152 | |
153 def testOneOwnerOneDescription(self): | |
154 xml_result = self._getProcessedAction(ONE_OWNER, DESCRIPTION, NO_VALUE) | |
155 self.assertEqual(ONE_OWNER_EXPECTED_XML, xml_result) | |
156 | |
157 def testTwoOwners(self): | |
158 xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, NO_VALUE) | |
159 self.assertEqual(TWO_OWNERS_EXPECTED_XML, xml_result) | |
160 | |
161 def testNoDescription(self): | |
162 xml_result = self._getProcessedAction(TWO_OWNERS, NO_VALUE, NO_VALUE) | |
163 self.assertEqual(NO_DESCRIPTION_EXPECTED_XML, xml_result) | |
164 | |
165 def testTwoDescriptions(self): | |
166 current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=NO_VALUE, | |
167 description=TWO_DESCRIPTIONS, | |
168 comment=NO_VALUE) | |
169 # Since there are two description tags, the function ParseActionFile will | |
170 # raise SystemExit with exit code 1. | |
171 with self.assertRaises(SystemExit) as cm: | |
172 _, _ = extract_actions.ParseActionFile(current_xml) | |
173 self.assertEqual(cm.exception.code, 1) | |
174 | |
175 def testObsolete(self): | |
176 xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, OBSOLETE) | |
177 self.assertEqual(OBSOLETE_EXPECTED_XML, xml_result) | |
178 | |
179 | |
180 def testTwoObsoletes(self): | |
181 current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=TWO_OBSOLETE, | |
182 description=DESCRIPTION, comment=NO_VALUE) | |
183 # Since there are two obsolete tags, the function ParseActionFile will | |
184 # raise SystemExit with exit code 1. | |
185 with self.assertRaises(SystemExit) as cm: | |
186 _, _ = extract_actions.ParseActionFile(current_xml) | |
187 self.assertEqual(cm.exception.code, 1) | |
188 | |
189 def testAddNewActions(self): | |
190 xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, NO_VALUE, | |
191 new_action='action2') | |
192 self.assertEqual(ADD_ACTION_EXPECTED_XML, xml_result) | |
193 | |
194 def testComment(self): | |
195 xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, NO_VALUE, | |
196 comment=COMMENT) | |
197 self.assertEqual(COMMENT_EXPECTED_XML, xml_result) | |
198 | |
199 | |
200 if __name__ == '__main__': | |
201 unittest.main() | |
OLD | NEW |