gtest_color_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2008, 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 """Verifies that Google Test correctly determines whether to use colors."""
00033 
00034 __author__ = 'wan@google.com (Zhanyong Wan)'
00035 
00036 import os
00037 import gtest_test_utils
00038 
00039 
00040 IS_WINDOWS = os.name = 'nt'
00041 
00042 COLOR_ENV_VAR = 'GTEST_COLOR'
00043 COLOR_FLAG = 'gtest_color'
00044 COMMAND = gtest_test_utils.GetTestExecutablePath('gtest_color_test_')
00045 
00046 
00047 def SetEnvVar(env_var, value):
00048   """Sets the env variable to 'value'; unsets it when 'value' is None."""
00049 
00050   if value is not None:
00051     os.environ[env_var] = value
00052   elif env_var in os.environ:
00053     del os.environ[env_var]
00054 
00055 
00056 def UsesColor(term, color_env_var, color_flag):
00057   """Runs gtest_color_test_ and returns its exit code."""
00058 
00059   SetEnvVar('TERM', term)
00060   SetEnvVar(COLOR_ENV_VAR, color_env_var)
00061 
00062   if color_flag is None:
00063     args = []
00064   else:
00065     args = ['--%s=%s' % (COLOR_FLAG, color_flag)]
00066   p = gtest_test_utils.Subprocess([COMMAND] + args)
00067   return not p.exited or p.exit_code
00068 
00069 
00070 class GTestColorTest(gtest_test_utils.TestCase):
00071   def testNoEnvVarNoFlag(self):
00072     """Tests the case when there's neither GTEST_COLOR nor --gtest_color."""
00073 
00074     if not IS_WINDOWS:
00075       self.assert_(not UsesColor('dumb', None, None))
00076       self.assert_(not UsesColor('emacs', None, None))
00077       self.assert_(not UsesColor('xterm-mono', None, None))
00078       self.assert_(not UsesColor('unknown', None, None))
00079       self.assert_(not UsesColor(None, None, None))
00080     self.assert_(UsesColor('linux', None, None))
00081     self.assert_(UsesColor('cygwin', None, None))
00082     self.assert_(UsesColor('xterm', None, None))
00083     self.assert_(UsesColor('xterm-color', None, None))
00084     self.assert_(UsesColor('xterm-256color', None, None))
00085 
00086   def testFlagOnly(self):
00087     """Tests the case when there's --gtest_color but not GTEST_COLOR."""
00088 
00089     self.assert_(not UsesColor('dumb', None, 'no'))
00090     self.assert_(not UsesColor('xterm-color', None, 'no'))
00091     if not IS_WINDOWS:
00092       self.assert_(not UsesColor('emacs', None, 'auto'))
00093     self.assert_(UsesColor('xterm', None, 'auto'))
00094     self.assert_(UsesColor('dumb', None, 'yes'))
00095     self.assert_(UsesColor('xterm', None, 'yes'))
00096 
00097   def testEnvVarOnly(self):
00098     """Tests the case when there's GTEST_COLOR but not --gtest_color."""
00099 
00100     self.assert_(not UsesColor('dumb', 'no', None))
00101     self.assert_(not UsesColor('xterm-color', 'no', None))
00102     if not IS_WINDOWS:
00103       self.assert_(not UsesColor('dumb', 'auto', None))
00104     self.assert_(UsesColor('xterm-color', 'auto', None))
00105     self.assert_(UsesColor('dumb', 'yes', None))
00106     self.assert_(UsesColor('xterm-color', 'yes', None))
00107 
00108   def testEnvVarAndFlag(self):
00109     """Tests the case when there are both GTEST_COLOR and --gtest_color."""
00110 
00111     self.assert_(not UsesColor('xterm-color', 'no', 'no'))
00112     self.assert_(UsesColor('dumb', 'no', 'yes'))
00113     self.assert_(UsesColor('xterm-color', 'no', 'auto'))
00114 
00115   def testAliasesOfYesAndNo(self):
00116     """Tests using aliases in specifying --gtest_color."""
00117 
00118     self.assert_(UsesColor('dumb', None, 'true'))
00119     self.assert_(UsesColor('dumb', None, 'YES'))
00120     self.assert_(UsesColor('dumb', None, 'T'))
00121     self.assert_(UsesColor('dumb', None, '1'))
00122 
00123     self.assert_(not UsesColor('xterm', None, 'f'))
00124     self.assert_(not UsesColor('xterm', None, 'false'))
00125     self.assert_(not UsesColor('xterm', None, '0'))
00126     self.assert_(not UsesColor('xterm', None, 'unknown'))
00127 
00128 
00129 if __name__ == '__main__':
00130   gtest_test_utils.Main()


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04