Index: tools/metrics/actions/extract_actions_test.py |
diff --git a/tools/metrics/actions/extract_actions_test.py b/tools/metrics/actions/extract_actions_test.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..d0ef7fbb5cd3f81df532c83663896a9ea55e8b62 |
--- /dev/null |
+++ b/tools/metrics/actions/extract_actions_test.py |
@@ -0,0 +1,201 @@ |
+#!/usr/bin/env python |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import tempfile |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Do you still need this?
yiyaoliu
2014/03/04 18:53:28
Done.
|
+import unittest |
+ |
+import extract_actions |
+ |
+# Empty value to be inserted to |ACTIONS_MOCK|. |
+NO_VALUE = '' |
+ |
+ONE_OWNER = '<owner>name1@google.com</owner>\n' |
+TWO_OWNERS = """ |
+<owner>name1@google.com</owner>\n |
+<owner>name2@google.com</owner>\n |
+""" |
+ |
+DESCRIPTION = '<description>Description.</description>\n' |
+TWO_DESCRIPTIONS = """ |
+<description>Description.</description>\n |
+<description>Description2.</description>\n |
+""" |
+ |
+OBSOLETE = '<obsolete>Not used anymore. Replaced by action2.</obsolete>\n' |
+TWO_OBSOLETE = '<obsolete>Obsolete1.</obsolete><obsolete>Obsolete2.</obsolete>' |
+ |
+COMMENT = '<!--comment-->' |
+ |
+# A format string to mock the input action.xml file. |
+ACTIONS_XML = """ |
+{comment} |
+<actions> |
+ |
+<action name="action1"> |
+{owners}{obsolete}{description} |
+</action> |
+ |
+</actions>""" |
+ |
+NO_OWNER_EXPECTED_XML = ( |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>Please list the metric\'s owners. ' |
+ 'Add more owner tags as needed.</owner>\n' |
+ ' <description>Description.</description>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+ONE_OWNER_EXPECTED_XML = ( |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>name1@google.com</owner>\n' |
+ ' <description>Description.</description>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+TWO_OWNERS_EXPECTED_XML = ( |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>name1@google.com</owner>\n' |
+ ' <owner>name2@google.com</owner>\n' |
+ ' <description>Description.</description>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+NO_DESCRIPTION_EXPECTED_XML = ( |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>name1@google.com</owner>\n' |
+ ' <owner>name2@google.com</owner>\n' |
+ ' <description>Please enter the description of the metric.</description>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+OBSOLETE_EXPECTED_XML = ( |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>name1@google.com</owner>\n' |
+ ' <owner>name2@google.com</owner>\n' |
+ ' <description>Description.</description>\n' |
+ ' <obsolete>Not used anymore. Replaced by action2.</obsolete>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+ADD_ACTION_EXPECTED_XML = ( |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>name1@google.com</owner>\n' |
+ ' <owner>name2@google.com</owner>\n' |
+ ' <description>Description.</description>\n' |
+ '</action>\n\n' |
+ '<action name="action2">\n' |
+ ' <owner>Please list the metric\'s owners.' |
+ ' Add more owner tags as needed.</owner>\n' |
+ ' <description>Please enter the description of the metric.</description>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+COMMENT_EXPECTED_XML = ( |
+ '<!--comment-->\n\n' |
+ '<actions>\n\n' |
+ '<action name="action1">\n' |
+ ' <owner>name1@google.com</owner>\n' |
+ ' <owner>name2@google.com</owner>\n' |
+ ' <description>Description.</description>\n' |
+ '</action>\n\n' |
+ '</actions>\n' |
+) |
+ |
+ |
+class ActionXmlTest(unittest.TestCase): |
+ |
+ 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.
|
+ comment=NO_VALUE): |
+ """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.
|
+ |
+ 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.
|
+ and pretty print it. |
+ |
+ Args: |
+ owner: the owner tag to be inserted in the original XML string. |
+ description: the description tag to be inserted in the original XML |
+ string. |
+ obsolete: the obsolete tag to be inserted in the original XML string. |
+ 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.
|
+ comment: the comment tag to be inserted in the original XML string. |
+ |
+ Returns: |
+ An updated and pretty-printed action XML string. |
+ """ |
+ # Form the actions.xml mock content based on the input parameters. |
+ current_xml = ACTIONS_XML.format(owners=owner, description=description, |
+ obsolete=obsolete, comment=comment) |
+ actions, actions_dict, comments = extract_actions.ParseActionFile( |
+ current_xml) |
+ if new_action: |
+ actions.add(new_action) |
+ return extract_actions.PrettyPrint(actions, actions_dict, comments) |
+ self.assertEqual(expected_result, pretty) |
Alexei Svitkine (slow)
2014/03/04 18:40:22
Delete line.
yiyaoliu
2014/03/04 18:53:28
Done.
|
+ |
+ def testNoOwner(self): |
+ xml_result = self._getProcessedAction(NO_VALUE, DESCRIPTION, NO_VALUE) |
+ self.assertEqual(NO_OWNER_EXPECTED_XML, xml_result) |
+ |
+ def testOneOwnerOneDescription(self): |
+ xml_result = self._getProcessedAction(ONE_OWNER, DESCRIPTION, NO_VALUE) |
+ self.assertEqual(ONE_OWNER_EXPECTED_XML, xml_result) |
+ |
+ def testTwoOwners(self): |
+ xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, NO_VALUE) |
+ self.assertEqual(TWO_OWNERS_EXPECTED_XML, xml_result) |
+ |
+ def testNoDescription(self): |
+ xml_result = self._getProcessedAction(TWO_OWNERS, NO_VALUE, NO_VALUE) |
+ self.assertEqual(NO_DESCRIPTION_EXPECTED_XML, xml_result) |
+ |
+ def testTwoDescriptions(self): |
+ current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=NO_VALUE, |
+ description=TWO_DESCRIPTIONS, |
+ comment=NO_VALUE) |
+ # Since there are two description tags, the function ParseActionFile will |
+ # raise SystemExit with exit code 1. |
+ with self.assertRaises(SystemExit) as cm: |
+ _, _ = extract_actions.ParseActionFile(current_xml) |
+ self.assertEqual(cm.exception.code, 1) |
+ |
+ def testObsolete(self): |
+ xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, OBSOLETE) |
+ self.assertEqual(OBSOLETE_EXPECTED_XML, xml_result) |
+ |
+ |
+ def testTwoObsoletes(self): |
+ current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=TWO_OBSOLETE, |
+ description=DESCRIPTION, comment=NO_VALUE) |
+ # Since there are two obsolete tags, the function ParseActionFile will |
+ # raise SystemExit with exit code 1. |
+ with self.assertRaises(SystemExit) as cm: |
+ _, _ = extract_actions.ParseActionFile(current_xml) |
+ self.assertEqual(cm.exception.code, 1) |
+ |
+ def testAddNewActions(self): |
+ xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, NO_VALUE, |
+ new_action='action2') |
+ self.assertEqual(ADD_ACTION_EXPECTED_XML, xml_result) |
+ |
+ def testComment(self): |
+ xml_result = self._getProcessedAction(TWO_OWNERS, DESCRIPTION, NO_VALUE, |
+ comment=COMMENT) |
+ self.assertEqual(COMMENT_EXPECTED_XML, xml_result) |
+ |
+ |
+if __name__ == '__main__': |
+ unittest.main() |