| 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..7c8f0592f61fdd87751f8e83c1c8d7792291ea58
|
| --- /dev/null
|
| +++ b/tools/metrics/actions/extract_actions_test.py
|
| @@ -0,0 +1,164 @@
|
| +#!/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
|
| +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>'
|
| +
|
| +ACTIONS_XML = """
|
| +<actions>
|
| +
|
| +<action name="action1">\n{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'
|
| +)
|
| +
|
| +
|
| +class ActionXmlTest(unittest.TestCase):
|
| +
|
| + def _assertActionResult(self, owner, description, obsolete, expected_result,
|
| + new_action=None):
|
| + current_xml = ACTIONS_XML.format(owners=owner, description=description,
|
| + obsolete=obsolete)
|
| + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
|
| + f.write(current_xml)
|
| + file_name = f.name
|
| + actions, actions_dict = extract_actions.ParseActionFile(file_name)
|
| + if new_action:
|
| + actions.add(new_action)
|
| + pretty = extract_actions.PrettyPrint(actions, actions_dict)
|
| + self.assertEqual(expected_result, pretty)
|
| +
|
| + def testNoOwner(self):
|
| + self._assertActionResult(NO_VALUE, DESCRIPTION, NO_VALUE,
|
| + NO_OWNER_EXPECTED_XML)
|
| +
|
| + def testOneOwnerOneDescription(self):
|
| + self._assertActionResult(ONE_OWNER, DESCRIPTION, NO_VALUE,
|
| + ONE_OWNER_EXPECTED_XML)
|
| +
|
| + def testTwoOwners(self):
|
| + self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE,
|
| + TWO_OWNERS_EXPECTED_XML)
|
| +
|
| + def testNoDescription(self):
|
| + self._assertActionResult(TWO_OWNERS, NO_VALUE, NO_VALUE,
|
| + NO_DESCRIPTION_EXPECTED_XML)
|
| +
|
| + def testTwoDescriptions(self):
|
| + current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=NO_VALUE,
|
| + description=TWO_DESCRIPTIONS)
|
| + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
|
| + f.write(current_xml)
|
| + file_name = f.name
|
| + with self.assertRaises(SystemExit) as cm:
|
| + _, _ = extract_actions.ParseActionFile(file_name)
|
| + self.assertEqual(cm.exception.code, 1)
|
| +
|
| + def testObsolete(self):
|
| + self._assertActionResult(TWO_OWNERS, DESCRIPTION, OBSOLETE,
|
| + OBSOLETE_EXPECTED_XML)
|
| +
|
| + def testTwoObsoletes(self):
|
| + current_xml = ACTIONS_XML.format(owners=TWO_OWNERS, obsolete=TWO_OBSOLETE,
|
| + description=DESCRIPTION)
|
| + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
|
| + f.write(current_xml)
|
| + file_name = f.name
|
| + with self.assertRaises(SystemExit) as cm:
|
| + _, _ = extract_actions.ParseActionFile(file_name)
|
| + self.assertEqual(cm.exception.code, 1)
|
| +
|
| + def testAddNewActions(self):
|
| + self._assertActionResult(TWO_OWNERS, DESCRIPTION, NO_VALUE,
|
| + ADD_ACTION_EXPECTED_XML, new_action='action2')
|
| +
|
| +
|
| +if __name__ == '__main__':
|
| + unittest.main()
|
|
|