gtest_throw_on_failure_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2009, Google Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are
00008 # met:
00009 #
00010 #     * Redistributions of source code must retain the above copyright
00011 # notice, this list of conditions and the following disclaimer.
00012 #     * Redistributions in binary form must reproduce the above
00013 # copyright notice, this list of conditions and the following disclaimer
00014 # in the documentation and/or other materials provided with the
00015 # distribution.
00016 #     * Neither the name of Google Inc. nor the names of its
00017 # contributors may be used to endorse or promote products derived from
00018 # this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00023 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 
00032 """Tests Google Test's throw-on-failure mode with exceptions disabled.
00033 
00034 This script invokes gtest_throw_on_failure_test_ (a program written with
00035 Google Test) with different environments and command line flags.
00036 """
00037 
00038 __author__ = 'wan@google.com (Zhanyong Wan)'
00039 
00040 import os
00041 import gtest_test_utils
00042 
00043 
00044 # Constants.
00045 
00046 # The command line flag for enabling/disabling the throw-on-failure mode.
00047 THROW_ON_FAILURE = 'gtest_throw_on_failure'
00048 
00049 # Path to the gtest_throw_on_failure_test_ program, compiled with
00050 # exceptions disabled.
00051 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00052     'gtest_throw_on_failure_test_')
00053 
00054 
00055 # Utilities.
00056 
00057 
00058 def SetEnvVar(env_var, value):
00059   """Sets an environment variable to a given value; unsets it when the
00060   given value is None.
00061   """
00062 
00063   env_var = env_var.upper()
00064   if value is not None:
00065     os.environ[env_var] = value
00066   elif env_var in os.environ:
00067     del os.environ[env_var]
00068 
00069 
00070 def Run(command):
00071   """Runs a command; returns True/False if its exit code is/isn't 0."""
00072 
00073   print 'Running "%s". . .' % ' '.join(command)
00074   p = gtest_test_utils.Subprocess(command)
00075   return p.exited and p.exit_code == 0
00076 
00077 
00078 # The tests.  TODO(wan@google.com): refactor the class to share common
00079 # logic with code in gtest_break_on_failure_unittest.py.
00080 class ThrowOnFailureTest(gtest_test_utils.TestCase):
00081   """Tests the throw-on-failure mode."""
00082 
00083   def RunAndVerify(self, env_var_value, flag_value, should_fail):
00084     """Runs gtest_throw_on_failure_test_ and verifies that it does
00085     (or does not) exit with a non-zero code.
00086 
00087     Args:
00088       env_var_value:    value of the GTEST_BREAK_ON_FAILURE environment
00089                         variable; None if the variable should be unset.
00090       flag_value:       value of the --gtest_break_on_failure flag;
00091                         None if the flag should not be present.
00092       should_fail:      True iff the program is expected to fail.
00093     """
00094 
00095     SetEnvVar(THROW_ON_FAILURE, env_var_value)
00096 
00097     if env_var_value is None:
00098       env_var_value_msg = ' is not set'
00099     else:
00100       env_var_value_msg = '=' + env_var_value
00101 
00102     if flag_value is None:
00103       flag = ''
00104     elif flag_value == '0':
00105       flag = '--%s=0' % THROW_ON_FAILURE
00106     else:
00107       flag = '--%s' % THROW_ON_FAILURE
00108 
00109     command = [EXE_PATH]
00110     if flag:
00111       command.append(flag)
00112 
00113     if should_fail:
00114       should_or_not = 'should'
00115     else:
00116       should_or_not = 'should not'
00117 
00118     failed = not Run(command)
00119 
00120     SetEnvVar(THROW_ON_FAILURE, None)
00121 
00122     msg = ('when %s%s, an assertion failure in "%s" %s cause a non-zero '
00123            'exit code.' %
00124            (THROW_ON_FAILURE, env_var_value_msg, ' '.join(command),
00125             should_or_not))
00126     self.assert_(failed == should_fail, msg)
00127 
00128   def testDefaultBehavior(self):
00129     """Tests the behavior of the default mode."""
00130 
00131     self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False)
00132 
00133   def testThrowOnFailureEnvVar(self):
00134     """Tests using the GTEST_THROW_ON_FAILURE environment variable."""
00135 
00136     self.RunAndVerify(env_var_value='0',
00137                       flag_value=None,
00138                       should_fail=False)
00139     self.RunAndVerify(env_var_value='1',
00140                       flag_value=None,
00141                       should_fail=True)
00142 
00143   def testThrowOnFailureFlag(self):
00144     """Tests using the --gtest_throw_on_failure flag."""
00145 
00146     self.RunAndVerify(env_var_value=None,
00147                       flag_value='0',
00148                       should_fail=False)
00149     self.RunAndVerify(env_var_value=None,
00150                       flag_value='1',
00151                       should_fail=True)
00152 
00153   def testThrowOnFailureFlagOverridesEnvVar(self):
00154     """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE."""
00155 
00156     self.RunAndVerify(env_var_value='0',
00157                       flag_value='0',
00158                       should_fail=False)
00159     self.RunAndVerify(env_var_value='0',
00160                       flag_value='1',
00161                       should_fail=True)
00162     self.RunAndVerify(env_var_value='1',
00163                       flag_value='0',
00164                       should_fail=False)
00165     self.RunAndVerify(env_var_value='1',
00166                       flag_value='1',
00167                       should_fail=True)
00168 
00169 
00170 if __name__ == '__main__':
00171   gtest_test_utils.Main()


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:55