1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for gtest.py"""
import unittest
from typing import Optional
import conditions
from conditions import Condition
import gtest
class GtestTest(unittest.TestCase):
def disabler_test(self,
input_file: str,
test_name: str,
new_cond,
expected_result: str,
message: Optional[str] = None):
"""Helper function for testing gtest.disabler."""
self.maxDiff = None
if isinstance(new_cond, list):
new_cond = conditions.parse(new_cond)
else:
assert isinstance(new_cond, conditions.BaseCondition)
resulting_file = gtest.disabler(test_name, input_file, new_cond, message)
self.assertEqual(expected_result.strip(), resulting_file.strip())
def test_disable_enabled_test(self):
self.disabler_test('TEST_F(Suite, Test) {}', 'Suite.Test',
conditions.ALWAYS, 'TEST_F(Suite, DISABLED_Test) {}')
def test_disable_already_disabled_test(self):
self.disabler_test('TEST(Suite, DISABLED_Test) {}', 'Suite.Test',
conditions.ALWAYS, 'TEST(Suite, DISABLED_Test) {}')
def test_enable_disabled_test(self):
self.disabler_test('TEST_F(Suite, DISABLED_Test) {}', 'Suite.Test',
conditions.NEVER, 'TEST_F(Suite, Test) {}')
def test_enable_already_enabled_test(self):
self.disabler_test('TEST(Suite, Test) {}', 'Suite.Test', conditions.NEVER,
'TEST(Suite, Test) {}')
def test_conditionally_disable_test(self):
# Include some bad formatting in the original TEST line, to make sure
# the set of modified lines passed to clang-format includes it.
self.disabler_test(
'TEST ( Suite, Test) {}', 'Suite.Test', ['linux', 'mac'], '''
#include "build/build_config.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''')
def test_conditionally_disable_already_disabled_test(self):
# Include some bad formatting in the original TEST line, to make sure
# the set of modified lines passed to clang-format includes it.
self.disabler_test(
'TEST(Suite , DISABLED_Test) {}', 'Suite.Test', ['linux', 'mac'], '''
#include "build/build_config.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''')
def test_extend_conditions_on_conditionally_disabled_test(self):
self.disabler_test(
'''
#include "build/build_config.h"
#if BUILDFLAG(IS_WIN)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''', 'Suite.Test', ['linux', 'mac'], '''
#include "build/build_config.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''')
def test_enable_conditionally_disabled_test(self):
self.disabler_test(
'''
#if BUILDFLAG(IS_WIN)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''', 'Suite.Test', conditions.NEVER, 'TEST(Suite, Test) {}')
def test_simplify_redundant_conditions(self):
self.disabler_test(
'''
#include "build/build_config.h"
#if BUILDFLAG(IS_LINUX) && defined(ADDRESS_SANITIZER))
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''', 'Suite.Test', ['linux'], '''
#include "build/build_config.h"
#if BUILDFLAG(IS_LINUX)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''')
def test_handle_backslash_line_continuations(self):
self.disabler_test(
r'''
#if BUILDFLAG(IS_WIN)
#define MAYBE_Test \
DISABLED_Test
#else
#define MAYBE_Test \
Test
#endif
TEST(Suite, MAYBE_Test) {}
''', 'Suite.Test', conditions.ALWAYS, 'TEST(Suite, DISABLED_Test) {}')
def test_disable_test_via_build_flag(self):
self.disabler_test(
'TEST(Suite, Test) {}', 'Suite.Test', ['chromeos'], '''
#include "build/build_config.h"
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''')
def test_split_long_lines_disabled(self):
self.disabler_test(
'TEST(Suite, ' +
'ReallyReallyReallyReallyReallyReallyReallyReallyLongTestName) {}',
'Suite.ReallyReallyReallyReallyReallyReallyReallyReallyLongTestName',
conditions.ALWAYS, '''
TEST(Suite,
DISABLED_ReallyReallyReallyReallyReallyReallyReallyReallyLongTestName) {}
''')
def test_split_long_lines_conditionally_disabled(self):
self.disabler_test(
'TEST(Suite, ReallyReallyReallyReallyLongTestName) {}',
'Suite.ReallyReallyReallyReallyLongTestName', ['win'], r'''
#include "build/build_config.h"
#if BUILDFLAG(IS_WIN)
#define MAYBE_ReallyReallyReallyReallyLongTestName \
DISABLED_ReallyReallyReallyReallyLongTestName
#else
#define MAYBE_ReallyReallyReallyReallyLongTestName \
ReallyReallyReallyReallyLongTestName
#endif
TEST(Suite, MAYBE_ReallyReallyReallyReallyLongTestName) {}
''')
def test_with_name_ocurring_later_in_file(self):
self.disabler_test(
'''
TEST(Suite, Test) {}
void SomeFunctionWihTestInTheName() {}
''', 'Suite.Test', conditions.ALWAYS, '''
TEST(Suite, DISABLED_Test) {}
void SomeFunctionWihTestInTheName() {}
''')
def test_ignore_comments_in_preprocessor_directive(self):
self.disabler_test(
'''
#if /* something */ BUILDFLAG( /* something else */ IS_WIN) // comment ||
#define MAYBE_Test DISABLED_Test // another one &&
#else /* and another... */
#define MAYBE_Test Test // you know the drill(
#endif // one more!
TEST(Suite, MAYBE_Test) {}
''', 'Suite.Test', conditions.NEVER, 'TEST(Suite, Test) {}')
def test_disable_unconditionally_with_message(self):
# Also include some formatting that should be fixed up, to test that the
# correct line number is passed to clang-format.
self.disabler_test('''
// existing comment
TEST(Suite, Test) {}
// another comment''',
'Suite.Test',
conditions.ALWAYS,
'''
// existing comment
// here is the message
TEST(Suite, DISABLED_Test) {}
// another comment''',
message='here is the message')
def test_conditionally_disable_test_with_message(self):
# Also include some formatting that should be fixed up, to test that the
# correct line number is passed to clang-format.
self.disabler_test('TEST(Suite, Test) {}',
'Suite.Test', ['linux', 'mac'],
'''
#include "build/build_config.h"
// we should really fix this
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
#define MAYBE_Test DISABLED_Test
#else
#define MAYBE_Test Test
#endif
TEST(Suite, MAYBE_Test) {}
''',
message='we should really fix this')
def test_ignores_test_names_in_other_contexts(self):
self.disabler_test(
'''
IN_PROC_BROWSER_TEST_P(Suite, Test) {
// This is a very important Test.
auto s = "Test";
auto t = "some other Test stuff";
}''', 'Suite.Test', conditions.ALWAYS, '''
IN_PROC_BROWSER_TEST_P(Suite, DISABLED_Test) {
// This is a very important Test.
auto s = "Test";
auto t = "some other Test stuff";
}''')
if __name__ == '__main__':
unittest.main()
|