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.
10 # Empty value to be inserted to |ACTIONS_MOCK|.
13 ONE_OWNER
= '<owner>name1@google.com</owner>\n'
15 <owner>name1@google.com</owner>\n
16 <owner>name2@google.com</owner>\n
19 DESCRIPTION
= '<description>Description.</description>\n'
20 TWO_DESCRIPTIONS
= """
21 <description>Description.</description>\n
22 <description>Description2.</description>\n
25 OBSOLETE
= '<obsolete>Not used anymore. Replaced by action2.</obsolete>\n'
26 TWO_OBSOLETE
= '<obsolete>Obsolete1.</obsolete><obsolete>Obsolete2.</obsolete>'
28 COMMENT
= '<!--comment-->'
30 # A format string to mock the input action.xml file.
35 <action name="action1">
36 {owners}{obsolete}{description}
41 NO_OWNER_EXPECTED_XML
= (
43 '<action name="action1">\n'
44 ' <owner>Please list the metric\'s owners. '
45 'Add more owner tags as needed.</owner>\n'
46 ' <description>Description.</description>\n'
51 ONE_OWNER_EXPECTED_XML
= (
53 '<action name="action1">\n'
54 ' <owner>name1@google.com</owner>\n'
55 ' <description>Description.</description>\n'
60 TWO_OWNERS_EXPECTED_XML
= (
62 '<action name="action1">\n'
63 ' <owner>name1@google.com</owner>\n'
64 ' <owner>name2@google.com</owner>\n'
65 ' <description>Description.</description>\n'
70 NO_DESCRIPTION_EXPECTED_XML
= (
72 '<action name="action1">\n'
73 ' <owner>name1@google.com</owner>\n'
74 ' <owner>name2@google.com</owner>\n'
75 ' <description>Please enter the description of the metric.</description>\n'
80 OBSOLETE_EXPECTED_XML
= (
82 '<action name="action1">\n'
83 ' <owner>name1@google.com</owner>\n'
84 ' <owner>name2@google.com</owner>\n'
85 ' <description>Description.</description>\n'
86 ' <obsolete>Not used anymore. Replaced by action2.</obsolete>\n'
91 ADD_ACTION_EXPECTED_XML
= (
93 '<action name="action1">\n'
94 ' <owner>name1@google.com</owner>\n'
95 ' <owner>name2@google.com</owner>\n'
96 ' <description>Description.</description>\n'
98 '<action name="action2">\n'
99 ' <owner>Please list the metric\'s owners.'
100 ' Add more owner tags as needed.</owner>\n'
101 ' <description>Please enter the description of the metric.</description>\n'
106 COMMENT_EXPECTED_XML
= (
109 '<action name="action1">\n'
110 ' <owner>name1@google.com</owner>\n'
111 ' <owner>name2@google.com</owner>\n'
112 ' <description>Description.</description>\n'
118 class ActionXmlTest(unittest
.TestCase
):
120 def _GetProcessedAction(self
, owner
, description
, obsolete
, new_actions
=[],
122 """Forms an actions XML string and returns it after processing.
124 It parses the original XML string, adds new user actions (if there is any),
125 and pretty prints it.
128 owner: the owner tag to be inserted in the original XML string.
129 description: the description tag to be inserted in the original XML
131 obsolete: the obsolete tag to be inserted in the original XML string.
132 new_actions: optional. List of new user actions' names to be inserted.
133 comment: the comment tag to be inserted in the original XML string.
136 An updated and pretty-printed action XML string.
138 # Form the actions.xml mock content based on the input parameters.
139 current_xml
= ACTIONS_XML
.format(owners
=owner
, description
=description
,
140 obsolete
=obsolete
, comment
=comment
)
141 actions
, actions_dict
, comments
= extract_actions
.ParseActionFile(
143 for new_action
in new_actions
:
144 actions
.add(new_action
)
145 return extract_actions
.PrettyPrint(actions
, actions_dict
, comments
)
147 def testNoOwner(self
):
148 xml_result
= self
._GetProcessedAction
(NO_VALUE
, DESCRIPTION
, NO_VALUE
)
149 self
.assertEqual(NO_OWNER_EXPECTED_XML
, xml_result
)
151 def testOneOwnerOneDescription(self
):
152 xml_result
= self
._GetProcessedAction
(ONE_OWNER
, DESCRIPTION
, NO_VALUE
)
153 self
.assertEqual(ONE_OWNER_EXPECTED_XML
, xml_result
)
155 def testTwoOwners(self
):
156 xml_result
= self
._GetProcessedAction
(TWO_OWNERS
, DESCRIPTION
, NO_VALUE
)
157 self
.assertEqual(TWO_OWNERS_EXPECTED_XML
, xml_result
)
159 def testNoDescription(self
):
160 xml_result
= self
._GetProcessedAction
(TWO_OWNERS
, NO_VALUE
, NO_VALUE
)
161 self
.assertEqual(NO_DESCRIPTION_EXPECTED_XML
, xml_result
)
163 def testTwoDescriptions(self
):
164 current_xml
= ACTIONS_XML
.format(owners
=TWO_OWNERS
, obsolete
=NO_VALUE
,
165 description
=TWO_DESCRIPTIONS
,
167 # Since there are two description tags, the function ParseActionFile will
168 # raise SystemExit with exit code 1.
169 with self
.assertRaises(SystemExit) as cm
:
170 _
, _
= extract_actions
.ParseActionFile(current_xml
)
171 self
.assertEqual(cm
.exception
.code
, 1)
173 def testObsolete(self
):
174 xml_result
= self
._GetProcessedAction
(TWO_OWNERS
, DESCRIPTION
, OBSOLETE
)
175 self
.assertEqual(OBSOLETE_EXPECTED_XML
, xml_result
)
178 def testTwoObsoletes(self
):
179 current_xml
= ACTIONS_XML
.format(owners
=TWO_OWNERS
, obsolete
=TWO_OBSOLETE
,
180 description
=DESCRIPTION
, comment
=NO_VALUE
)
181 # Since there are two obsolete tags, the function ParseActionFile will
182 # raise SystemExit with exit code 1.
183 with self
.assertRaises(SystemExit) as cm
:
184 _
, _
= extract_actions
.ParseActionFile(current_xml
)
185 self
.assertEqual(cm
.exception
.code
, 1)
187 def testAddNewActions(self
):
188 xml_result
= self
._GetProcessedAction
(TWO_OWNERS
, DESCRIPTION
, NO_VALUE
,
189 new_actions
=['action2'])
190 self
.assertEqual(ADD_ACTION_EXPECTED_XML
, xml_result
)
192 def testComment(self
):
193 xml_result
= self
._GetProcessedAction
(TWO_OWNERS
, DESCRIPTION
, NO_VALUE
,
195 self
.assertEqual(COMMENT_EXPECTED_XML
, xml_result
)
198 if __name__
== '__main__':