aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/py/gflags/tests
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/py/gflags/tests')
-rw-r--r--third_party/py/gflags/tests/flags_modules_for_testing/__init__.py0
-rwxr-xr-xthird_party/py/gflags/tests/flags_modules_for_testing/module_bar.py135
-rwxr-xr-xthird_party/py/gflags/tests/flags_modules_for_testing/module_baz.py45
-rwxr-xr-xthird_party/py/gflags/tests/flags_modules_for_testing/module_foo.py141
-rw-r--r--third_party/py/gflags/tests/gflags_googletest.py119
-rwxr-xr-xthird_party/py/gflags/tests/gflags_helpxml_test.py535
-rwxr-xr-xthird_party/py/gflags/tests/gflags_unittest.py1949
-rwxr-xr-xthird_party/py/gflags/tests/gflags_validators_test.py220
8 files changed, 0 insertions, 3144 deletions
diff --git a/third_party/py/gflags/tests/flags_modules_for_testing/__init__.py b/third_party/py/gflags/tests/flags_modules_for_testing/__init__.py
deleted file mode 100644
index e69de29bb2..0000000000
--- a/third_party/py/gflags/tests/flags_modules_for_testing/__init__.py
+++ /dev/null
diff --git a/third_party/py/gflags/tests/flags_modules_for_testing/module_bar.py b/third_party/py/gflags/tests/flags_modules_for_testing/module_bar.py
deleted file mode 100755
index 230627f23a..0000000000
--- a/third_party/py/gflags/tests/flags_modules_for_testing/module_bar.py
+++ /dev/null
@@ -1,135 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (c) 2009, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-
-"""Auxiliary module for testing gflags.py.
-
-The purpose of this module is to define a few flags. We want to make
-sure the unit tests for gflags.py involve more than one module.
-"""
-
-__author__ = 'salcianu@google.com (Alex Salcianu)'
-
-__pychecker__ = 'no-local' # for unittest
-
-import gflags
-
-FLAGS = gflags.FLAGS
-
-
-def DefineFlags(flag_values=FLAGS):
- """Defines some flags.
-
- Args:
- flag_values: The FlagValues object we want to register the flags
- with.
- """
- # The 'tmod_bar_' prefix (short for 'test_module_bar') ensures there
- # is no name clash with the existing flags.
- gflags.DEFINE_boolean('tmod_bar_x', True, 'Boolean flag.',
- flag_values=flag_values)
- gflags.DEFINE_string('tmod_bar_y', 'default', 'String flag.',
- flag_values=flag_values)
- gflags.DEFINE_boolean('tmod_bar_z', False,
- 'Another boolean flag from module bar.',
- flag_values=flag_values)
- gflags.DEFINE_integer('tmod_bar_t', 4, 'Sample int flag.',
- flag_values=flag_values)
- gflags.DEFINE_integer('tmod_bar_u', 5, 'Sample int flag.',
- flag_values=flag_values)
- gflags.DEFINE_integer('tmod_bar_v', 6, 'Sample int flag.',
- flag_values=flag_values)
-
-
-def RemoveOneFlag(flag_name, flag_values=FLAGS):
- """Removes the definition of one flag from gflags.FLAGS.
-
- Note: if the flag is not defined in gflags.FLAGS, this function does
- not do anything (in particular, it does not raise any exception).
-
- Motivation: We use this function for cleanup *after* a test: if
- there was a failure during a test and not all flags were declared,
- we do not want the cleanup code to crash.
-
- Args:
- flag_name: A string, the name of the flag to delete.
- flag_values: The FlagValues object we remove the flag from.
- """
- if flag_name in flag_values.FlagDict():
- flag_values.__delattr__(flag_name)
-
-
-def NamesOfDefinedFlags():
- """Returns: List of names of the flags declared in this module."""
- return ['tmod_bar_x',
- 'tmod_bar_y',
- 'tmod_bar_z',
- 'tmod_bar_t',
- 'tmod_bar_u',
- 'tmod_bar_v']
-
-
-def RemoveFlags(flag_values=FLAGS):
- """Deletes the flag definitions done by the above DefineFlags().
-
- Args:
- flag_values: The FlagValues object we remove the flags from.
- """
- for flag_name in NamesOfDefinedFlags():
- RemoveOneFlag(flag_name, flag_values=flag_values)
-
-
-def GetModuleName():
- """Uses gflags._GetCallingModule() to return the name of this module.
-
- For checking that _GetCallingModule works as expected.
-
- Returns:
- A string, the name of this module.
- """
- # Calling the protected _GetCallingModule generates a lint warning,
- # but we do not have any other alternative to test that function.
- return gflags._GetCallingModule()
-
-
-def ExecuteCode(code, global_dict):
- """Executes some code in a given global environment.
-
- For testing of _GetCallingModule.
-
- Args:
- code: A string, the code to be executed.
- global_dict: A dictionary, the global environment that code should
- be executed in.
- """
- # Indeed, using exec generates a lint warning. But some user code
- # actually uses exec, and we have to test for it ...
- exec code in global_dict
diff --git a/third_party/py/gflags/tests/flags_modules_for_testing/module_baz.py b/third_party/py/gflags/tests/flags_modules_for_testing/module_baz.py
deleted file mode 100755
index 2719c950ad..0000000000
--- a/third_party/py/gflags/tests/flags_modules_for_testing/module_baz.py
+++ /dev/null
@@ -1,45 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (c) 2009, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-"""Auxiliary module for testing gflags.py.
-
-The purpose of this module is to test the behavior of flags that are defined
-before main() executes.
-"""
-
-
-
-
-import gflags
-
-FLAGS = gflags.FLAGS
-
-gflags.DEFINE_boolean('tmod_baz_x', True, 'Boolean flag.')
diff --git a/third_party/py/gflags/tests/flags_modules_for_testing/module_foo.py b/third_party/py/gflags/tests/flags_modules_for_testing/module_foo.py
deleted file mode 100755
index 760a37cc7b..0000000000
--- a/third_party/py/gflags/tests/flags_modules_for_testing/module_foo.py
+++ /dev/null
@@ -1,141 +0,0 @@
-#!/usr/bin/env python
-#
-# Copyright (c) 2009, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-"""Auxiliary module for testing gflags.py.
-
-The purpose of this module is to define a few flags, and declare some
-other flags as being important. We want to make sure the unit tests
-for gflags.py involve more than one module.
-"""
-
-__author__ = 'salcianu@google.com (Alex Salcianu)'
-
-__pychecker__ = 'no-local' # for unittest
-
-import gflags
-from flags_modules_for_testing import module_bar
-
-FLAGS = gflags.FLAGS
-
-
-DECLARED_KEY_FLAGS = ['tmod_bar_x', 'tmod_bar_z', 'tmod_bar_t',
- # Special (not user-defined) flag:
- 'flagfile']
-
-
-def DefineFlags(flag_values=FLAGS):
- """Defines a few flags."""
- module_bar.DefineFlags(flag_values=flag_values)
- # The 'tmod_foo_' prefix (short for 'test_module_foo') ensures that we
- # have no name clash with existing flags.
- gflags.DEFINE_boolean('tmod_foo_bool', True, 'Boolean flag from module foo.',
- flag_values=flag_values)
- gflags.DEFINE_string('tmod_foo_str', 'default', 'String flag.',
- flag_values=flag_values)
- gflags.DEFINE_integer('tmod_foo_int', 3, 'Sample int flag.',
- flag_values=flag_values)
-
-
-def DeclareKeyFlags(flag_values=FLAGS):
- """Declares a few key flags."""
- for flag_name in DECLARED_KEY_FLAGS:
- gflags.DECLARE_key_flag(flag_name, flag_values=flag_values)
-
-
-def DeclareExtraKeyFlags(flag_values=FLAGS):
- """Declares some extra key flags."""
- gflags.ADOPT_module_key_flags(module_bar, flag_values=flag_values)
-
-
-def NamesOfDefinedFlags():
- """Returns: list of names of flags defined by this module."""
- return ['tmod_foo_bool', 'tmod_foo_str', 'tmod_foo_int']
-
-
-def NamesOfDeclaredKeyFlags():
- """Returns: list of names of key flags for this module."""
- return NamesOfDefinedFlags() + DECLARED_KEY_FLAGS
-
-
-def NamesOfDeclaredExtraKeyFlags():
- """Returns the list of names of additional key flags for this module.
-
- These are the flags that became key for this module only as a result
- of a call to DeclareExtraKeyFlags() above. I.e., the flags declared
- by module_bar, that were not already declared as key for this
- module.
-
- Returns:
- The list of names of additional key flags for this module.
- """
- names_of_extra_key_flags = list(module_bar.NamesOfDefinedFlags())
- for flag_name in NamesOfDeclaredKeyFlags():
- while flag_name in names_of_extra_key_flags:
- names_of_extra_key_flags.remove(flag_name)
- return names_of_extra_key_flags
-
-
-def RemoveFlags(flag_values=FLAGS):
- """Deletes the flag definitions done by the above DefineFlags()."""
- for flag_name in NamesOfDefinedFlags():
- module_bar.RemoveOneFlag(flag_name, flag_values=flag_values)
- module_bar.RemoveFlags(flag_values=flag_values)
-
-
-def GetModuleName():
- """Uses gflags._GetCallingModule() to return the name of this module.
-
- For checking that _GetCallingModule works as expected.
-
- Returns:
- A string, the name of this module.
- """
- # Calling the protected _GetCallingModule generates a lint warning,
- # but we do not have any other alternative to test that function.
- return gflags._GetCallingModule()
-
-
-def DuplicateFlags(flagnames=None):
- """Returns a new FlagValues object with the requested flagnames.
-
- Used to test DuplicateFlagError detection.
-
- Args:
- flagnames: str, A list of flag names to create.
-
- Returns:
- A FlagValues object with one boolean flag for each name in flagnames.
- """
- flag_values = gflags.FlagValues()
- for name in flagnames:
- gflags.DEFINE_boolean(name, False, 'Flag named %s' % (name,),
- flag_values=flag_values)
- return flag_values
diff --git a/third_party/py/gflags/tests/gflags_googletest.py b/third_party/py/gflags/tests/gflags_googletest.py
deleted file mode 100644
index 9ae614ce80..0000000000
--- a/third_party/py/gflags/tests/gflags_googletest.py
+++ /dev/null
@@ -1,119 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (c) 2011, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-"""Some simple additions to the unittest framework useful for gflags testing."""
-
-
-
-import re
-import unittest
-
-
-def Sorted(lst):
- """Equivalent of sorted(), but not dependent on python version."""
- sorted_list = lst[:]
- sorted_list.sort()
- return sorted_list
-
-
-def MultiLineEqual(expected, actual):
- """Returns True if expected == actual, or returns False and logs."""
- if actual == expected:
- return True
-
- print "Error: FLAGS.MainModuleHelp() didn't return the expected result."
- print "Got:"
- print actual
- print "[End of got]"
-
- actual_lines = actual.split("\n")
- expected_lines = expected.split("\n")
-
- num_actual_lines = len(actual_lines)
- num_expected_lines = len(expected_lines)
-
- if num_actual_lines != num_expected_lines:
- print "Number of actual lines = %d, expected %d" % (
- num_actual_lines, num_expected_lines)
-
- num_to_match = min(num_actual_lines, num_expected_lines)
-
- for i in range(num_to_match):
- if actual_lines[i] != expected_lines[i]:
- print "One discrepancy: Got:"
- print actual_lines[i]
- print "Expected:"
- print expected_lines[i]
- break
- else:
- # If we got here, found no discrepancy, print first new line.
- if num_actual_lines > num_expected_lines:
- print "New help line:"
- print actual_lines[num_expected_lines]
- elif num_expected_lines > num_actual_lines:
- print "Missing expected help line:"
- print expected_lines[num_actual_lines]
- else:
- print "Bug in this test -- discrepancy detected but not found."
-
- return False
-
-
-class TestCase(unittest.TestCase):
- def assertListEqual(self, list1, list2):
- """Asserts that, when sorted, list1 and list2 are identical."""
- # This exists in python 2.7, but not previous versions. Use the
- # built-in version if possible.
- if hasattr(unittest.TestCase, "assertListEqual"):
- unittest.TestCase.assertListEqual(self, Sorted(list1), Sorted(list2))
- else:
- self.assertEqual(Sorted(list1), Sorted(list2))
-
- def assertMultiLineEqual(self, expected, actual):
- # This exists in python 2.7, but not previous versions. Use the
- # built-in version if possible.
- if hasattr(unittest.TestCase, "assertMultiLineEqual"):
- unittest.TestCase.assertMultiLineEqual(self, expected, actual)
- else:
- self.assertTrue(MultiLineEqual(expected, actual))
-
- def assertRaisesWithRegexpMatch(self, exception, regexp, fn, *args, **kwargs):
- try:
- fn(*args, **kwargs)
- except exception, why:
- self.assertTrue(re.search(regexp, str(why)),
- "'%s' does not match '%s'" % (regexp, why))
- return
- self.fail(exception.__name__ + " not raised")
-
-
-def main():
- unittest.main()
diff --git a/third_party/py/gflags/tests/gflags_helpxml_test.py b/third_party/py/gflags/tests/gflags_helpxml_test.py
deleted file mode 100755
index fd78004b73..0000000000
--- a/third_party/py/gflags/tests/gflags_helpxml_test.py
+++ /dev/null
@@ -1,535 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (c) 2009, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-"""Unit tests for the XML-format help generated by the gflags.py module."""
-
-__author__ = 'salcianu@google.com (Alex Salcianu)'
-
-
-import string
-import StringIO
-import sys
-import xml.dom.minidom
-import xml.sax.saxutils
-import gflags_googletest as googletest
-import gflags
-from flags_modules_for_testing import module_bar
-
-
-class _MakeXMLSafeTest(googletest.TestCase):
-
- def _Check(self, s, expected_output):
- self.assertEqual(gflags._MakeXMLSafe(s), expected_output)
-
- def testMakeXMLSafe(self):
- self._Check('plain text', 'plain text')
- self._Check('(x < y) && (a >= b)',
- '(x &lt; y) &amp;&amp; (a &gt;= b)')
- # Some characters with ASCII code < 32 are illegal in XML 1.0 and
- # are removed by us. However, '\n', '\t', and '\r' are legal.
- self._Check('\x09\x0btext \x02 with\x0dsome \x08 good & bad chars',
- '\ttext with\rsome good &amp; bad chars')
-
-
-def _ListSeparatorsInXMLFormat(separators, indent=''):
- """Generates XML encoding of a list of list separators.
-
- Args:
- separators: A list of list separators. Usually, this should be a
- string whose characters are the valid list separators, e.g., ','
- means that both comma (',') and space (' ') are valid list
- separators.
- indent: A string that is added at the beginning of each generated
- XML element.
-
- Returns:
- A string.
- """
- result = ''
- separators = list(separators)
- separators.sort()
- for sep_char in separators:
- result += ('%s<list_separator>%s</list_separator>\n' %
- (indent, repr(sep_char)))
- return result
-
-
-class WriteFlagHelpInXMLFormatTest(googletest.TestCase):
- """Test the XML-format help for a single flag at a time.
-
- There is one test* method for each kind of DEFINE_* declaration.
- """
-
- def setUp(self):
- # self.fv is a FlagValues object, just like gflags.FLAGS. Each
- # test registers one flag with this FlagValues.
- self.fv = gflags.FlagValues()
-
- def _CheckFlagHelpInXML(self, flag_name, module_name,
- expected_output, is_key=False):
- # StringIO.StringIO is a file object that writes into a memory string.
- sio = StringIO.StringIO()
- flag_obj = self.fv[flag_name]
- flag_obj.WriteInfoInXMLFormat(sio, module_name, is_key=is_key, indent=' ')
- self.assertMultiLineEqual(sio.getvalue(), expected_output)
- sio.close()
-
- def testFlagHelpInXML_Int(self):
- gflags.DEFINE_integer('index', 17, 'An integer flag', flag_values=self.fv)
- expected_output_pattern = (
- ' <flag>\n'
- ' <file>module.name</file>\n'
- ' <name>index</name>\n'
- ' <meaning>An integer flag</meaning>\n'
- ' <default>17</default>\n'
- ' <current>%d</current>\n'
- ' <type>int</type>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('index', 'module.name',
- expected_output_pattern % 17)
- # Check that the output is correct even when the current value of
- # a flag is different from the default one.
- self.fv['index'].value = 20
- self._CheckFlagHelpInXML('index', 'module.name',
- expected_output_pattern % 20)
-
- def testFlagHelpInXML_IntWithBounds(self):
- gflags.DEFINE_integer('nb_iters', 17, 'An integer flag',
- lower_bound=5, upper_bound=27,
- flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <key>yes</key>\n'
- ' <file>module.name</file>\n'
- ' <name>nb_iters</name>\n'
- ' <meaning>An integer flag</meaning>\n'
- ' <default>17</default>\n'
- ' <current>17</current>\n'
- ' <type>int</type>\n'
- ' <lower_bound>5</lower_bound>\n'
- ' <upper_bound>27</upper_bound>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('nb_iters', 'module.name',
- expected_output, is_key=True)
-
- def testFlagHelpInXML_String(self):
- gflags.DEFINE_string('file_path', '/path/to/my/dir', 'A test string flag.',
- flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>simple_module</file>\n'
- ' <name>file_path</name>\n'
- ' <meaning>A test string flag.</meaning>\n'
- ' <default>/path/to/my/dir</default>\n'
- ' <current>/path/to/my/dir</current>\n'
- ' <type>string</type>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('file_path', 'simple_module',
- expected_output)
-
- def testFlagHelpInXML_StringWithXMLIllegalChars(self):
- gflags.DEFINE_string('file_path', '/path/to/\x08my/dir',
- 'A test string flag.', flag_values=self.fv)
- # '\x08' is not a legal character in XML 1.0 documents. Our
- # current code purges such characters from the generated XML.
- expected_output = (
- ' <flag>\n'
- ' <file>simple_module</file>\n'
- ' <name>file_path</name>\n'
- ' <meaning>A test string flag.</meaning>\n'
- ' <default>/path/to/my/dir</default>\n'
- ' <current>/path/to/my/dir</current>\n'
- ' <type>string</type>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('file_path', 'simple_module',
- expected_output)
-
- def testFlagHelpInXML_Boolean(self):
- gflags.DEFINE_boolean('use_hack', False, 'Use performance hack',
- flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <key>yes</key>\n'
- ' <file>a_module</file>\n'
- ' <name>use_hack</name>\n'
- ' <meaning>Use performance hack</meaning>\n'
- ' <default>false</default>\n'
- ' <current>false</current>\n'
- ' <type>bool</type>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('use_hack', 'a_module',
- expected_output, is_key=True)
-
- def testFlagHelpInXML_Enum(self):
- gflags.DEFINE_enum('cc_version', 'stable', ['stable', 'experimental'],
- 'Compiler version to use.', flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>tool</file>\n'
- ' <name>cc_version</name>\n'
- ' <meaning>&lt;stable|experimental&gt;: '
- 'Compiler version to use.</meaning>\n'
- ' <default>stable</default>\n'
- ' <current>stable</current>\n'
- ' <type>string enum</type>\n'
- ' <enum_value>stable</enum_value>\n'
- ' <enum_value>experimental</enum_value>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('cc_version', 'tool', expected_output)
-
- def testFlagHelpInXML_CommaSeparatedList(self):
- gflags.DEFINE_list('files', 'a.cc,a.h,archive/old.zip',
- 'Files to process.', flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>tool</file>\n'
- ' <name>files</name>\n'
- ' <meaning>Files to process.</meaning>\n'
- ' <default>a.cc,a.h,archive/old.zip</default>\n'
- ' <current>[\'a.cc\', \'a.h\', \'archive/old.zip\']</current>\n'
- ' <type>comma separated list of strings</type>\n'
- ' <list_separator>\',\'</list_separator>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('files', 'tool', expected_output)
-
- def testListAsDefaultArgument_CommaSeparatedList(self):
- gflags.DEFINE_list('allow_users', ['alice', 'bob'],
- 'Users with access.', flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>tool</file>\n'
- ' <name>allow_users</name>\n'
- ' <meaning>Users with access.</meaning>\n'
- ' <default>alice,bob</default>\n'
- ' <current>[\'alice\', \'bob\']</current>\n'
- ' <type>comma separated list of strings</type>\n'
- ' <list_separator>\',\'</list_separator>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('allow_users', 'tool', expected_output)
-
- def testFlagHelpInXML_SpaceSeparatedList(self):
- gflags.DEFINE_spaceseplist('dirs', 'src libs bin',
- 'Directories to search.', flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>tool</file>\n'
- ' <name>dirs</name>\n'
- ' <meaning>Directories to search.</meaning>\n'
- ' <default>src libs bin</default>\n'
- ' <current>[\'src\', \'libs\', \'bin\']</current>\n'
- ' <type>whitespace separated list of strings</type>\n'
- 'LIST_SEPARATORS'
- ' </flag>\n').replace('LIST_SEPARATORS',
- _ListSeparatorsInXMLFormat(string.whitespace,
- indent=' '))
- self._CheckFlagHelpInXML('dirs', 'tool', expected_output)
-
- def testFlagHelpInXML_MultiString(self):
- gflags.DEFINE_multistring('to_delete', ['a.cc', 'b.h'],
- 'Files to delete', flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>tool</file>\n'
- ' <name>to_delete</name>\n'
- ' <meaning>Files to delete;\n '
- 'repeat this option to specify a list of values</meaning>\n'
- ' <default>[\'a.cc\', \'b.h\']</default>\n'
- ' <current>[\'a.cc\', \'b.h\']</current>\n'
- ' <type>multi string</type>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('to_delete', 'tool', expected_output)
-
- def testFlagHelpInXML_MultiInt(self):
- gflags.DEFINE_multi_int('cols', [5, 7, 23],
- 'Columns to select', flag_values=self.fv)
- expected_output = (
- ' <flag>\n'
- ' <file>tool</file>\n'
- ' <name>cols</name>\n'
- ' <meaning>Columns to select;\n '
- 'repeat this option to specify a list of values</meaning>\n'
- ' <default>[5, 7, 23]</default>\n'
- ' <current>[5, 7, 23]</current>\n'
- ' <type>multi int</type>\n'
- ' </flag>\n')
- self._CheckFlagHelpInXML('cols', 'tool', expected_output)
-
-
-# The next EXPECTED_HELP_XML_* constants are parts of a template for
-# the expected XML output from WriteHelpInXMLFormatTest below. When
-# we assemble these parts into a single big string, we'll take into
-# account the ordering between the name of the main module and the
-# name of module_bar. Next, we'll fill in the docstring for this
-# module (%(usage_doc)s), the name of the main module
-# (%(main_module_name)s) and the name of the module module_bar
-# (%(module_bar_name)s). See WriteHelpInXMLFormatTest below.
-#
-# NOTE: given the current implementation of _GetMainModule(), we
-# already know the ordering between the main module and module_bar.
-# However, there is no guarantee that _GetMainModule will never be
-# changed in the future (especially since it's far from perfect).
-EXPECTED_HELP_XML_START = """\
-<?xml version="1.0"?>
-<AllFlags>
- <program>gflags_helpxml_test.py</program>
- <usage>%(usage_doc)s</usage>
-"""
-
-EXPECTED_HELP_XML_FOR_FLAGS_FROM_MAIN_MODULE = """\
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>allow_users</name>
- <meaning>Users with access.</meaning>
- <default>alice,bob</default>
- <current>['alice', 'bob']</current>
- <type>comma separated list of strings</type>
- <list_separator>','</list_separator>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>cc_version</name>
- <meaning>&lt;stable|experimental&gt;: Compiler version to use.</meaning>
- <default>stable</default>
- <current>stable</current>
- <type>string enum</type>
- <enum_value>stable</enum_value>
- <enum_value>experimental</enum_value>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>cols</name>
- <meaning>Columns to select;
- repeat this option to specify a list of values</meaning>
- <default>[5, 7, 23]</default>
- <current>[5, 7, 23]</current>
- <type>multi int</type>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>dirs</name>
- <meaning>Directories to create.</meaning>
- <default>src libs bins</default>
- <current>['src', 'libs', 'bins']</current>
- <type>whitespace separated list of strings</type>
-%(whitespace_separators)s </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>file_path</name>
- <meaning>A test string flag.</meaning>
- <default>/path/to/my/dir</default>
- <current>/path/to/my/dir</current>
- <type>string</type>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>files</name>
- <meaning>Files to process.</meaning>
- <default>a.cc,a.h,archive/old.zip</default>
- <current>['a.cc', 'a.h', 'archive/old.zip']</current>
- <type>comma separated list of strings</type>
- <list_separator>\',\'</list_separator>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>index</name>
- <meaning>An integer flag</meaning>
- <default>17</default>
- <current>17</current>
- <type>int</type>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>nb_iters</name>
- <meaning>An integer flag</meaning>
- <default>17</default>
- <current>17</current>
- <type>int</type>
- <lower_bound>5</lower_bound>
- <upper_bound>27</upper_bound>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>to_delete</name>
- <meaning>Files to delete;
- repeat this option to specify a list of values</meaning>
- <default>['a.cc', 'b.h']</default>
- <current>['a.cc', 'b.h']</current>
- <type>multi string</type>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(main_module_name)s</file>
- <name>use_hack</name>
- <meaning>Use performance hack</meaning>
- <default>false</default>
- <current>false</current>
- <type>bool</type>
- </flag>
-"""
-
-EXPECTED_HELP_XML_FOR_FLAGS_FROM_MODULE_BAR = """\
- <flag>
- <file>%(module_bar_name)s</file>
- <name>tmod_bar_t</name>
- <meaning>Sample int flag.</meaning>
- <default>4</default>
- <current>4</current>
- <type>int</type>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(module_bar_name)s</file>
- <name>tmod_bar_u</name>
- <meaning>Sample int flag.</meaning>
- <default>5</default>
- <current>5</current>
- <type>int</type>
- </flag>
- <flag>
- <file>%(module_bar_name)s</file>
- <name>tmod_bar_v</name>
- <meaning>Sample int flag.</meaning>
- <default>6</default>
- <current>6</current>
- <type>int</type>
- </flag>
- <flag>
- <file>%(module_bar_name)s</file>
- <name>tmod_bar_x</name>
- <meaning>Boolean flag.</meaning>
- <default>true</default>
- <current>true</current>
- <type>bool</type>
- </flag>
- <flag>
- <file>%(module_bar_name)s</file>
- <name>tmod_bar_y</name>
- <meaning>String flag.</meaning>
- <default>default</default>
- <current>default</current>
- <type>string</type>
- </flag>
- <flag>
- <key>yes</key>
- <file>%(module_bar_name)s</file>
- <name>tmod_bar_z</name>
- <meaning>Another boolean flag from module bar.</meaning>
- <default>false</default>
- <current>false</current>
- <type>bool</type>
- </flag>
-"""
-
-EXPECTED_HELP_XML_END = """\
-</AllFlags>
-"""
-
-
-class WriteHelpInXMLFormatTest(googletest.TestCase):
- """Big test of FlagValues.WriteHelpInXMLFormat, with several flags."""
-
- def testWriteHelpInXMLFormat(self):
- fv = gflags.FlagValues()
- # Since these flags are defined by the top module, they are all key.
- gflags.DEFINE_integer('index', 17, 'An integer flag', flag_values=fv)
- gflags.DEFINE_integer('nb_iters', 17, 'An integer flag',
- lower_bound=5, upper_bound=27, flag_values=fv)
- gflags.DEFINE_string('file_path', '/path/to/my/dir', 'A test string flag.',
- flag_values=fv)
- gflags.DEFINE_boolean('use_hack', False, 'Use performance hack',
- flag_values=fv)
- gflags.DEFINE_enum('cc_version', 'stable', ['stable', 'experimental'],
- 'Compiler version to use.', flag_values=fv)
- gflags.DEFINE_list('files', 'a.cc,a.h,archive/old.zip',
- 'Files to process.', flag_values=fv)
- gflags.DEFINE_list('allow_users', ['alice', 'bob'],
- 'Users with access.', flag_values=fv)
- gflags.DEFINE_spaceseplist('dirs', 'src libs bins',
- 'Directories to create.', flag_values=fv)
- gflags.DEFINE_multistring('to_delete', ['a.cc', 'b.h'],
- 'Files to delete', flag_values=fv)
- gflags.DEFINE_multi_int('cols', [5, 7, 23],
- 'Columns to select', flag_values=fv)
- # Define a few flags in a different module.
- module_bar.DefineFlags(flag_values=fv)
- # And declare only a few of them to be key. This way, we have
- # different kinds of flags, defined in different modules, and not
- # all of them are key flags.
- gflags.DECLARE_key_flag('tmod_bar_z', flag_values=fv)
- gflags.DECLARE_key_flag('tmod_bar_u', flag_values=fv)
-
- # Generate flag help in XML format in the StringIO sio.
- sio = StringIO.StringIO()
- fv.WriteHelpInXMLFormat(sio)
-
- # Check that we got the expected result.
- expected_output_template = EXPECTED_HELP_XML_START
- main_module_name = gflags._GetMainModule()
- module_bar_name = module_bar.__name__
-
- if main_module_name < module_bar_name:
- expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MAIN_MODULE
- expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MODULE_BAR
- else:
- expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MODULE_BAR
- expected_output_template += EXPECTED_HELP_XML_FOR_FLAGS_FROM_MAIN_MODULE
-
- expected_output_template += EXPECTED_HELP_XML_END
-
- # XML representation of the whitespace list separators.
- whitespace_separators = _ListSeparatorsInXMLFormat(string.whitespace,
- indent=' ')
- expected_output = (
- expected_output_template %
- {'usage_doc': sys.modules['__main__'].__doc__,
- 'main_module_name': main_module_name,
- 'module_bar_name': module_bar_name,
- 'whitespace_separators': whitespace_separators})
-
- actual_output = sio.getvalue()
- self.assertMultiLineEqual(actual_output, expected_output)
-
- # Also check that our result is valid XML. minidom.parseString
- # throws an xml.parsers.expat.ExpatError in case of an error.
- xml.dom.minidom.parseString(actual_output)
-
-
-if __name__ == '__main__':
- googletest.main()
diff --git a/third_party/py/gflags/tests/gflags_unittest.py b/third_party/py/gflags/tests/gflags_unittest.py
deleted file mode 100755
index 8e948bf36f..0000000000
--- a/third_party/py/gflags/tests/gflags_unittest.py
+++ /dev/null
@@ -1,1949 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (c) 2007, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-"Unittest for gflags.py module"
-
-__pychecker__ = "no-local" # for unittest
-
-
-import cStringIO
-import sys
-import os
-import shutil
-
-import gflags
-from flags_modules_for_testing import module_foo
-from flags_modules_for_testing import module_bar
-from flags_modules_for_testing import module_baz
-
-FLAGS=gflags.FLAGS
-
-import gflags_googletest as googletest
-
-# TODO(csilvers): add a wrapper function around FLAGS(argv) that
-# verifies the input is a list or tuple. This avoids bugs where we
-# make argv a string instead of a list, by mistake.
-
-class FlagsUnitTest(googletest.TestCase):
- "Flags Unit Test"
-
- def setUp(self):
- # make sure we are using the old, stupid way of parsing flags.
- FLAGS.UseGnuGetOpt(False)
-
- def test_flags(self):
-
- ##############################################
- # Test normal usage with no (expected) errors.
-
- # Define flags
- number_test_framework_flags = len(FLAGS.RegisteredFlags())
- repeatHelp = "how many times to repeat (0-5)"
- gflags.DEFINE_integer("repeat", 4, repeatHelp,
- lower_bound=0, short_name='r')
- gflags.DEFINE_string("name", "Bob", "namehelp")
- gflags.DEFINE_boolean("debug", 0, "debughelp")
- gflags.DEFINE_boolean("q", 1, "quiet mode")
- gflags.DEFINE_boolean("quack", 0, "superstring of 'q'")
- gflags.DEFINE_boolean("noexec", 1, "boolean flag with no as prefix")
- gflags.DEFINE_integer("x", 3, "how eXtreme to be")
- gflags.DEFINE_integer("l", 0x7fffffff00000000, "how long to be")
- gflags.DEFINE_list('letters', 'a,b,c', "a list of letters")
- gflags.DEFINE_list('numbers', [1, 2, 3], "a list of numbers")
- gflags.DEFINE_enum("kwery", None, ['who', 'what', 'why', 'where', 'when'],
- "?")
-
- # Specify number of flags defined above. The short_name defined
- # for 'repeat' counts as an extra flag.
- number_defined_flags = 11 + 1
- self.assertEqual(len(FLAGS.RegisteredFlags()),
- number_defined_flags + number_test_framework_flags)
-
- assert FLAGS.repeat == 4, "integer default values not set:" + FLAGS.repeat
- assert FLAGS.name == 'Bob', "default values not set:" + FLAGS.name
- assert FLAGS.debug == 0, "boolean default values not set:" + FLAGS.debug
- assert FLAGS.q == 1, "boolean default values not set:" + FLAGS.q
- assert FLAGS.x == 3, "integer default values not set:" + FLAGS.x
- assert FLAGS.l == 0x7fffffff00000000, ("integer default values not set:"
- + FLAGS.l)
- assert FLAGS.letters == ['a', 'b', 'c'], ("list default values not set:"
- + FLAGS.letters)
- assert FLAGS.numbers == [1, 2, 3], ("list default values not set:"
- + FLAGS.numbers)
- assert FLAGS.kwery is None, ("enum default None value not set:"
- + FLAGS.kwery)
-
- flag_values = FLAGS.FlagValuesDict()
- assert flag_values['repeat'] == 4
- assert flag_values['name'] == 'Bob'
- assert flag_values['debug'] == 0
- assert flag_values['r'] == 4 # short for repeat
- assert flag_values['q'] == 1
- assert flag_values['quack'] == 0
- assert flag_values['x'] == 3
- assert flag_values['l'] == 0x7fffffff00000000
- assert flag_values['letters'] == ['a', 'b', 'c']
- assert flag_values['numbers'] == [1, 2, 3]
- assert flag_values['kwery'] is None
-
- # Verify string form of defaults
- assert FLAGS['repeat'].default_as_str == "'4'"
- assert FLAGS['name'].default_as_str == "'Bob'"
- assert FLAGS['debug'].default_as_str == "'false'"
- assert FLAGS['q'].default_as_str == "'true'"
- assert FLAGS['quack'].default_as_str == "'false'"
- assert FLAGS['noexec'].default_as_str == "'true'"
- assert FLAGS['x'].default_as_str == "'3'"
- assert FLAGS['l'].default_as_str == "'9223372032559808512'"
- assert FLAGS['letters'].default_as_str == "'a,b,c'"
- assert FLAGS['numbers'].default_as_str == "'1,2,3'"
-
- # Verify that the iterator for flags yields all the keys
- keys = list(FLAGS)
- keys.sort()
- reg_flags = FLAGS.RegisteredFlags()
- reg_flags.sort()
- self.assertEqual(keys, reg_flags)
-
- # Parse flags
- # .. empty command line
- argv = ('./program',)
- argv = FLAGS(argv)
- assert len(argv) == 1, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
-
- # .. non-empty command line
- argv = ('./program', '--debug', '--name=Bob', '-q', '--x=8')
- argv = FLAGS(argv)
- assert len(argv) == 1, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert FLAGS['debug'].present == 1
- FLAGS['debug'].present = 0 # Reset
- assert FLAGS['name'].present == 1
- FLAGS['name'].present = 0 # Reset
- assert FLAGS['q'].present == 1
- FLAGS['q'].present = 0 # Reset
- assert FLAGS['x'].present == 1
- FLAGS['x'].present = 0 # Reset
-
- # Flags list
- self.assertEqual(len(FLAGS.RegisteredFlags()),
- number_defined_flags + number_test_framework_flags)
- assert 'name' in FLAGS.RegisteredFlags()
- assert 'debug' in FLAGS.RegisteredFlags()
- assert 'repeat' in FLAGS.RegisteredFlags()
- assert 'r' in FLAGS.RegisteredFlags()
- assert 'q' in FLAGS.RegisteredFlags()
- assert 'quack' in FLAGS.RegisteredFlags()
- assert 'x' in FLAGS.RegisteredFlags()
- assert 'l' in FLAGS.RegisteredFlags()
- assert 'letters' in FLAGS.RegisteredFlags()
- assert 'numbers' in FLAGS.RegisteredFlags()
-
- # has_key
- assert FLAGS.has_key('name')
- assert not FLAGS.has_key('name2')
- assert 'name' in FLAGS
- assert 'name2' not in FLAGS
-
- # try deleting a flag
- del FLAGS.r
- self.assertEqual(len(FLAGS.RegisteredFlags()),
- number_defined_flags - 1 + number_test_framework_flags)
- assert not 'r' in FLAGS.RegisteredFlags()
-
- # .. command line with extra stuff
- argv = ('./program', '--debug', '--name=Bob', 'extra')
- argv = FLAGS(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
- assert FLAGS['debug'].present == 1
- FLAGS['debug'].present = 0 # Reset
- assert FLAGS['name'].present == 1
- FLAGS['name'].present = 0 # Reset
-
- # Test reset
- argv = ('./program', '--debug')
- argv = FLAGS(argv)
- assert len(argv) == 1, "wrong number of arguments pulled"
- assert argv[0] == './program', "program name not preserved"
- assert FLAGS['debug'].present == 1
- assert FLAGS['debug'].value
- FLAGS.Reset()
- assert FLAGS['debug'].present == 0
- assert not FLAGS['debug'].value
-
- # Test that reset restores default value when default value is None.
- argv = ('./program', '--kwery=who')
- argv = FLAGS(argv)
- assert len(argv) == 1, "wrong number of arguments pulled"
- assert argv[0] == './program', "program name not preserved"
- assert FLAGS['kwery'].present == 1
- assert FLAGS['kwery'].value == 'who'
- FLAGS.Reset()
- assert FLAGS['kwery'].present == 0
- assert FLAGS['kwery'].value == None
-
- # Test integer argument passing
- argv = ('./program', '--x', '0x12345')
- argv = FLAGS(argv)
- self.assertEquals(FLAGS.x, 0x12345)
- self.assertEquals(type(FLAGS.x), int)
-
- argv = ('./program', '--x', '0x1234567890ABCDEF1234567890ABCDEF')
- argv = FLAGS(argv)
- self.assertEquals(FLAGS.x, 0x1234567890ABCDEF1234567890ABCDEF)
- self.assertEquals(type(FLAGS.x), long)
-
- # Treat 0-prefixed parameters as base-10, not base-8
- argv = ('./program', '--x', '012345')
- argv = FLAGS(argv)
- self.assertEquals(FLAGS.x, 12345)
- self.assertEquals(type(FLAGS.x), int)
-
- argv = ('./program', '--x', '0123459')
- argv = FLAGS(argv)
- self.assertEquals(FLAGS.x, 123459)
- self.assertEquals(type(FLAGS.x), int)
-
- argv = ('./program', '--x', '0x123efg')
- try:
- argv = FLAGS(argv)
- raise AssertionError("failed to detect invalid hex argument")
- except gflags.IllegalFlagValue:
- pass
-
- # Test boolean argument parsing
- gflags.DEFINE_boolean("test0", None, "test boolean parsing")
- argv = ('./program', '--notest0')
- argv = FLAGS(argv)
- assert FLAGS.test0 == 0
-
- gflags.DEFINE_boolean("test1", None, "test boolean parsing")
- argv = ('./program', '--test1')
- argv = FLAGS(argv)
- assert FLAGS.test1 == 1
-
- FLAGS.test0 = None
- argv = ('./program', '--test0=false')
- argv = FLAGS(argv)
- assert FLAGS.test0 == 0
-
- FLAGS.test1 = None
- argv = ('./program', '--test1=true')
- argv = FLAGS(argv)
- assert FLAGS.test1 == 1
-
- FLAGS.test0 = None
- argv = ('./program', '--test0=0')
- argv = FLAGS(argv)
- assert FLAGS.test0 == 0
-
- FLAGS.test1 = None
- argv = ('./program', '--test1=1')
- argv = FLAGS(argv)
- assert FLAGS.test1 == 1
-
- # Test booleans that already have 'no' as a prefix
- FLAGS.noexec = None
- argv = ('./program', '--nonoexec', '--name', 'Bob')
- argv = FLAGS(argv)
- assert FLAGS.noexec == 0
-
- FLAGS.noexec = None
- argv = ('./program', '--name', 'Bob', '--noexec')
- argv = FLAGS(argv)
- assert FLAGS.noexec == 1
-
- # Test unassigned booleans
- gflags.DEFINE_boolean("testnone", None, "test boolean parsing")
- argv = ('./program',)
- argv = FLAGS(argv)
- assert FLAGS.testnone == None
-
- # Test get with default
- gflags.DEFINE_boolean("testget1", None, "test parsing with defaults")
- gflags.DEFINE_boolean("testget2", None, "test parsing with defaults")
- gflags.DEFINE_boolean("testget3", None, "test parsing with defaults")
- gflags.DEFINE_integer("testget4", None, "test parsing with defaults")
- argv = ('./program','--testget1','--notestget2')
- argv = FLAGS(argv)
- assert FLAGS.get('testget1', 'foo') == 1
- assert FLAGS.get('testget2', 'foo') == 0
- assert FLAGS.get('testget3', 'foo') == 'foo'
- assert FLAGS.get('testget4', 'foo') == 'foo'
-
- # test list code
- lists = [['hello','moo','boo','1'],
- [],]
-
- gflags.DEFINE_list('testlist', '', 'test lists parsing')
- gflags.DEFINE_spaceseplist('testspacelist', '', 'tests space lists parsing')
-
- for name, sep in (('testlist', ','), ('testspacelist', ' '),
- ('testspacelist', '\n')):
- for lst in lists:
- argv = ('./program', '--%s=%s' % (name, sep.join(lst)))
- argv = FLAGS(argv)
- self.assertEquals(getattr(FLAGS, name), lst)
-
- # Test help text
- flagsHelp = str(FLAGS)
- assert flagsHelp.find("repeat") != -1, "cannot find flag in help"
- assert flagsHelp.find(repeatHelp) != -1, "cannot find help string in help"
-
- # Test flag specified twice
- argv = ('./program', '--repeat=4', '--repeat=2', '--debug', '--nodebug')
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.get('repeat', None), 2)
- self.assertEqual(FLAGS.get('debug', None), 0)
-
- # Test MultiFlag with single default value
- gflags.DEFINE_multistring('s_str', 'sing1',
- 'string option that can occur multiple times',
- short_name='s')
- self.assertEqual(FLAGS.get('s_str', None), [ 'sing1', ])
-
- # Test MultiFlag with list of default values
- multi_string_defs = [ 'def1', 'def2', ]
- gflags.DEFINE_multistring('m_str', multi_string_defs,
- 'string option that can occur multiple times',
- short_name='m')
- self.assertEqual(FLAGS.get('m_str', None), multi_string_defs)
-
- # Test flag specified multiple times with a MultiFlag
- argv = ('./program', '--m_str=str1', '-m', 'str2')
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.get('m_str', None), [ 'str1', 'str2', ])
-
- # Test single-letter flags; should support both single and double dash
- argv = ('./program', '-q', '-x8')
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.get('q', None), 1)
- self.assertEqual(FLAGS.get('x', None), 8)
-
- argv = ('./program', '--q', '--x', '9', '--noqu')
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.get('q', None), 1)
- self.assertEqual(FLAGS.get('x', None), 9)
- # --noqu should match '--noquack since it's a unique prefix
- self.assertEqual(FLAGS.get('quack', None), 0)
-
- argv = ('./program', '--noq', '--x=10', '--qu')
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.get('q', None), 0)
- self.assertEqual(FLAGS.get('x', None), 10)
- self.assertEqual(FLAGS.get('quack', None), 1)
-
- ####################################
- # Test flag serialization code:
-
- oldtestlist = FLAGS.testlist
- oldtestspacelist = FLAGS.testspacelist
-
- argv = ('./program',
- FLAGS['test0'].Serialize(),
- FLAGS['test1'].Serialize(),
- FLAGS['testnone'].Serialize(),
- FLAGS['s_str'].Serialize())
- argv = FLAGS(argv)
- self.assertEqual(FLAGS['test0'].Serialize(), '--notest0')
- self.assertEqual(FLAGS['test1'].Serialize(), '--test1')
- self.assertEqual(FLAGS['testnone'].Serialize(), '')
- self.assertEqual(FLAGS['s_str'].Serialize(), '--s_str=sing1')
-
- testlist1 = ['aa', 'bb']
- testspacelist1 = ['aa', 'bb', 'cc']
- FLAGS.testlist = list(testlist1)
- FLAGS.testspacelist = list(testspacelist1)
- argv = ('./program',
- FLAGS['testlist'].Serialize(),
- FLAGS['testspacelist'].Serialize())
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.testlist, testlist1)
- self.assertEqual(FLAGS.testspacelist, testspacelist1)
-
- testlist1 = ['aa some spaces', 'bb']
- testspacelist1 = ['aa', 'bb,some,commas,', 'cc']
- FLAGS.testlist = list(testlist1)
- FLAGS.testspacelist = list(testspacelist1)
- argv = ('./program',
- FLAGS['testlist'].Serialize(),
- FLAGS['testspacelist'].Serialize())
- argv = FLAGS(argv)
- self.assertEqual(FLAGS.testlist, testlist1)
- self.assertEqual(FLAGS.testspacelist, testspacelist1)
-
- FLAGS.testlist = oldtestlist
- FLAGS.testspacelist = oldtestspacelist
-
- ####################################
- # Test flag-update:
-
- def ArgsString():
- flagnames = FLAGS.RegisteredFlags()
-
- flagnames.sort()
- nonbool_flags = ['--%s %s' % (name, FLAGS.get(name, None))
- for name in flagnames
- if not isinstance(FLAGS[name], gflags.BooleanFlag)]
-
- truebool_flags = ['--%s' % (name)
- for name in flagnames
- if isinstance(FLAGS[name], gflags.BooleanFlag) and
- FLAGS.get(name, None)]
- falsebool_flags = ['--no%s' % (name)
- for name in flagnames
- if isinstance(FLAGS[name], gflags.BooleanFlag) and
- not FLAGS.get(name, None)]
- return ' '.join(nonbool_flags + truebool_flags + falsebool_flags)
-
- argv = ('./program', '--repeat=3', '--name=giants', '--nodebug')
-
- FLAGS(argv)
- self.assertEqual(FLAGS.get('repeat', None), 3)
- self.assertEqual(FLAGS.get('name', None), 'giants')
- self.assertEqual(FLAGS.get('debug', None), 0)
- self.assertEqual(ArgsString(),
- "--kwery None "
- "--l 9223372032559808512 "
- "--letters ['a', 'b', 'c'] "
- "--m ['str1', 'str2'] --m_str ['str1', 'str2'] "
- "--name giants "
- "--numbers [1, 2, 3] "
- "--repeat 3 "
- "--s ['sing1'] --s_str ['sing1'] "
- ""
- ""
- "--testget4 None --testlist [] "
- "--testspacelist [] --x 10 "
- "--noexec --quack "
- "--test1 "
- "--testget1 --tmod_baz_x "
- "--no? --nodebug --nohelp --nohelpshort --nohelpxml --noq "
- ""
- "--notest0 --notestget2 --notestget3 --notestnone")
-
- argv = ('./program', '--debug', '--m_str=upd1', '-s', 'upd2')
- FLAGS(argv)
- self.assertEqual(FLAGS.get('repeat', None), 3)
- self.assertEqual(FLAGS.get('name', None), 'giants')
- self.assertEqual(FLAGS.get('debug', None), 1)
-
- # items appended to existing non-default value lists for --m/--m_str
- # new value overwrites default value (not appended to it) for --s/--s_str
- self.assertEqual(ArgsString(),
- "--kwery None "
- "--l 9223372032559808512 "
- "--letters ['a', 'b', 'c'] "
- "--m ['str1', 'str2', 'upd1'] "
- "--m_str ['str1', 'str2', 'upd1'] "
- "--name giants "
- "--numbers [1, 2, 3] "
- "--repeat 3 "
- "--s ['upd2'] --s_str ['upd2'] "
- ""
- ""
- "--testget4 None --testlist [] "
- "--testspacelist [] --x 10 "
- "--debug --noexec --quack "
- "--test1 "
- "--testget1 --tmod_baz_x "
- "--no? --nohelp --nohelpshort --nohelpxml --noq "
- ""
- "--notest0 --notestget2 --notestget3 --notestnone")
-
- ####################################
- # Test all kind of error conditions.
-
- # Duplicate flag detection
- try:
- gflags.DEFINE_boolean("run", 0, "runhelp", short_name='q')
- raise AssertionError("duplicate flag detection failed")
- except gflags.DuplicateFlag:
- pass
-
- # Duplicate short flag detection
- try:
- gflags.DEFINE_boolean("zoom1", 0, "runhelp z1", short_name='z')
- gflags.DEFINE_boolean("zoom2", 0, "runhelp z2", short_name='z')
- raise AssertionError("duplicate short flag detection failed")
- except gflags.DuplicateFlag, e:
- self.assertTrue("The flag 'z' is defined twice. " in e.args[0])
- self.assertTrue("First from" in e.args[0])
- self.assertTrue(", Second from" in e.args[0])
-
- # Duplicate mixed flag detection
- try:
- gflags.DEFINE_boolean("short1", 0, "runhelp s1", short_name='s')
- gflags.DEFINE_boolean("s", 0, "runhelp s2")
- raise AssertionError("duplicate mixed flag detection failed")
- except gflags.DuplicateFlag, e:
- self.assertTrue("The flag 's' is defined twice. " in e.args[0])
- self.assertTrue("First from" in e.args[0])
- self.assertTrue(", Second from" in e.args[0])
-
- # Check that duplicate flag detection detects definition sites
- # correctly.
- flagnames = ["repeated"]
- original_flags = gflags.FlagValues()
- gflags.DEFINE_boolean(flagnames[0], False, "Flag about to be repeated.",
- flag_values=original_flags)
- duplicate_flags = module_foo.DuplicateFlags(flagnames)
- try:
- original_flags.AppendFlagValues(duplicate_flags)
- except gflags.DuplicateFlagError, e:
- self.assertTrue("flags_unittest" in str(e))
- self.assertTrue("module_foo" in str(e))
-
- # Make sure allow_override works
- try:
- gflags.DEFINE_boolean("dup1", 0, "runhelp d11", short_name='u',
- allow_override=0)
- flag = FLAGS.FlagDict()['dup1']
- self.assertEqual(flag.default, 0)
-
- gflags.DEFINE_boolean("dup1", 1, "runhelp d12", short_name='u',
- allow_override=1)
- flag = FLAGS.FlagDict()['dup1']
- self.assertEqual(flag.default, 1)
- except gflags.DuplicateFlag:
- raise AssertionError("allow_override did not permit a flag duplication")
-
- # Make sure allow_override works
- try:
- gflags.DEFINE_boolean("dup2", 0, "runhelp d21", short_name='u',
- allow_override=1)
- flag = FLAGS.FlagDict()['dup2']
- self.assertEqual(flag.default, 0)
-
- gflags.DEFINE_boolean("dup2", 1, "runhelp d22", short_name='u',
- allow_override=0)
- flag = FLAGS.FlagDict()['dup2']
- self.assertEqual(flag.default, 1)
- except gflags.DuplicateFlag:
- raise AssertionError("allow_override did not permit a flag duplication")
-
- # Make sure allow_override doesn't work with None default
- try:
- gflags.DEFINE_boolean("dup3", 0, "runhelp d31", short_name='u3',
- allow_override=0)
- flag = FLAGS.FlagDict()['dup3']
- self.assertEqual(flag.default, 0)
-
- gflags.DEFINE_boolean("dup3", None, "runhelp d32", short_name='u3',
- allow_override=1)
- raise AssertionError('Cannot override a flag with a default of None')
- except gflags.DuplicateFlagCannotPropagateNoneToSwig:
- pass
-
- # Make sure that re-importing a module does not cause a DuplicateFlagError
- # to be raised.
- try:
- sys.modules.pop(
- "flags_modules_for_testing.module_baz")
- import flags_modules_for_testing.module_baz
- except gflags.DuplicateFlagError:
- raise AssertionError("Module reimport caused flag duplication error")
-
- # Make sure that when we override, the help string gets updated correctly
- gflags.DEFINE_boolean("dup3", 0, "runhelp d31", short_name='u',
- allow_override=1)
- gflags.DEFINE_boolean("dup3", 1, "runhelp d32", short_name='u',
- allow_override=1)
- self.assert_(str(FLAGS).find('runhelp d31') == -1)
- self.assert_(str(FLAGS).find('runhelp d32') != -1)
-
- # Make sure AppendFlagValues works
- new_flags = gflags.FlagValues()
- gflags.DEFINE_boolean("new1", 0, "runhelp n1", flag_values=new_flags)
- gflags.DEFINE_boolean("new2", 0, "runhelp n2", flag_values=new_flags)
- self.assertEqual(len(new_flags.FlagDict()), 2)
- old_len = len(FLAGS.FlagDict())
- FLAGS.AppendFlagValues(new_flags)
- self.assertEqual(len(FLAGS.FlagDict())-old_len, 2)
- self.assertEqual("new1" in FLAGS.FlagDict(), True)
- self.assertEqual("new2" in FLAGS.FlagDict(), True)
-
- # Then test that removing those flags works
- FLAGS.RemoveFlagValues(new_flags)
- self.assertEqual(len(FLAGS.FlagDict()), old_len)
- self.assertFalse("new1" in FLAGS.FlagDict())
- self.assertFalse("new2" in FLAGS.FlagDict())
-
- # Make sure AppendFlagValues works with flags with shortnames.
- new_flags = gflags.FlagValues()
- gflags.DEFINE_boolean("new3", 0, "runhelp n3", flag_values=new_flags)
- gflags.DEFINE_boolean("new4", 0, "runhelp n4", flag_values=new_flags,
- short_name="n4")
- self.assertEqual(len(new_flags.FlagDict()), 3)
- old_len = len(FLAGS.FlagDict())
- FLAGS.AppendFlagValues(new_flags)
- self.assertEqual(len(FLAGS.FlagDict())-old_len, 3)
- self.assertTrue("new3" in FLAGS.FlagDict())
- self.assertTrue("new4" in FLAGS.FlagDict())
- self.assertTrue("n4" in FLAGS.FlagDict())
- self.assertEqual(FLAGS.FlagDict()['n4'], FLAGS.FlagDict()['new4'])
-
- # Then test removing them
- FLAGS.RemoveFlagValues(new_flags)
- self.assertEqual(len(FLAGS.FlagDict()), old_len)
- self.assertFalse("new3" in FLAGS.FlagDict())
- self.assertFalse("new4" in FLAGS.FlagDict())
- self.assertFalse("n4" in FLAGS.FlagDict())
-
- # Make sure AppendFlagValues fails on duplicates
- gflags.DEFINE_boolean("dup4", 0, "runhelp d41")
- new_flags = gflags.FlagValues()
- gflags.DEFINE_boolean("dup4", 0, "runhelp d42", flag_values=new_flags)
- try:
- FLAGS.AppendFlagValues(new_flags)
- raise AssertionError("ignore_copy was not set but caused no exception")
- except gflags.DuplicateFlag:
- pass
-
- # Integer out of bounds
- try:
- argv = ('./program', '--repeat=-4')
- FLAGS(argv)
- raise AssertionError('integer bounds exception not raised:'
- + str(FLAGS.repeat))
- except gflags.IllegalFlagValue:
- pass
-
- # Non-integer
- try:
- argv = ('./program', '--repeat=2.5')
- FLAGS(argv)
- raise AssertionError("malformed integer value exception not raised")
- except gflags.IllegalFlagValue:
- pass
-
- # Missing required arugment
- try:
- argv = ('./program', '--name')
- FLAGS(argv)
- raise AssertionError("Flag argument required exception not raised")
- except gflags.FlagsError:
- pass
-
- # Non-boolean arguments for boolean
- try:
- argv = ('./program', '--debug=goofup')
- FLAGS(argv)
- raise AssertionError("Illegal flag value exception not raised")
- except gflags.IllegalFlagValue:
- pass
-
- try:
- argv = ('./program', '--debug=42')
- FLAGS(argv)
- raise AssertionError("Illegal flag value exception not raised")
- except gflags.IllegalFlagValue:
- pass
-
-
- # Non-numeric argument for integer flag --repeat
- try:
- argv = ('./program', '--repeat', 'Bob', 'extra')
- FLAGS(argv)
- raise AssertionError("Illegal flag value exception not raised")
- except gflags.IllegalFlagValue:
- pass
-
- # Test ModuleHelp().
- helpstr = FLAGS.ModuleHelp(module_baz)
-
- expected_help = "\n" + module_baz.__name__ + ":" + """
- --[no]tmod_baz_x: Boolean flag.
- (default: 'true')"""
-
- self.assertMultiLineEqual(expected_help, helpstr)
-
- # Test MainModuleHelp(). This must be part of test_flags because
- # it dpeends on dup1/2/3/etc being introduced first.
- helpstr = FLAGS.MainModuleHelp()
-
- expected_help = "\n" + sys.argv[0] + ':' + """
- --[no]debug: debughelp
- (default: 'false')
- -u,--[no]dup1: runhelp d12
- (default: 'true')
- -u,--[no]dup2: runhelp d22
- (default: 'true')
- -u,--[no]dup3: runhelp d32
- (default: 'true')
- --[no]dup4: runhelp d41
- (default: 'false')
- --kwery: <who|what|why|where|when>: ?
- --l: how long to be
- (default: '9223372032559808512')
- (an integer)
- --letters: a list of letters
- (default: 'a,b,c')
- (a comma separated list)
- -m,--m_str: string option that can occur multiple times;
- repeat this option to specify a list of values
- (default: "['def1', 'def2']")
- --name: namehelp
- (default: 'Bob')
- --[no]noexec: boolean flag with no as prefix
- (default: 'true')
- --numbers: a list of numbers
- (default: '1,2,3')
- (a comma separated list)
- --[no]q: quiet mode
- (default: 'true')
- --[no]quack: superstring of 'q'
- (default: 'false')
- -r,--repeat: how many times to repeat (0-5)
- (default: '4')
- (a non-negative integer)
- -s,--s_str: string option that can occur multiple times;
- repeat this option to specify a list of values
- (default: "['sing1']")
- --[no]test0: test boolean parsing
- --[no]test1: test boolean parsing
- --[no]testget1: test parsing with defaults
- --[no]testget2: test parsing with defaults
- --[no]testget3: test parsing with defaults
- --testget4: test parsing with defaults
- (an integer)
- --testlist: test lists parsing
- (default: '')
- (a comma separated list)
- --[no]testnone: test boolean parsing
- --testspacelist: tests space lists parsing
- (default: '')
- (a whitespace separated list)
- --x: how eXtreme to be
- (default: '3')
- (an integer)
- -z,--[no]zoom1: runhelp z1
- (default: 'false')"""
-
- # Insert the --help flags in their proper place.
- help_help = """\
- -?,--[no]help: show this help
- --[no]helpshort: show usage only for this module
- --[no]helpxml: like --help, but generates XML output
-"""
- expected_help = expected_help.replace(' --kwery',
- help_help + ' --kwery')
-
- self.assertMultiLineEqual(expected_help, helpstr)
-
-
-class MultiNumericalFlagsTest(googletest.TestCase):
-
- def testMultiNumericalFlags(self):
- """Test multi_int and multi_float flags."""
-
- int_defaults = [77, 88,]
- gflags.DEFINE_multi_int('m_int', int_defaults,
- 'integer option that can occur multiple times',
- short_name='mi')
- self.assertListEqual(FLAGS.get('m_int', None), int_defaults)
- argv = ('./program', '--m_int=-99', '--mi=101')
- FLAGS(argv)
- self.assertListEqual(FLAGS.get('m_int', None), [-99, 101,])
-
- float_defaults = [2.2, 3]
- gflags.DEFINE_multi_float('m_float', float_defaults,
- 'float option that can occur multiple times',
- short_name='mf')
- for (expected, actual) in zip(float_defaults, FLAGS.get('m_float', None)):
- self.assertAlmostEquals(expected, actual)
- argv = ('./program', '--m_float=-17', '--mf=2.78e9')
- FLAGS(argv)
- expected_floats = [-17.0, 2.78e9]
- for (expected, actual) in zip(expected_floats, FLAGS.get('m_float', None)):
- self.assertAlmostEquals(expected, actual)
-
- def testSingleValueDefault(self):
- """Test multi_int and multi_float flags with a single default value."""
- int_default = 77
- gflags.DEFINE_multi_int('m_int1', int_default,
- 'integer option that can occur multiple times')
- self.assertListEqual(FLAGS.get('m_int1', None), [int_default])
-
- float_default = 2.2
- gflags.DEFINE_multi_float('m_float1', float_default,
- 'float option that can occur multiple times')
- actual = FLAGS.get('m_float1', None)
- self.assertEquals(1, len(actual))
- self.assertAlmostEquals(actual[0], float_default)
-
- def testBadMultiNumericalFlags(self):
- """Test multi_int and multi_float flags with non-parseable values."""
-
- # Test non-parseable defaults.
- self.assertRaisesWithRegexpMatch(
- gflags.IllegalFlagValue,
- 'flag --m_int2=abc: invalid literal for int\(\) with base 10: \'abc\'',
- gflags.DEFINE_multi_int, 'm_int2', ['abc'], 'desc')
-
- self.assertRaisesWithRegexpMatch(
- gflags.IllegalFlagValue,
- 'flag --m_float2=abc: invalid literal for float\(\): abc',
- gflags.DEFINE_multi_float, 'm_float2', ['abc'], 'desc')
-
- # Test non-parseable command line values.
- gflags.DEFINE_multi_int('m_int2', '77',
- 'integer option that can occur multiple times')
- argv = ('./program', '--m_int2=def')
- self.assertRaisesWithRegexpMatch(
- gflags.IllegalFlagValue,
- 'flag --m_int2=def: invalid literal for int\(\) with base 10: \'def\'',
- FLAGS, argv)
-
- gflags.DEFINE_multi_float('m_float2', 2.2,
- 'float option that can occur multiple times')
- argv = ('./program', '--m_float2=def')
- self.assertRaisesWithRegexpMatch(
- gflags.IllegalFlagValue,
- 'flag --m_float2=def: invalid literal for float\(\): def',
- FLAGS, argv)
-
-
-class UnicodeFlagsTest(googletest.TestCase):
- """Testing proper unicode support for flags."""
-
- def testUnicodeDefaultAndHelpstring(self):
- gflags.DEFINE_string("unicode_str", "\xC3\x80\xC3\xBD".decode("utf-8"),
- "help:\xC3\xAA".decode("utf-8"))
- argv = ("./program",)
- FLAGS(argv) # should not raise any exceptions
-
- argv = ("./program", "--unicode_str=foo")
- FLAGS(argv) # should not raise any exceptions
-
- def testUnicodeInList(self):
- gflags.DEFINE_list("unicode_list", ["abc", "\xC3\x80".decode("utf-8"),
- "\xC3\xBD".decode("utf-8")],
- "help:\xC3\xAB".decode("utf-8"))
- argv = ("./program",)
- FLAGS(argv) # should not raise any exceptions
-
- argv = ("./program", "--unicode_list=hello,there")
- FLAGS(argv) # should not raise any exceptions
-
- def testXMLOutput(self):
- gflags.DEFINE_string("unicode1", "\xC3\x80\xC3\xBD".decode("utf-8"),
- "help:\xC3\xAC".decode("utf-8"))
- gflags.DEFINE_list("unicode2", ["abc", "\xC3\x80".decode("utf-8"),
- "\xC3\xBD".decode("utf-8")],
- "help:\xC3\xAD".decode("utf-8"))
- gflags.DEFINE_list("non_unicode", ["abc", "def", "ghi"],
- "help:\xC3\xAD".decode("utf-8"))
-
- outfile = cStringIO.StringIO()
- FLAGS.WriteHelpInXMLFormat(outfile)
- actual_output = outfile.getvalue()
-
- # The xml output is large, so we just check parts of it.
- self.assertTrue("<name>unicode1</name>\n"
- " <meaning>help:&#236;</meaning>\n"
- " <default>&#192;&#253;</default>\n"
- " <current>&#192;&#253;</current>"
- in actual_output)
- self.assertTrue("<name>unicode2</name>\n"
- " <meaning>help:&#237;</meaning>\n"
- " <default>abc,&#192;,&#253;</default>\n"
- " <current>[\'abc\', u\'\\xc0\', u\'\\xfd\']</current>"
- in actual_output)
- self.assertTrue("<name>non_unicode</name>\n"
- " <meaning>help:&#237;</meaning>\n"
- " <default>abc,def,ghi</default>\n"
- " <current>[\'abc\', \'def\', \'ghi\']</current>"
- in actual_output)
-
-
-class LoadFromFlagFileTest(googletest.TestCase):
- """Testing loading flags from a file and parsing them."""
-
- def setUp(self):
- self.flag_values = gflags.FlagValues()
- # make sure we are using the old, stupid way of parsing flags.
- self.flag_values.UseGnuGetOpt(False)
- gflags.DEFINE_string('UnitTestMessage1', 'Foo!', 'You Add Here.',
- flag_values=self.flag_values)
- gflags.DEFINE_string('UnitTestMessage2', 'Bar!', 'Hello, Sailor!',
- flag_values=self.flag_values)
- gflags.DEFINE_boolean('UnitTestBoolFlag', 0, 'Some Boolean thing',
- flag_values=self.flag_values)
- gflags.DEFINE_integer('UnitTestNumber', 12345, 'Some integer',
- lower_bound=0, flag_values=self.flag_values)
- gflags.DEFINE_list('UnitTestList', "1,2,3", 'Some list',
- flag_values=self.flag_values)
- self.files_to_delete = []
-
- def tearDown(self):
- self._RemoveTestFiles()
-
- def _SetupTestFiles(self):
- """ Creates and sets up some dummy flagfile files with bogus flags"""
-
- # Figure out where to create temporary files
- tmp_path = '/tmp/flags_unittest'
- if os.path.exists(tmp_path):
- shutil.rmtree(tmp_path)
- os.makedirs(tmp_path)
-
- try:
- tmp_flag_file_1 = open(tmp_path + '/UnitTestFile1.tst', 'w')
- tmp_flag_file_2 = open(tmp_path + '/UnitTestFile2.tst', 'w')
- tmp_flag_file_3 = open(tmp_path + '/UnitTestFile3.tst', 'w')
- tmp_flag_file_4 = open(tmp_path + '/UnitTestFile4.tst', 'w')
- except IOError, e_msg:
- print e_msg
- print 'FAIL\n File Creation problem in Unit Test'
- sys.exit(1)
-
- # put some dummy flags in our test files
- tmp_flag_file_1.write('#A Fake Comment\n')
- tmp_flag_file_1.write('--UnitTestMessage1=tempFile1!\n')
- tmp_flag_file_1.write('\n')
- tmp_flag_file_1.write('--UnitTestNumber=54321\n')
- tmp_flag_file_1.write('--noUnitTestBoolFlag\n')
- file_list = [tmp_flag_file_1.name]
- # this one includes test file 1
- tmp_flag_file_2.write('//A Different Fake Comment\n')
- tmp_flag_file_2.write('--flagfile=%s\n' % tmp_flag_file_1.name)
- tmp_flag_file_2.write('--UnitTestMessage2=setFromTempFile2\n')
- tmp_flag_file_2.write('\t\t\n')
- tmp_flag_file_2.write('--UnitTestNumber=6789a\n')
- file_list.append(tmp_flag_file_2.name)
- # this file points to itself
- tmp_flag_file_3.write('--flagfile=%s\n' % tmp_flag_file_3.name)
- tmp_flag_file_3.write('--UnitTestMessage1=setFromTempFile3\n')
- tmp_flag_file_3.write('#YAFC\n')
- tmp_flag_file_3.write('--UnitTestBoolFlag\n')
- file_list.append(tmp_flag_file_3.name)
- # this file is unreadable
- tmp_flag_file_4.write('--flagfile=%s\n' % tmp_flag_file_3.name)
- tmp_flag_file_4.write('--UnitTestMessage1=setFromTempFile3\n')
- tmp_flag_file_4.write('--UnitTestMessage1=setFromTempFile3\n')
- os.chmod(tmp_path + '/UnitTestFile4.tst', 0)
- file_list.append(tmp_flag_file_4.name)
-
- tmp_flag_file_1.close()
- tmp_flag_file_2.close()
- tmp_flag_file_3.close()
- tmp_flag_file_4.close()
-
- self.files_to_delete = file_list
-
- return file_list # these are just the file names
- # end SetupFiles def
-
- def _RemoveTestFiles(self):
- """Closes the files we just created. tempfile deletes them for us """
- for file_name in self.files_to_delete:
- try:
- os.remove(file_name)
- except OSError, e_msg:
- print '%s\n, Problem deleting test file' % e_msg
- #end RemoveTestFiles def
-
- def _ReadFlagsFromFiles(self, argv, force_gnu):
- return argv[:1] + self.flag_values.ReadFlagsFromFiles(argv[1:],
- force_gnu=force_gnu)
-
- #### Flagfile Unit Tests ####
- def testMethod_flagfiles_1(self):
- """ Test trivial case with no flagfile based options. """
- fake_cmd_line = 'fooScript --UnitTestBoolFlag'
- fake_argv = fake_cmd_line.split(' ')
- self.flag_values(fake_argv)
- self.assertEqual( self.flag_values.UnitTestBoolFlag, 1)
- self.assertEqual( fake_argv, self._ReadFlagsFromFiles(fake_argv, False))
-
- # end testMethodOne
-
- def testMethod_flagfiles_2(self):
- """Tests parsing one file + arguments off simulated argv"""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = 'fooScript --q --flagfile=%s' % tmp_files[0]
- fake_argv = fake_cmd_line.split(' ')
-
- # We should see the original cmd line with the file's contents spliced in.
- # Flags from the file will appear in the order order they are sepcified
- # in the file, in the same position as the flagfile argument.
- expected_results = ['fooScript',
- '--q',
- '--UnitTestMessage1=tempFile1!',
- '--UnitTestNumber=54321',
- '--noUnitTestBoolFlag']
- test_results = self._ReadFlagsFromFiles(fake_argv, False)
- self.assertEqual(expected_results, test_results)
- # end testTwo def
-
- def testMethod_flagfiles_3(self):
- """Tests parsing nested files + arguments of simulated argv"""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --UnitTestNumber=77 --flagfile=%s'
- % tmp_files[1])
- fake_argv = fake_cmd_line.split(' ')
-
- expected_results = ['fooScript',
- '--UnitTestNumber=77',
- '--UnitTestMessage1=tempFile1!',
- '--UnitTestNumber=54321',
- '--noUnitTestBoolFlag',
- '--UnitTestMessage2=setFromTempFile2',
- '--UnitTestNumber=6789a']
- test_results = self._ReadFlagsFromFiles(fake_argv, False)
- self.assertEqual(expected_results, test_results)
- # end testThree def
-
- def testMethod_flagfiles_4(self):
- """Tests parsing self-referential files + arguments of simulated argv.
- This test should print a warning to stderr of some sort.
- """
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --flagfile=%s --noUnitTestBoolFlag'
- % tmp_files[2])
- fake_argv = fake_cmd_line.split(' ')
- expected_results = ['fooScript',
- '--UnitTestMessage1=setFromTempFile3',
- '--UnitTestBoolFlag',
- '--noUnitTestBoolFlag' ]
-
- test_results = self._ReadFlagsFromFiles(fake_argv, False)
- self.assertEqual(expected_results, test_results)
-
- def testMethod_flagfiles_5(self):
- """Test that --flagfile parsing respects the '--' end-of-options marker."""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = 'fooScript --SomeFlag -- --flagfile=%s' % tmp_files[0]
- fake_argv = fake_cmd_line.split(' ')
- expected_results = ['fooScript',
- '--SomeFlag',
- '--',
- '--flagfile=%s' % tmp_files[0]]
-
- test_results = self._ReadFlagsFromFiles(fake_argv, False)
- self.assertEqual(expected_results, test_results)
-
- def testMethod_flagfiles_6(self):
- """Test that --flagfile parsing stops at non-options (non-GNU behavior)."""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --SomeFlag some_arg --flagfile=%s'
- % tmp_files[0])
- fake_argv = fake_cmd_line.split(' ')
- expected_results = ['fooScript',
- '--SomeFlag',
- 'some_arg',
- '--flagfile=%s' % tmp_files[0]]
-
- test_results = self._ReadFlagsFromFiles(fake_argv, False)
- self.assertEqual(expected_results, test_results)
-
- def testMethod_flagfiles_7(self):
- """Test that --flagfile parsing skips over a non-option (GNU behavior)."""
- self.flag_values.UseGnuGetOpt()
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --SomeFlag some_arg --flagfile=%s'
- % tmp_files[0])
- fake_argv = fake_cmd_line.split(' ')
- expected_results = ['fooScript',
- '--SomeFlag',
- 'some_arg',
- '--UnitTestMessage1=tempFile1!',
- '--UnitTestNumber=54321',
- '--noUnitTestBoolFlag']
-
- test_results = self._ReadFlagsFromFiles(fake_argv, False)
- self.assertEqual(expected_results, test_results)
-
- def testMethod_flagfiles_8(self):
- """Test that --flagfile parsing respects force_gnu=True."""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --SomeFlag some_arg --flagfile=%s'
- % tmp_files[0])
- fake_argv = fake_cmd_line.split(' ')
- expected_results = ['fooScript',
- '--SomeFlag',
- 'some_arg',
- '--UnitTestMessage1=tempFile1!',
- '--UnitTestNumber=54321',
- '--noUnitTestBoolFlag']
-
- test_results = self._ReadFlagsFromFiles(fake_argv, True)
- self.assertEqual(expected_results, test_results)
-
- def testMethod_flagfiles_NoPermissions(self):
- """Test that --flagfile raises except on file that is unreadable."""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --SomeFlag some_arg --flagfile=%s'
- % tmp_files[3])
- fake_argv = fake_cmd_line.split(' ')
- self.assertRaises(gflags.CantOpenFlagFileError,
- self._ReadFlagsFromFiles, fake_argv, True)
-
- def testMethod_flagfiles_NotFound(self):
- """Test that --flagfile raises except on file that does not exist."""
- tmp_files = self._SetupTestFiles()
- # specify our temp file on the fake cmd line
- fake_cmd_line = ('fooScript --SomeFlag some_arg --flagfile=%sNOTEXIST'
- % tmp_files[3])
- fake_argv = fake_cmd_line.split(' ')
- self.assertRaises(gflags.CantOpenFlagFileError,
- self._ReadFlagsFromFiles, fake_argv, True)
-
- def test_flagfiles_user_path_expansion(self):
- """Test that user directory referenced paths (ie. ~/foo) are correctly
- expanded. This test depends on whatever account's running the unit test
- to have read/write access to their own home directory, otherwise it'll
- FAIL.
- """
- fake_flagfile_item_style_1 = '--flagfile=~/foo.file'
- fake_flagfile_item_style_2 = '-flagfile=~/foo.file'
-
- expected_results = os.path.expanduser('~/foo.file')
-
- test_results = self.flag_values.ExtractFilename(fake_flagfile_item_style_1)
- self.assertEqual(expected_results, test_results)
-
- test_results = self.flag_values.ExtractFilename(fake_flagfile_item_style_2)
- self.assertEqual(expected_results, test_results)
-
- # end testFour def
-
- def test_no_touchy_non_flags(self):
- """
- Test that the flags parser does not mutilate arguments which are
- not supposed to be flags
- """
- fake_argv = ['fooScript', '--UnitTestBoolFlag',
- 'command', '--command_arg1', '--UnitTestBoom', '--UnitTestB']
- argv = self.flag_values(fake_argv)
- self.assertEqual(argv, fake_argv[:1] + fake_argv[2:])
-
- def test_parse_flags_after_args_if_using_gnu_getopt(self):
- """
- Test that flags given after arguments are parsed if using gnu_getopt.
- """
- self.flag_values.UseGnuGetOpt()
- fake_argv = ['fooScript', '--UnitTestBoolFlag',
- 'command', '--UnitTestB']
- argv = self.flag_values(fake_argv)
- self.assertEqual(argv, ['fooScript', 'command'])
-
- def test_SetDefault(self):
- """
- Test changing flag defaults.
- """
- # Test that SetDefault changes both the default and the value,
- # and that the value is changed when one is given as an option.
- self.flag_values['UnitTestMessage1'].SetDefault('New value')
- self.assertEqual(self.flag_values.UnitTestMessage1, 'New value')
- self.assertEqual(self.flag_values['UnitTestMessage1'].default_as_str,
- "'New value'")
- self.flag_values([ 'dummyscript', '--UnitTestMessage1=Newer value' ])
- self.assertEqual(self.flag_values.UnitTestMessage1, 'Newer value')
-
- # Test that setting the default to None works correctly.
- self.flag_values['UnitTestNumber'].SetDefault(None)
- self.assertEqual(self.flag_values.UnitTestNumber, None)
- self.assertEqual(self.flag_values['UnitTestNumber'].default_as_str, None)
- self.flag_values([ 'dummyscript', '--UnitTestNumber=56' ])
- self.assertEqual(self.flag_values.UnitTestNumber, 56)
-
- # Test that setting the default to zero works correctly.
- self.flag_values['UnitTestNumber'].SetDefault(0)
- self.assertEqual(self.flag_values.UnitTestNumber, 0)
- self.assertEqual(self.flag_values['UnitTestNumber'].default_as_str, "'0'")
- self.flag_values([ 'dummyscript', '--UnitTestNumber=56' ])
- self.assertEqual(self.flag_values.UnitTestNumber, 56)
-
- # Test that setting the default to "" works correctly.
- self.flag_values['UnitTestMessage1'].SetDefault("")
- self.assertEqual(self.flag_values.UnitTestMessage1, "")
- self.assertEqual(self.flag_values['UnitTestMessage1'].default_as_str, "''")
- self.flag_values([ 'dummyscript', '--UnitTestMessage1=fifty-six' ])
- self.assertEqual(self.flag_values.UnitTestMessage1, "fifty-six")
-
- # Test that setting the default to false works correctly.
- self.flag_values['UnitTestBoolFlag'].SetDefault(False)
- self.assertEqual(self.flag_values.UnitTestBoolFlag, False)
- self.assertEqual(self.flag_values['UnitTestBoolFlag'].default_as_str,
- "'false'")
- self.flag_values([ 'dummyscript', '--UnitTestBoolFlag=true' ])
- self.assertEqual(self.flag_values.UnitTestBoolFlag, True)
-
- # Test that setting a list default works correctly.
- self.flag_values['UnitTestList'].SetDefault('4,5,6')
- self.assertEqual(self.flag_values.UnitTestList, ['4', '5', '6'])
- self.assertEqual(self.flag_values['UnitTestList'].default_as_str, "'4,5,6'")
- self.flag_values([ 'dummyscript', '--UnitTestList=7,8,9' ])
- self.assertEqual(self.flag_values.UnitTestList, ['7', '8', '9'])
-
- # Test that setting invalid defaults raises exceptions
- self.assertRaises(gflags.IllegalFlagValue,
- self.flag_values['UnitTestNumber'].SetDefault, 'oops')
- self.assertRaises(gflags.IllegalFlagValue,
- self.flag_values.SetDefault, 'UnitTestNumber', -1)
-
-
-class FlagsParsingTest(googletest.TestCase):
- """Testing different aspects of parsing: '-f' vs '--flag', etc."""
-
- def setUp(self):
- self.flag_values = gflags.FlagValues()
-
- def testMethod_ShortestUniquePrefixes(self):
- """Test FlagValues.ShortestUniquePrefixes"""
-
- gflags.DEFINE_string('a', '', '', flag_values=self.flag_values)
- gflags.DEFINE_string('abc', '', '', flag_values=self.flag_values)
- gflags.DEFINE_string('common_a_string', '', '', flag_values=self.flag_values)
- gflags.DEFINE_boolean('common_b_boolean', 0, '',
- flag_values=self.flag_values)
- gflags.DEFINE_boolean('common_c_boolean', 0, '',
- flag_values=self.flag_values)
- gflags.DEFINE_boolean('common', 0, '', flag_values=self.flag_values)
- gflags.DEFINE_integer('commonly', 0, '', flag_values=self.flag_values)
- gflags.DEFINE_boolean('zz', 0, '', flag_values=self.flag_values)
- gflags.DEFINE_integer('nozz', 0, '', flag_values=self.flag_values)
-
- shorter_flags = self.flag_values.ShortestUniquePrefixes(
- self.flag_values.FlagDict())
-
- expected_results = {'nocommon_b_boolean': 'nocommon_b',
- 'common_c_boolean': 'common_c',
- 'common_b_boolean': 'common_b',
- 'a': 'a',
- 'abc': 'ab',
- 'zz': 'z',
- 'nozz': 'nozz',
- 'common_a_string': 'common_a',
- 'commonly': 'commonl',
- 'nocommon_c_boolean': 'nocommon_c',
- 'nocommon': 'nocommon',
- 'common': 'common'}
-
- for name, shorter in expected_results.iteritems():
- self.assertEquals(shorter_flags[name], shorter)
-
- self.flag_values.__delattr__('a')
- self.flag_values.__delattr__('abc')
- self.flag_values.__delattr__('common_a_string')
- self.flag_values.__delattr__('common_b_boolean')
- self.flag_values.__delattr__('common_c_boolean')
- self.flag_values.__delattr__('common')
- self.flag_values.__delattr__('commonly')
- self.flag_values.__delattr__('zz')
- self.flag_values.__delattr__('nozz')
-
- def test_twodasharg_first(self):
- gflags.DEFINE_string("twodash_name", "Bob", "namehelp",
- flag_values=self.flag_values)
- gflags.DEFINE_string("twodash_blame", "Rob", "blamehelp",
- flag_values=self.flag_values)
- argv = ('./program',
- '--',
- '--twodash_name=Harry')
- argv = self.flag_values(argv)
- self.assertEqual('Bob', self.flag_values.twodash_name)
- self.assertEqual(argv[1], '--twodash_name=Harry')
-
- def test_twodasharg_middle(self):
- gflags.DEFINE_string("twodash2_name", "Bob", "namehelp",
- flag_values=self.flag_values)
- gflags.DEFINE_string("twodash2_blame", "Rob", "blamehelp",
- flag_values=self.flag_values)
- argv = ('./program',
- '--twodash2_blame=Larry',
- '--',
- '--twodash2_name=Harry')
- argv = self.flag_values(argv)
- self.assertEqual('Bob', self.flag_values.twodash2_name)
- self.assertEqual('Larry', self.flag_values.twodash2_blame)
- self.assertEqual(argv[1], '--twodash2_name=Harry')
-
- def test_onedasharg_first(self):
- gflags.DEFINE_string("onedash_name", "Bob", "namehelp",
- flag_values=self.flag_values)
- gflags.DEFINE_string("onedash_blame", "Rob", "blamehelp",
- flag_values=self.flag_values)
- argv = ('./program',
- '-',
- '--onedash_name=Harry')
- argv = self.flag_values(argv)
- self.assertEqual(argv[1], '-')
- # TODO(csilvers): we should still parse --onedash_name=Harry as a
- # flag, but currently we don't (we stop flag processing as soon as
- # we see the first non-flag).
- # - This requires gnu_getopt from Python 2.3+ see FLAGS.UseGnuGetOpt()
-
- def test_unrecognized_flags(self):
- gflags.DEFINE_string("name", "Bob", "namehelp", flag_values=self.flag_values)
- # Unknown flag --nosuchflag
- try:
- argv = ('./program', '--nosuchflag', '--name=Bob', 'extra')
- self.flag_values(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'nosuchflag'
- assert e.flagvalue == '--nosuchflag'
-
- # Unknown flag -w (short option)
- try:
- argv = ('./program', '-w', '--name=Bob', 'extra')
- self.flag_values(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'w'
- assert e.flagvalue == '-w'
-
- # Unknown flag --nosuchflagwithparam=foo
- try:
- argv = ('./program', '--nosuchflagwithparam=foo', '--name=Bob', 'extra')
- self.flag_values(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'nosuchflagwithparam'
- assert e.flagvalue == '--nosuchflagwithparam=foo'
-
- # Allow unknown flag --nosuchflag if specified with undefok
- argv = ('./program', '--nosuchflag', '--name=Bob',
- '--undefok=nosuchflag', 'extra')
- argv = self.flag_values(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
-
- # Allow unknown flag --noboolflag if undefok=boolflag is specified
- argv = ('./program', '--noboolflag', '--name=Bob',
- '--undefok=boolflag', 'extra')
- argv = self.flag_values(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
-
- # But not if the flagname is misspelled:
- try:
- argv = ('./program', '--nosuchflag', '--name=Bob',
- '--undefok=nosuchfla', 'extra')
- self.flag_values(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'nosuchflag'
-
- try:
- argv = ('./program', '--nosuchflag', '--name=Bob',
- '--undefok=nosuchflagg', 'extra')
- self.flag_values(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'nosuchflag'
-
- # Allow unknown short flag -w if specified with undefok
- argv = ('./program', '-w', '--name=Bob', '--undefok=w', 'extra')
- argv = self.flag_values(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
-
- # Allow unknown flag --nosuchflagwithparam=foo if specified
- # with undefok
- argv = ('./program', '--nosuchflagwithparam=foo', '--name=Bob',
- '--undefok=nosuchflagwithparam', 'extra')
- argv = self.flag_values(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
-
- # Even if undefok specifies multiple flags
- argv = ('./program', '--nosuchflag', '-w', '--nosuchflagwithparam=foo',
- '--name=Bob',
- '--undefok=nosuchflag,w,nosuchflagwithparam',
- 'extra')
- argv = self.flag_values(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
-
- # However, not if undefok doesn't specify the flag
- try:
- argv = ('./program', '--nosuchflag', '--name=Bob',
- '--undefok=another_such', 'extra')
- self.flag_values(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'nosuchflag'
-
- # Make sure --undefok doesn't mask other option errors.
- try:
- # Provide an option requiring a parameter but not giving it one.
- argv = ('./program', '--undefok=name', '--name')
- self.flag_values(argv)
- raise AssertionError("Missing option parameter exception not raised")
- except gflags.UnrecognizedFlag:
- raise AssertionError("Wrong kind of error exception raised")
- except gflags.FlagsError:
- pass
-
- # Test --undefok <list>
- argv = ('./program', '--nosuchflag', '-w', '--nosuchflagwithparam=foo',
- '--name=Bob',
- '--undefok',
- 'nosuchflag,w,nosuchflagwithparam',
- 'extra')
- argv = self.flag_values(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
-
-
-class NonGlobalFlagsTest(googletest.TestCase):
-
- def test_nonglobal_flags(self):
- """Test use of non-global FlagValues"""
- nonglobal_flags = gflags.FlagValues()
- gflags.DEFINE_string("nonglobal_flag", "Bob", "flaghelp", nonglobal_flags)
- argv = ('./program',
- '--nonglobal_flag=Mary',
- 'extra')
- argv = nonglobal_flags(argv)
- assert len(argv) == 2, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
- assert argv[1]=='extra', "extra argument not preserved"
- assert nonglobal_flags['nonglobal_flag'].value == 'Mary'
-
- def test_unrecognized_nonglobal_flags(self):
- """Test unrecognized non-global flags"""
- nonglobal_flags = gflags.FlagValues()
- argv = ('./program',
- '--nosuchflag')
- try:
- argv = nonglobal_flags(argv)
- raise AssertionError("Unknown flag exception not raised")
- except gflags.UnrecognizedFlag, e:
- assert e.flagname == 'nosuchflag'
- pass
-
- argv = ('./program',
- '--nosuchflag',
- '--undefok=nosuchflag')
-
- argv = nonglobal_flags(argv)
- assert len(argv) == 1, "wrong number of arguments pulled"
- assert argv[0]=='./program', "program name not preserved"
-
- def test_create_flag_errors(self):
- # Since the exception classes are exposed, nothing stops users
- # from creating their own instances. This test makes sure that
- # people modifying the flags module understand that the external
- # mechanisms for creating the exceptions should continue to work.
- e = gflags.FlagsError()
- e = gflags.FlagsError("message")
- e = gflags.DuplicateFlag()
- e = gflags.DuplicateFlag("message")
- e = gflags.IllegalFlagValue()
- e = gflags.IllegalFlagValue("message")
- e = gflags.UnrecognizedFlag()
- e = gflags.UnrecognizedFlag("message")
-
- def testFlagValuesDelAttr(self):
- """Checks that del self.flag_values.flag_id works."""
- default_value = 'default value for testFlagValuesDelAttr'
- # 1. Declare and delete a flag with no short name.
- flag_values = gflags.FlagValues()
- gflags.DEFINE_string('delattr_foo', default_value, 'A simple flag.',
- flag_values=flag_values)
- self.assertEquals(flag_values.delattr_foo, default_value)
- flag_obj = flag_values['delattr_foo']
- # We also check that _FlagIsRegistered works as expected :)
- self.assertTrue(flag_values._FlagIsRegistered(flag_obj))
- del flag_values.delattr_foo
- self.assertFalse('delattr_foo' in flag_values.FlagDict())
- self.assertFalse(flag_values._FlagIsRegistered(flag_obj))
- # If the previous del FLAGS.delattr_foo did not work properly, the
- # next definition will trigger a redefinition error.
- gflags.DEFINE_integer('delattr_foo', 3, 'A simple flag.',
- flag_values=flag_values)
- del flag_values.delattr_foo
-
- self.assertFalse('delattr_foo' in flag_values.RegisteredFlags())
-
- # 2. Declare and delete a flag with a short name.
- gflags.DEFINE_string('delattr_bar', default_value, 'flag with short name',
- short_name='x5', flag_values=flag_values)
- flag_obj = flag_values['delattr_bar']
- self.assertTrue(flag_values._FlagIsRegistered(flag_obj))
- del flag_values.x5
- self.assertTrue(flag_values._FlagIsRegistered(flag_obj))
- del flag_values.delattr_bar
- self.assertFalse(flag_values._FlagIsRegistered(flag_obj))
-
- # 3. Just like 2, but del flag_values.name last
- gflags.DEFINE_string('delattr_bar', default_value, 'flag with short name',
- short_name='x5', flag_values=flag_values)
- flag_obj = flag_values['delattr_bar']
- self.assertTrue(flag_values._FlagIsRegistered(flag_obj))
- del flag_values.delattr_bar
- self.assertTrue(flag_values._FlagIsRegistered(flag_obj))
- del flag_values.x5
- self.assertFalse(flag_values._FlagIsRegistered(flag_obj))
-
- self.assertFalse('delattr_bar' in flag_values.RegisteredFlags())
- self.assertFalse('x5' in flag_values.RegisteredFlags())
-
-
-class KeyFlagsTest(googletest.TestCase):
-
- def setUp(self):
- self.flag_values = gflags.FlagValues()
-
- def _GetNamesOfDefinedFlags(self, module, flag_values):
- """Returns the list of names of flags defined by a module.
-
- Auxiliary for the testKeyFlags* methods.
-
- Args:
- module: A module object or a string module name.
- flag_values: A FlagValues object.
-
- Returns:
- A list of strings.
- """
- return [f.name for f in flag_values._GetFlagsDefinedByModule(module)]
-
- def _GetNamesOfKeyFlags(self, module, flag_values):
- """Returns the list of names of key flags for a module.
-
- Auxiliary for the testKeyFlags* methods.
-
- Args:
- module: A module object or a string module name.
- flag_values: A FlagValues object.
-
- Returns:
- A list of strings.
- """
- return [f.name for f in flag_values._GetKeyFlagsForModule(module)]
-
- def _AssertListsHaveSameElements(self, list_1, list_2):
- # Checks that two lists have the same elements with the same
- # multiplicity, in possibly different order.
- list_1 = list(list_1)
- list_1.sort()
- list_2 = list(list_2)
- list_2.sort()
- self.assertListEqual(list_1, list_2)
-
- def testKeyFlags(self):
- # Before starting any testing, make sure no flags are already
- # defined for module_foo and module_bar.
- self.assertListEqual(self._GetNamesOfKeyFlags(module_foo, self.flag_values),
- [])
- self.assertListEqual(self._GetNamesOfKeyFlags(module_bar, self.flag_values),
- [])
- self.assertListEqual(self._GetNamesOfDefinedFlags(module_foo,
- self.flag_values),
- [])
- self.assertListEqual(self._GetNamesOfDefinedFlags(module_bar,
- self.flag_values),
- [])
-
- # Defines a few flags in module_foo and module_bar.
- module_foo.DefineFlags(flag_values=self.flag_values)
-
- try:
- # Part 1. Check that all flags defined by module_foo are key for
- # that module, and similarly for module_bar.
- for module in [module_foo, module_bar]:
- self._AssertListsHaveSameElements(
- self.flag_values._GetFlagsDefinedByModule(module),
- self.flag_values._GetKeyFlagsForModule(module))
- # Also check that each module defined the expected flags.
- self._AssertListsHaveSameElements(
- self._GetNamesOfDefinedFlags(module, self.flag_values),
- module.NamesOfDefinedFlags())
-
- # Part 2. Check that gflags.DECLARE_key_flag works fine.
- # Declare that some flags from module_bar are key for
- # module_foo.
- module_foo.DeclareKeyFlags(flag_values=self.flag_values)
-
- # Check that module_foo has the expected list of defined flags.
- self._AssertListsHaveSameElements(
- self._GetNamesOfDefinedFlags(module_foo, self.flag_values),
- module_foo.NamesOfDefinedFlags())
-
- # Check that module_foo has the expected list of key flags.
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(module_foo, self.flag_values),
- module_foo.NamesOfDeclaredKeyFlags())
-
- # Part 3. Check that gflags.ADOPT_module_key_flags works fine.
- # Trigger a call to gflags.ADOPT_module_key_flags(module_bar)
- # inside module_foo. This should declare a few more key
- # flags in module_foo.
- module_foo.DeclareExtraKeyFlags(flag_values=self.flag_values)
-
- # Check that module_foo has the expected list of key flags.
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(module_foo, self.flag_values),
- module_foo.NamesOfDeclaredKeyFlags() +
- module_foo.NamesOfDeclaredExtraKeyFlags())
- finally:
- module_foo.RemoveFlags(flag_values=self.flag_values)
-
- def testKeyFlagsWithNonDefaultFlagValuesObject(self):
- # Check that key flags work even when we use a FlagValues object
- # that is not the default gflags.self.flag_values object. Otherwise, this
- # test is similar to testKeyFlags, but it uses only module_bar.
- # The other test module (module_foo) uses only the default values
- # for the flag_values keyword arguments. This way, testKeyFlags
- # and this method test both the default FlagValues, the explicitly
- # specified one, and a mixed usage of the two.
-
- # A brand-new FlagValues object, to use instead of gflags.self.flag_values.
- fv = gflags.FlagValues()
-
- # Before starting any testing, make sure no flags are already
- # defined for module_foo and module_bar.
- self.assertListEqual(
- self._GetNamesOfKeyFlags(module_bar, fv),
- [])
- self.assertListEqual(
- self._GetNamesOfDefinedFlags(module_bar, fv),
- [])
-
- module_bar.DefineFlags(flag_values=fv)
-
- # Check that all flags defined by module_bar are key for that
- # module, and that module_bar defined the expected flags.
- self._AssertListsHaveSameElements(
- fv._GetFlagsDefinedByModule(module_bar),
- fv._GetKeyFlagsForModule(module_bar))
- self._AssertListsHaveSameElements(
- self._GetNamesOfDefinedFlags(module_bar, fv),
- module_bar.NamesOfDefinedFlags())
-
- # Pick two flags from module_bar, declare them as key for the
- # current (i.e., main) module (via gflags.DECLARE_key_flag), and
- # check that we get the expected effect. The important thing is
- # that we always use flags_values=fv (instead of the default
- # self.flag_values).
- main_module = gflags._GetMainModule()
- names_of_flags_defined_by_bar = module_bar.NamesOfDefinedFlags()
- flag_name_0 = names_of_flags_defined_by_bar[0]
- flag_name_2 = names_of_flags_defined_by_bar[2]
-
- gflags.DECLARE_key_flag(flag_name_0, flag_values=fv)
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(main_module, fv),
- [flag_name_0])
-
- gflags.DECLARE_key_flag(flag_name_2, flag_values=fv)
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(main_module, fv),
- [flag_name_0, flag_name_2])
-
- # Try with a special (not user-defined) flag too:
- gflags.DECLARE_key_flag('undefok', flag_values=fv)
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(main_module, fv),
- [flag_name_0, flag_name_2, 'undefok'])
-
- gflags.ADOPT_module_key_flags(module_bar, fv)
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(main_module, fv),
- names_of_flags_defined_by_bar + ['undefok'])
-
- # Adopt key flags from the flags module itself.
- gflags.ADOPT_module_key_flags(gflags, flag_values=fv)
- self._AssertListsHaveSameElements(
- self._GetNamesOfKeyFlags(main_module, fv),
- names_of_flags_defined_by_bar + ['flagfile', 'undefok'])
-
- def testMainModuleHelpWithKeyFlags(self):
- # Similar to test_main_module_help, but this time we make sure to
- # declare some key flags.
-
- # Safety check that the main module does not declare any flags
- # at the beginning of this test.
- expected_help = ''
- self.assertMultiLineEqual(expected_help, self.flag_values.MainModuleHelp())
-
- # Define one flag in this main module and some flags in modules
- # a and b. Also declare one flag from module a and one flag
- # from module b as key flags for the main module.
- gflags.DEFINE_integer('main_module_int_fg', 1,
- 'Integer flag in the main module.',
- flag_values=self.flag_values)
-
- try:
- main_module_int_fg_help = (
- " --main_module_int_fg: Integer flag in the main module.\n"
- " (default: '1')\n"
- " (an integer)")
-
- expected_help += "\n%s:\n%s" % (sys.argv[0], main_module_int_fg_help)
- self.assertMultiLineEqual(expected_help,
- self.flag_values.MainModuleHelp())
-
- # The following call should be a no-op: any flag declared by a
- # module is automatically key for that module.
- gflags.DECLARE_key_flag('main_module_int_fg', flag_values=self.flag_values)
- self.assertMultiLineEqual(expected_help,
- self.flag_values.MainModuleHelp())
-
- # The definition of a few flags in an imported module should not
- # change the main module help.
- module_foo.DefineFlags(flag_values=self.flag_values)
- self.assertMultiLineEqual(expected_help,
- self.flag_values.MainModuleHelp())
-
- gflags.DECLARE_key_flag('tmod_foo_bool', flag_values=self.flag_values)
- tmod_foo_bool_help = (
- " --[no]tmod_foo_bool: Boolean flag from module foo.\n"
- " (default: 'true')")
- expected_help += "\n" + tmod_foo_bool_help
- self.assertMultiLineEqual(expected_help,
- self.flag_values.MainModuleHelp())
-
- gflags.DECLARE_key_flag('tmod_bar_z', flag_values=self.flag_values)
- tmod_bar_z_help = (
- " --[no]tmod_bar_z: Another boolean flag from module bar.\n"
- " (default: 'false')")
- # Unfortunately, there is some flag sorting inside
- # MainModuleHelp, so we can't keep incrementally extending
- # the expected_help string ...
- expected_help = ("\n%s:\n%s\n%s\n%s" %
- (sys.argv[0],
- main_module_int_fg_help,
- tmod_bar_z_help,
- tmod_foo_bool_help))
- self.assertMultiLineEqual(self.flag_values.MainModuleHelp(),
- expected_help)
-
- finally:
- # At the end, delete all the flag information we created.
- self.flag_values.__delattr__('main_module_int_fg')
- module_foo.RemoveFlags(flag_values=self.flag_values)
-
- def test_ADOPT_module_key_flags(self):
- # Check that ADOPT_module_key_flags raises an exception when
- # called with a module name (as opposed to a module object).
- self.assertRaises(gflags.FlagsError,
- gflags.ADOPT_module_key_flags,
- 'pyglib.app')
-
-
-class GetCallingModuleTest(googletest.TestCase):
- """Test whether we correctly determine the module which defines the flag."""
-
- def test_GetCallingModule(self):
- self.assertEqual(gflags._GetCallingModule(), sys.argv[0])
- self.assertEqual(
- module_foo.GetModuleName(),
- 'flags_modules_for_testing.module_foo')
- self.assertEqual(
- module_bar.GetModuleName(),
- 'flags_modules_for_testing.module_bar')
-
- # We execute the following exec statements for their side-effect
- # (i.e., not raising an error). They emphasize the case that not
- # all code resides in one of the imported modules: Python is a
- # really dynamic language, where we can dynamically construct some
- # code and execute it.
- code = ("import gflags\n"
- "module_name = gflags._GetCallingModule()")
- exec(code)
-
- # Next two exec statements executes code with a global environment
- # that is different from the global environment of any imported
- # module.
- exec(code, {})
- # vars(self) returns a dictionary corresponding to the symbol
- # table of the self object. dict(...) makes a distinct copy of
- # this dictionary, such that any new symbol definition by the
- # exec-ed code (e.g., import flags, module_name = ...) does not
- # affect the symbol table of self.
- exec(code, dict(vars(self)))
-
- # Next test is actually more involved: it checks not only that
- # _GetCallingModule does not crash inside exec code, it also checks
- # that it returns the expected value: the code executed via exec
- # code is treated as being executed by the current module. We
- # check it twice: first time by executing exec from the main
- # module, second time by executing it from module_bar.
- global_dict = {}
- exec(code, global_dict)
- self.assertEqual(global_dict['module_name'],
- sys.argv[0])
-
- global_dict = {}
- module_bar.ExecuteCode(code, global_dict)
- self.assertEqual(
- global_dict['module_name'],
- 'flags_modules_for_testing.module_bar')
-
- def test_GetCallingModuleWithIteritemsError(self):
- # This test checks that _GetCallingModule is using
- # sys.modules.items(), instead of .iteritems().
- orig_sys_modules = sys.modules
-
- # Mock sys.modules: simulates error produced by importing a module
- # in paralel with our iteration over sys.modules.iteritems().
- class SysModulesMock(dict):
- def __init__(self, original_content):
- dict.__init__(self, original_content)
-
- def iteritems(self):
- # Any dictionary method is fine, but not .iteritems().
- raise RuntimeError('dictionary changed size during iteration')
-
- sys.modules = SysModulesMock(orig_sys_modules)
- try:
- # _GetCallingModule should still work as expected:
- self.assertEqual(gflags._GetCallingModule(), sys.argv[0])
- self.assertEqual(
- module_foo.GetModuleName(),
- 'flags_modules_for_testing.module_foo')
- finally:
- sys.modules = orig_sys_modules
-
-
-class FindModuleTest(googletest.TestCase):
- """Testing methods that find a module that defines a given flag."""
-
- def testFindModuleDefiningFlag(self):
- self.assertEqual('default', FLAGS.FindModuleDefiningFlag(
- '__NON_EXISTENT_FLAG__', 'default'))
- self.assertEqual(
- module_baz.__name__, FLAGS.FindModuleDefiningFlag('tmod_baz_x'))
-
- def testFindModuleIdDefiningFlag(self):
- self.assertEqual('default', FLAGS.FindModuleIdDefiningFlag(
- '__NON_EXISTENT_FLAG__', 'default'))
- self.assertEqual(
- id(module_baz), FLAGS.FindModuleIdDefiningFlag('tmod_baz_x'))
-
-
-class FlagsErrorMessagesTest(googletest.TestCase):
- """Testing special cases for integer and float flags error messages."""
-
- def setUp(self):
- # make sure we are using the old, stupid way of parsing flags.
- self.flag_values = gflags.FlagValues()
- self.flag_values.UseGnuGetOpt(False)
-
- def testIntegerErrorText(self):
- # Make sure we get proper error text
- gflags.DEFINE_integer('positive', 4, 'non-negative flag', lower_bound=1,
- flag_values=self.flag_values)
- gflags.DEFINE_integer('non_negative', 4, 'positive flag', lower_bound=0,
- flag_values=self.flag_values)
- gflags.DEFINE_integer('negative', -4, 'negative flag', upper_bound=-1,
- flag_values=self.flag_values)
- gflags.DEFINE_integer('non_positive', -4, 'non-positive flag', upper_bound=0,
- flag_values=self.flag_values)
- gflags.DEFINE_integer('greater', 19, 'greater-than flag', lower_bound=4,
- flag_values=self.flag_values)
- gflags.DEFINE_integer('smaller', -19, 'smaller-than flag', upper_bound=4,
- flag_values=self.flag_values)
- gflags.DEFINE_integer('usual', 4, 'usual flag', lower_bound=0,
- upper_bound=10000, flag_values=self.flag_values)
- gflags.DEFINE_integer('another_usual', 0, 'usual flag', lower_bound=-1,
- upper_bound=1, flag_values=self.flag_values)
-
- self._CheckErrorMessage('positive', -4, 'a positive integer')
- self._CheckErrorMessage('non_negative', -4, 'a non-negative integer')
- self._CheckErrorMessage('negative', 0, 'a negative integer')
- self._CheckErrorMessage('non_positive', 4, 'a non-positive integer')
- self._CheckErrorMessage('usual', -4, 'an integer in the range [0, 10000]')
- self._CheckErrorMessage('another_usual', 4,
- 'an integer in the range [-1, 1]')
- self._CheckErrorMessage('greater', -5, 'integer >= 4')
- self._CheckErrorMessage('smaller', 5, 'integer <= 4')
-
- def testFloatErrorText(self):
- gflags.DEFINE_float('positive', 4, 'non-negative flag', lower_bound=1,
- flag_values=self.flag_values)
- gflags.DEFINE_float('non_negative', 4, 'positive flag', lower_bound=0,
- flag_values=self.flag_values)
- gflags.DEFINE_float('negative', -4, 'negative flag', upper_bound=-1,
- flag_values=self.flag_values)
- gflags.DEFINE_float('non_positive', -4, 'non-positive flag', upper_bound=0,
- flag_values=self.flag_values)
- gflags.DEFINE_float('greater', 19, 'greater-than flag', lower_bound=4,
- flag_values=self.flag_values)
- gflags.DEFINE_float('smaller', -19, 'smaller-than flag', upper_bound=4,
- flag_values=self.flag_values)
- gflags.DEFINE_float('usual', 4, 'usual flag', lower_bound=0,
- upper_bound=10000, flag_values=self.flag_values)
- gflags.DEFINE_float('another_usual', 0, 'usual flag', lower_bound=-1,
- upper_bound=1, flag_values=self.flag_values)
-
- self._CheckErrorMessage('positive', 0.5, 'number >= 1')
- self._CheckErrorMessage('non_negative', -4.0, 'a non-negative number')
- self._CheckErrorMessage('negative', 0.5, 'number <= -1')
- self._CheckErrorMessage('non_positive', 4.0, 'a non-positive number')
- self._CheckErrorMessage('usual', -4.0, 'a number in the range [0, 10000]')
- self._CheckErrorMessage('another_usual', 4.0,
- 'a number in the range [-1, 1]')
- self._CheckErrorMessage('smaller', 5.0, 'number <= 4')
-
- def _CheckErrorMessage(self, flag_name, flag_value, expected_message_suffix):
- """Set a flag to a given value and make sure we get expected message."""
-
- try:
- self.flag_values.__setattr__(flag_name, flag_value)
- raise AssertionError('Bounds exception not raised!')
- except gflags.IllegalFlagValue, e:
- expected = ('flag --%(name)s=%(value)s: %(value)s is not %(suffix)s' %
- {'name': flag_name, 'value': flag_value,
- 'suffix': expected_message_suffix})
- self.assertEquals(str(e), expected)
-
-
-def main():
- googletest.main()
-
-
-if __name__ == '__main__':
- main()
diff --git a/third_party/py/gflags/tests/gflags_validators_test.py b/third_party/py/gflags/tests/gflags_validators_test.py
deleted file mode 100755
index 460e6d01d9..0000000000
--- a/third_party/py/gflags/tests/gflags_validators_test.py
+++ /dev/null
@@ -1,220 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (c) 2010, Google Inc.
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following disclaimer
-# in the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Google Inc. nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-"""Testing that flags validators framework does work.
-
-This file tests that each flag validator called when it should be, and that
-failed validator will throw an exception, etc.
-"""
-
-__author__ = 'olexiy@google.com (Olexiy Oryeshko)'
-
-import gflags_googletest as googletest
-import gflags
-import gflags_validators
-
-
-class SimpleValidatorTest(googletest.TestCase):
- """Testing gflags.RegisterValidator() method."""
-
- def setUp(self):
- super(SimpleValidatorTest, self).setUp()
- self.flag_values = gflags.FlagValues()
- self.call_args = []
-
- def testSuccess(self):
- def Checker(x):
- self.call_args.append(x)
- return True
- gflags.DEFINE_integer('test_flag', None, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program')
- self.flag_values(argv)
- self.assertEquals(None, self.flag_values.test_flag)
- self.flag_values.test_flag = 2
- self.assertEquals(2, self.flag_values.test_flag)
- self.assertEquals([None, 2], self.call_args)
-
- def testDefaultValueNotUsedSuccess(self):
- def Checker(x):
- self.call_args.append(x)
- return True
- gflags.DEFINE_integer('test_flag', None, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program', '--test_flag=1')
- self.flag_values(argv)
- self.assertEquals(1, self.flag_values.test_flag)
- self.assertEquals([1], self.call_args)
-
- def testValidatorNotCalledWhenOtherFlagIsChanged(self):
- def Checker(x):
- self.call_args.append(x)
- return True
- gflags.DEFINE_integer('test_flag', 1, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.DEFINE_integer('other_flag', 2, 'Other integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program')
- self.flag_values(argv)
- self.assertEquals(1, self.flag_values.test_flag)
- self.flag_values.other_flag = 3
- self.assertEquals([1], self.call_args)
-
- def testExceptionRaisedIfCheckerFails(self):
- def Checker(x):
- self.call_args.append(x)
- return x == 1
- gflags.DEFINE_integer('test_flag', None, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program', '--test_flag=1')
- self.flag_values(argv)
- try:
- self.flag_values.test_flag = 2
- raise AssertionError('gflags.IllegalFlagValue expected')
- except gflags.IllegalFlagValue, e:
- self.assertEquals('flag --test_flag=2: Errors happen', str(e))
- self.assertEquals([1, 2], self.call_args)
-
- def testExceptionRaisedIfCheckerRaisesException(self):
- def Checker(x):
- self.call_args.append(x)
- if x == 1:
- return True
- raise gflags_validators.Error('Specific message')
- gflags.DEFINE_integer('test_flag', None, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program', '--test_flag=1')
- self.flag_values(argv)
- try:
- self.flag_values.test_flag = 2
- raise AssertionError('gflags.IllegalFlagValue expected')
- except gflags.IllegalFlagValue, e:
- self.assertEquals('flag --test_flag=2: Specific message', str(e))
- self.assertEquals([1, 2], self.call_args)
-
- def testErrorMessageWhenCheckerReturnsFalseOnStart(self):
- def Checker(x):
- self.call_args.append(x)
- return False
- gflags.DEFINE_integer('test_flag', None, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program', '--test_flag=1')
- try:
- self.flag_values(argv)
- raise AssertionError('gflags.IllegalFlagValue expected')
- except gflags.IllegalFlagValue, e:
- self.assertEquals('flag --test_flag=1: Errors happen', str(e))
- self.assertEquals([1], self.call_args)
-
- def testErrorMessageWhenCheckerRaisesExceptionOnStart(self):
- def Checker(x):
- self.call_args.append(x)
- raise gflags_validators.Error('Specific message')
- gflags.DEFINE_integer('test_flag', None, 'Usual integer flag',
- flag_values=self.flag_values)
- gflags.RegisterValidator('test_flag',
- Checker,
- message='Errors happen',
- flag_values=self.flag_values)
-
- argv = ('./program', '--test_flag=1')
- try:
- self.flag_values(argv)
- raise AssertionError('IllegalFlagValue expected')
- except gflags.IllegalFlagValue, e:
- self.assertEquals('flag --test_flag=1: Specific message', str(e))
- self.assertEquals([1], self.call_args)
-
- def testValidatorsCheckedInOrder(self):
-
- def Required(x):
- self.calls.append('Required')
- return x is not None
-
- def Even(x):
- self.calls.append('Even')
- return x % 2 == 0
-
- self.calls = []
- self._DefineFlagAndValidators(Required, Even)
- self.assertEquals(['Required', 'Even'], self.calls)
-
- self.calls = []
- self._DefineFlagAndValidators(Even, Required)
- self.assertEquals(['Even', 'Required'], self.calls)
-
- def _DefineFlagAndValidators(self, first_validator, second_validator):
- local_flags = gflags.FlagValues()
- gflags.DEFINE_integer('test_flag', 2, 'test flag', flag_values=local_flags)
- gflags.RegisterValidator('test_flag',
- first_validator,
- message='',
- flag_values=local_flags)
- gflags.RegisterValidator('test_flag',
- second_validator,
- message='',
- flag_values=local_flags)
- argv = ('./program')
- local_flags(argv)
-
-
-if __name__ == '__main__':
- googletest.main()