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 | |
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 ACTIONS_XML = """ | |
Alexei Svitkine (slow)
2014/03/04 15:32:13
This should either have a more meaningful name (e.
yiyaoliu
2014/03/04 18:02:54
Done.
| |
32 {comment} | |
33 <actions> | |
34 | |
35 <action name="action1">\n{owners}{obsolete}{description} | |
Ilya Sherman
2014/03/03 23:17:34
nit: Please leave a blank line after the \n, unles
yiyaoliu
2014/03/04 15:31:07
Done.
| |
36 </action> | |
37 | |
38 </actions>""" | |
39 | |
40 NO_OWNER_EXPECTED_XML = ( | |
41 '<actions>\n\n' | |
42 '<action name="action1">\n' | |
43 ' <owner>Please list the metric\'s owners.' | |
44 ' Add more owner tags as needed.</owner>\n' | |
Ilya Sherman
2014/03/03 23:17:34
nit: Odd indentation on this line.
yiyaoliu
2014/03/04 15:31:07
I put the blank up to the line above. Is this what
| |
45 ' <description>Description.</description>\n' | |
46 '</action>\n\n' | |
47 '</actions>\n' | |
48 ) | |
49 | |
50 ONE_OWNER_EXPECTED_XML = ( | |
51 '<actions>\n\n' | |
52 '<action name="action1">\n' | |
53 ' <owner>name1@google.com</owner>\n' | |
54 ' <description>Description.</description>\n' | |
55 '</action>\n\n' | |
56 '</actions>\n' | |
57 ) | |
58 | |
59 TWO_OWNERS_EXPECTED_XML = ( | |
60 '<actions>\n\n' | |
61 '<action name="action1">\n' | |
62 ' <owner>name1@google.com</owner>\n' | |
63 ' <owner>name2@google.com</owner>\n' | |
64 ' <description>Description.</description>\n' | |
65 '</action>\n\n' | |
66 '</actions>\n' | |
67 ) | |
68 | |
69 NO_DESCRIPTION_EXPECTED_XML = ( | |
70 '<actions>\n\n' | |
71 '<action name="action1">\n' | |
72 ' <owner>name1@google.com</owner>\n' | |
73 ' <owner>name2@google.com</owner>\n' | |
74 ' <description>Please enter the description of the metric.</description>\n' | |
75 '</action>\n\n' | |
76 '</actions>\n' | |
77 ) | |
78 | |
79 OBSOLETE_EXPECTED_XML = ( | |
80 '<actions>\n\n' | |
81 '<action name="action1">\n' | |
82 ' <owner>name1@google.com</owner>\n' | |
83 ' <owner>name2@google.com</owner>\n' | |
84 ' <description>Description.</description>\n' | |
85 ' <obsolete>Not used anymore. Replaced by action2.</obsolete>\n' | |
86 '</action>\n\n' | |
87 '</actions>\n' | |
88 ) | |
89 | |
90 ADD_ACTION_EXPECTED_XML = ( | |
91 '<actions>\n\n' | |
92 '<action name="action1">\n' | |
93 ' <owner>name1@google.com</owner>\n' | |
94 ' <owner>name2@google.com</owner>\n' | |
95 ' <description>Description.</description>\n' | |
96 '</action>\n\n' | |
97 '<action name="action2">\n' | |
98 ' <owner>Please list the metric\'s owners.' | |
99 ' Add more owner tags as needed.</owner>\n' | |
100 ' <description>Please enter the description of the metric.</description>\n' | |
101 '</action>\n\n' | |
102 '</actions>\n' | |
103 ) | |
104 | |
105 COMMENT_EXPECTED_XML = ( | |
106 '<!--comment-->\n\n' | |
107 '<actions>\n\n' | |
108 '<action name="action1">\n' | |
109 ' <owner>name1@google.com</owner>\n' | |
110 ' <owner>name2@google.com</owner>\n' | |
111 ' <description>Description.</description>\n' | |
112 '</action>\n\n' | |
113 '</actions>\n' | |
114 ) | |
115 | |
116 | |
117 class ActionXmlTest(unittest.TestCase): | |
118 | |
119 def _assertActionResult(self, owner, description, obsolete, expected_result, | |
Alexei Svitkine (slow)
2014/03/04 15:32:13
Please add some comments for this method to explai
yiyaoliu
2014/03/04 18:02:54
Done.
| |
120 new_action=None, comment=NO_VALUE): | |
121 current_xml = ACTIONS_XML.format(owners=owner, description=description, | |
122 obsolete=obsolete, comment=comment) | |
123 with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: | |
Alexei Svitkine (slow)
2014/03/04 15:32:13
How hard would it be to expose a extract_actions.P
yiyaoliu
2014/03/04 18:02:54
It's actually better to pass in a string. Changed
| |
124 f.write(current_xml) | |
125 file_name = f.name | |
126 actions, actions_dict, comments = extract_actions.ParseActionFile(file_name) | |
127 if new_action: | |
128 actions.add(new_action) | |
129 pretty = extract_actions.PrettyPrint(actions, actions_dict, comments) | |
130 self.assertEqual(expected_result, pretty) | |
Alexei Svitkine (slow)
2014/03/04 15:32:13
I think it's clearer to have this assert line be i
yiyaoliu
2014/03/04 18:02:54
Done.
| |
131 | |
132 def testNoOwner(self): | |
133 self._assertActionResult(NO_VALUE, DESCRIPTION, NO_VALUE, | |
134 NO_OWNER_EXPECTED_XML) | |
135 | |
136 def testOneOwnerOneDescription(self): | |
137 self._assertActionResult(ONE_OWNER, DESCRIPTION, NO_VALUE, | |
138 ONE_OWNER_EXPECTED_XML) | |
139 | |
140 def testTwoOwners(self): | |
141 self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE, | |
142 TWO_OWNERS_EXPECTED_XML) | |
143 | |
144 def testNoDescription(self): | |
145 self._assertActionResult(TWO_OWNERS, NO_VALUE, NO_VALUE, | |
146 NO_DESCRIPTION_EXPECTED_XML) | |
147 | |
148 def testTwoDescriptions(self): | |
149 current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=NO_VALUE, | |
150 description=TWO_DESCRIPTIONS, | |
151 comment=NO_VALUE) | |
152 with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: | |
153 f.write(current_xml) | |
154 file_name = f.name | |
155 with self.assertRaises(SystemExit) as cm: | |
156 _, _ = extract_actions.ParseActionFile(file_name) | |
157 self.assertEqual(cm.exception.code, 1) | |
158 | |
159 def testObsolete(self): | |
160 self._assertActionResult(TWO_OWNERS, DESCRIPTION, OBSOLETE, | |
161 OBSOLETE_EXPECTED_XML) | |
162 | |
163 def testTwoObsoletes(self): | |
164 current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=TWO_OBSOLETE, | |
165 description=DESCRIPTION, | |
166 comment=NO_VALUE) | |
167 with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: | |
Alexei Svitkine (slow)
2014/03/04 15:32:13
Seems this logic is common between several places.
| |
168 f.write(current_xml) | |
169 file_name = f.name | |
170 with self.assertRaises(SystemExit) as cm: | |
Alexei Svitkine (slow)
2014/03/04 15:32:13
Can you add a comment explaining what this is doin
| |
171 _, _ = extract_actions.ParseActionFile(file_name) | |
172 self.assertEqual(cm.exception.code, 1) | |
173 | |
174 def testAddNewActions(self): | |
175 self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE, | |
176 ADD_ACTION_EXPECTED_XML, new_action='action2') | |
177 | |
178 def testComment(self): | |
179 self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE, | |
180 COMMENT_EXPECTED_XML, comment=COMMENT) | |
181 | |
182 | |
183 if __name__ == '__main__': | |
184 unittest.main() | |
OLD | NEW |