media/webrtc/trunk/testing/gtest/scripts/gen_gtest_pred_impl.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2 #
michael@0 3 # Copyright 2006, Google Inc.
michael@0 4 # All rights reserved.
michael@0 5 #
michael@0 6 # Redistribution and use in source and binary forms, with or without
michael@0 7 # modification, are permitted provided that the following conditions are
michael@0 8 # met:
michael@0 9 #
michael@0 10 # * Redistributions of source code must retain the above copyright
michael@0 11 # notice, this list of conditions and the following disclaimer.
michael@0 12 # * Redistributions in binary form must reproduce the above
michael@0 13 # copyright notice, this list of conditions and the following disclaimer
michael@0 14 # in the documentation and/or other materials provided with the
michael@0 15 # distribution.
michael@0 16 # * Neither the name of Google Inc. nor the names of its
michael@0 17 # contributors may be used to endorse or promote products derived from
michael@0 18 # this software without specific prior written permission.
michael@0 19 #
michael@0 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31
michael@0 32 """gen_gtest_pred_impl.py v0.1
michael@0 33
michael@0 34 Generates the implementation of Google Test predicate assertions and
michael@0 35 accompanying tests.
michael@0 36
michael@0 37 Usage:
michael@0 38
michael@0 39 gen_gtest_pred_impl.py MAX_ARITY
michael@0 40
michael@0 41 where MAX_ARITY is a positive integer.
michael@0 42
michael@0 43 The command generates the implementation of up-to MAX_ARITY-ary
michael@0 44 predicate assertions, and writes it to file gtest_pred_impl.h in the
michael@0 45 directory where the script is. It also generates the accompanying
michael@0 46 unit test in file gtest_pred_impl_unittest.cc.
michael@0 47 """
michael@0 48
michael@0 49 __author__ = 'wan@google.com (Zhanyong Wan)'
michael@0 50
michael@0 51 import os
michael@0 52 import sys
michael@0 53 import time
michael@0 54
michael@0 55 # Where this script is.
michael@0 56 SCRIPT_DIR = os.path.dirname(sys.argv[0])
michael@0 57
michael@0 58 # Where to store the generated header.
michael@0 59 HEADER = os.path.join(SCRIPT_DIR, '../include/gtest/gtest_pred_impl.h')
michael@0 60
michael@0 61 # Where to store the generated unit test.
michael@0 62 UNIT_TEST = os.path.join(SCRIPT_DIR, '../test/gtest_pred_impl_unittest.cc')
michael@0 63
michael@0 64
michael@0 65 def HeaderPreamble(n):
michael@0 66 """Returns the preamble for the header file.
michael@0 67
michael@0 68 Args:
michael@0 69 n: the maximum arity of the predicate macros to be generated.
michael@0 70 """
michael@0 71
michael@0 72 # A map that defines the values used in the preamble template.
michael@0 73 DEFS = {
michael@0 74 'today' : time.strftime('%m/%d/%Y'),
michael@0 75 'year' : time.strftime('%Y'),
michael@0 76 'command' : '%s %s' % (os.path.basename(sys.argv[0]), n),
michael@0 77 'n' : n
michael@0 78 }
michael@0 79
michael@0 80 return (
michael@0 81 """// Copyright 2006, Google Inc.
michael@0 82 // All rights reserved.
michael@0 83 //
michael@0 84 // Redistribution and use in source and binary forms, with or without
michael@0 85 // modification, are permitted provided that the following conditions are
michael@0 86 // met:
michael@0 87 //
michael@0 88 // * Redistributions of source code must retain the above copyright
michael@0 89 // notice, this list of conditions and the following disclaimer.
michael@0 90 // * Redistributions in binary form must reproduce the above
michael@0 91 // copyright notice, this list of conditions and the following disclaimer
michael@0 92 // in the documentation and/or other materials provided with the
michael@0 93 // distribution.
michael@0 94 // * Neither the name of Google Inc. nor the names of its
michael@0 95 // contributors may be used to endorse or promote products derived from
michael@0 96 // this software without specific prior written permission.
michael@0 97 //
michael@0 98 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 99 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 100 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 101 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 102 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 103 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 104 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 105 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 106 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 107 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 108 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 109
michael@0 110 // This file is AUTOMATICALLY GENERATED on %(today)s by command
michael@0 111 // '%(command)s'. DO NOT EDIT BY HAND!
michael@0 112 //
michael@0 113 // Implements a family of generic predicate assertion macros.
michael@0 114
michael@0 115 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
michael@0 116 #define GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
michael@0 117
michael@0 118 // Makes sure this header is not included before gtest.h.
michael@0 119 #ifndef GTEST_INCLUDE_GTEST_GTEST_H_
michael@0 120 # error Do not include gtest_pred_impl.h directly. Include gtest.h instead.
michael@0 121 #endif // GTEST_INCLUDE_GTEST_GTEST_H_
michael@0 122
michael@0 123 // This header implements a family of generic predicate assertion
michael@0 124 // macros:
michael@0 125 //
michael@0 126 // ASSERT_PRED_FORMAT1(pred_format, v1)
michael@0 127 // ASSERT_PRED_FORMAT2(pred_format, v1, v2)
michael@0 128 // ...
michael@0 129 //
michael@0 130 // where pred_format is a function or functor that takes n (in the
michael@0 131 // case of ASSERT_PRED_FORMATn) values and their source expression
michael@0 132 // text, and returns a testing::AssertionResult. See the definition
michael@0 133 // of ASSERT_EQ in gtest.h for an example.
michael@0 134 //
michael@0 135 // If you don't care about formatting, you can use the more
michael@0 136 // restrictive version:
michael@0 137 //
michael@0 138 // ASSERT_PRED1(pred, v1)
michael@0 139 // ASSERT_PRED2(pred, v1, v2)
michael@0 140 // ...
michael@0 141 //
michael@0 142 // where pred is an n-ary function or functor that returns bool,
michael@0 143 // and the values v1, v2, ..., must support the << operator for
michael@0 144 // streaming to std::ostream.
michael@0 145 //
michael@0 146 // We also define the EXPECT_* variations.
michael@0 147 //
michael@0 148 // For now we only support predicates whose arity is at most %(n)s.
michael@0 149 // Please email googletestframework@googlegroups.com if you need
michael@0 150 // support for higher arities.
michael@0 151
michael@0 152 // GTEST_ASSERT_ is the basic statement to which all of the assertions
michael@0 153 // in this file reduce. Don't use this in your code.
michael@0 154
michael@0 155 #define GTEST_ASSERT_(expression, on_failure) \\
michael@0 156 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\
michael@0 157 if (const ::testing::AssertionResult gtest_ar = (expression)) \\
michael@0 158 ; \\
michael@0 159 else \\
michael@0 160 on_failure(gtest_ar.failure_message())
michael@0 161 """ % DEFS)
michael@0 162
michael@0 163
michael@0 164 def Arity(n):
michael@0 165 """Returns the English name of the given arity."""
michael@0 166
michael@0 167 if n < 0:
michael@0 168 return None
michael@0 169 elif n <= 3:
michael@0 170 return ['nullary', 'unary', 'binary', 'ternary'][n]
michael@0 171 else:
michael@0 172 return '%s-ary' % n
michael@0 173
michael@0 174
michael@0 175 def Title(word):
michael@0 176 """Returns the given word in title case. The difference between
michael@0 177 this and string's title() method is that Title('4-ary') is '4-ary'
michael@0 178 while '4-ary'.title() is '4-Ary'."""
michael@0 179
michael@0 180 return word[0].upper() + word[1:]
michael@0 181
michael@0 182
michael@0 183 def OneTo(n):
michael@0 184 """Returns the list [1, 2, 3, ..., n]."""
michael@0 185
michael@0 186 return range(1, n + 1)
michael@0 187
michael@0 188
michael@0 189 def Iter(n, format, sep=''):
michael@0 190 """Given a positive integer n, a format string that contains 0 or
michael@0 191 more '%s' format specs, and optionally a separator string, returns
michael@0 192 the join of n strings, each formatted with the format string on an
michael@0 193 iterator ranged from 1 to n.
michael@0 194
michael@0 195 Example:
michael@0 196
michael@0 197 Iter(3, 'v%s', sep=', ') returns 'v1, v2, v3'.
michael@0 198 """
michael@0 199
michael@0 200 # How many '%s' specs are in format?
michael@0 201 spec_count = len(format.split('%s')) - 1
michael@0 202 return sep.join([format % (spec_count * (i,)) for i in OneTo(n)])
michael@0 203
michael@0 204
michael@0 205 def ImplementationForArity(n):
michael@0 206 """Returns the implementation of n-ary predicate assertions."""
michael@0 207
michael@0 208 # A map the defines the values used in the implementation template.
michael@0 209 DEFS = {
michael@0 210 'n' : str(n),
michael@0 211 'vs' : Iter(n, 'v%s', sep=', '),
michael@0 212 'vts' : Iter(n, '#v%s', sep=', '),
michael@0 213 'arity' : Arity(n),
michael@0 214 'Arity' : Title(Arity(n))
michael@0 215 }
michael@0 216
michael@0 217 impl = """
michael@0 218
michael@0 219 // Helper function for implementing {EXPECT|ASSERT}_PRED%(n)s. Don't use
michael@0 220 // this in your code.
michael@0 221 template <typename Pred""" % DEFS
michael@0 222
michael@0 223 impl += Iter(n, """,
michael@0 224 typename T%s""")
michael@0 225
michael@0 226 impl += """>
michael@0 227 AssertionResult AssertPred%(n)sHelper(const char* pred_text""" % DEFS
michael@0 228
michael@0 229 impl += Iter(n, """,
michael@0 230 const char* e%s""")
michael@0 231
michael@0 232 impl += """,
michael@0 233 Pred pred"""
michael@0 234
michael@0 235 impl += Iter(n, """,
michael@0 236 const T%s& v%s""")
michael@0 237
michael@0 238 impl += """) {
michael@0 239 if (pred(%(vs)s)) return AssertionSuccess();
michael@0 240
michael@0 241 """ % DEFS
michael@0 242
michael@0 243 impl += ' return AssertionFailure() << pred_text << "("'
michael@0 244
michael@0 245 impl += Iter(n, """
michael@0 246 << e%s""", sep=' << ", "')
michael@0 247
michael@0 248 impl += ' << ") evaluates to false, where"'
michael@0 249
michael@0 250 impl += Iter(n, """
michael@0 251 << "\\n" << e%s << " evaluates to " << v%s""")
michael@0 252
michael@0 253 impl += """;
michael@0 254 }
michael@0 255
michael@0 256 // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT%(n)s.
michael@0 257 // Don't use this in your code.
michael@0 258 #define GTEST_PRED_FORMAT%(n)s_(pred_format, %(vs)s, on_failure)\\
michael@0 259 GTEST_ASSERT_(pred_format(%(vts)s, %(vs)s), \\
michael@0 260 on_failure)
michael@0 261
michael@0 262 // Internal macro for implementing {EXPECT|ASSERT}_PRED%(n)s. Don't use
michael@0 263 // this in your code.
michael@0 264 #define GTEST_PRED%(n)s_(pred, %(vs)s, on_failure)\\
michael@0 265 GTEST_ASSERT_(::testing::AssertPred%(n)sHelper(#pred""" % DEFS
michael@0 266
michael@0 267 impl += Iter(n, """, \\
michael@0 268 #v%s""")
michael@0 269
michael@0 270 impl += """, \\
michael@0 271 pred"""
michael@0 272
michael@0 273 impl += Iter(n, """, \\
michael@0 274 v%s""")
michael@0 275
michael@0 276 impl += """), on_failure)
michael@0 277
michael@0 278 // %(Arity)s predicate assertion macros.
michael@0 279 #define EXPECT_PRED_FORMAT%(n)s(pred_format, %(vs)s) \\
michael@0 280 GTEST_PRED_FORMAT%(n)s_(pred_format, %(vs)s, GTEST_NONFATAL_FAILURE_)
michael@0 281 #define EXPECT_PRED%(n)s(pred, %(vs)s) \\
michael@0 282 GTEST_PRED%(n)s_(pred, %(vs)s, GTEST_NONFATAL_FAILURE_)
michael@0 283 #define ASSERT_PRED_FORMAT%(n)s(pred_format, %(vs)s) \\
michael@0 284 GTEST_PRED_FORMAT%(n)s_(pred_format, %(vs)s, GTEST_FATAL_FAILURE_)
michael@0 285 #define ASSERT_PRED%(n)s(pred, %(vs)s) \\
michael@0 286 GTEST_PRED%(n)s_(pred, %(vs)s, GTEST_FATAL_FAILURE_)
michael@0 287
michael@0 288 """ % DEFS
michael@0 289
michael@0 290 return impl
michael@0 291
michael@0 292
michael@0 293 def HeaderPostamble():
michael@0 294 """Returns the postamble for the header file."""
michael@0 295
michael@0 296 return """
michael@0 297
michael@0 298 #endif // GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
michael@0 299 """
michael@0 300
michael@0 301
michael@0 302 def GenerateFile(path, content):
michael@0 303 """Given a file path and a content string, overwrites it with the
michael@0 304 given content."""
michael@0 305
michael@0 306 print 'Updating file %s . . .' % path
michael@0 307
michael@0 308 f = file(path, 'w+')
michael@0 309 print >>f, content,
michael@0 310 f.close()
michael@0 311
michael@0 312 print 'File %s has been updated.' % path
michael@0 313
michael@0 314
michael@0 315 def GenerateHeader(n):
michael@0 316 """Given the maximum arity n, updates the header file that implements
michael@0 317 the predicate assertions."""
michael@0 318
michael@0 319 GenerateFile(HEADER,
michael@0 320 HeaderPreamble(n)
michael@0 321 + ''.join([ImplementationForArity(i) for i in OneTo(n)])
michael@0 322 + HeaderPostamble())
michael@0 323
michael@0 324
michael@0 325 def UnitTestPreamble():
michael@0 326 """Returns the preamble for the unit test file."""
michael@0 327
michael@0 328 # A map that defines the values used in the preamble template.
michael@0 329 DEFS = {
michael@0 330 'today' : time.strftime('%m/%d/%Y'),
michael@0 331 'year' : time.strftime('%Y'),
michael@0 332 'command' : '%s %s' % (os.path.basename(sys.argv[0]), sys.argv[1]),
michael@0 333 }
michael@0 334
michael@0 335 return (
michael@0 336 """// Copyright 2006, Google Inc.
michael@0 337 // All rights reserved.
michael@0 338 //
michael@0 339 // Redistribution and use in source and binary forms, with or without
michael@0 340 // modification, are permitted provided that the following conditions are
michael@0 341 // met:
michael@0 342 //
michael@0 343 // * Redistributions of source code must retain the above copyright
michael@0 344 // notice, this list of conditions and the following disclaimer.
michael@0 345 // * Redistributions in binary form must reproduce the above
michael@0 346 // copyright notice, this list of conditions and the following disclaimer
michael@0 347 // in the documentation and/or other materials provided with the
michael@0 348 // distribution.
michael@0 349 // * Neither the name of Google Inc. nor the names of its
michael@0 350 // contributors may be used to endorse or promote products derived from
michael@0 351 // this software without specific prior written permission.
michael@0 352 //
michael@0 353 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 354 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 355 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 356 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 357 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 358 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 359 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 360 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 361 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 362 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 363 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 364
michael@0 365 // This file is AUTOMATICALLY GENERATED on %(today)s by command
michael@0 366 // '%(command)s'. DO NOT EDIT BY HAND!
michael@0 367
michael@0 368 // Regression test for gtest_pred_impl.h
michael@0 369 //
michael@0 370 // This file is generated by a script and quite long. If you intend to
michael@0 371 // learn how Google Test works by reading its unit tests, read
michael@0 372 // gtest_unittest.cc instead.
michael@0 373 //
michael@0 374 // This is intended as a regression test for the Google Test predicate
michael@0 375 // assertions. We compile it as part of the gtest_unittest target
michael@0 376 // only to keep the implementation tidy and compact, as it is quite
michael@0 377 // involved to set up the stage for testing Google Test using Google
michael@0 378 // Test itself.
michael@0 379 //
michael@0 380 // Currently, gtest_unittest takes ~11 seconds to run in the testing
michael@0 381 // daemon. In the future, if it grows too large and needs much more
michael@0 382 // time to finish, we should consider separating this file into a
michael@0 383 // stand-alone regression test.
michael@0 384
michael@0 385 #include <iostream>
michael@0 386
michael@0 387 #include "gtest/gtest.h"
michael@0 388 #include "gtest/gtest-spi.h"
michael@0 389
michael@0 390 // A user-defined data type.
michael@0 391 struct Bool {
michael@0 392 explicit Bool(int val) : value(val != 0) {}
michael@0 393
michael@0 394 bool operator>(int n) const { return value > Bool(n).value; }
michael@0 395
michael@0 396 Bool operator+(const Bool& rhs) const { return Bool(value + rhs.value); }
michael@0 397
michael@0 398 bool operator==(const Bool& rhs) const { return value == rhs.value; }
michael@0 399
michael@0 400 bool value;
michael@0 401 };
michael@0 402
michael@0 403 // Enables Bool to be used in assertions.
michael@0 404 std::ostream& operator<<(std::ostream& os, const Bool& x) {
michael@0 405 return os << (x.value ? "true" : "false");
michael@0 406 }
michael@0 407
michael@0 408 """ % DEFS)
michael@0 409
michael@0 410
michael@0 411 def TestsForArity(n):
michael@0 412 """Returns the tests for n-ary predicate assertions."""
michael@0 413
michael@0 414 # A map that defines the values used in the template for the tests.
michael@0 415 DEFS = {
michael@0 416 'n' : n,
michael@0 417 'es' : Iter(n, 'e%s', sep=', '),
michael@0 418 'vs' : Iter(n, 'v%s', sep=', '),
michael@0 419 'vts' : Iter(n, '#v%s', sep=', '),
michael@0 420 'tvs' : Iter(n, 'T%s v%s', sep=', '),
michael@0 421 'int_vs' : Iter(n, 'int v%s', sep=', '),
michael@0 422 'Bool_vs' : Iter(n, 'Bool v%s', sep=', '),
michael@0 423 'types' : Iter(n, 'typename T%s', sep=', '),
michael@0 424 'v_sum' : Iter(n, 'v%s', sep=' + '),
michael@0 425 'arity' : Arity(n),
michael@0 426 'Arity' : Title(Arity(n)),
michael@0 427 }
michael@0 428
michael@0 429 tests = (
michael@0 430 """// Sample functions/functors for testing %(arity)s predicate assertions.
michael@0 431
michael@0 432 // A %(arity)s predicate function.
michael@0 433 template <%(types)s>
michael@0 434 bool PredFunction%(n)s(%(tvs)s) {
michael@0 435 return %(v_sum)s > 0;
michael@0 436 }
michael@0 437
michael@0 438 // The following two functions are needed to circumvent a bug in
michael@0 439 // gcc 2.95.3, which sometimes has problem with the above template
michael@0 440 // function.
michael@0 441 bool PredFunction%(n)sInt(%(int_vs)s) {
michael@0 442 return %(v_sum)s > 0;
michael@0 443 }
michael@0 444 bool PredFunction%(n)sBool(%(Bool_vs)s) {
michael@0 445 return %(v_sum)s > 0;
michael@0 446 }
michael@0 447 """ % DEFS)
michael@0 448
michael@0 449 tests += """
michael@0 450 // A %(arity)s predicate functor.
michael@0 451 struct PredFunctor%(n)s {
michael@0 452 template <%(types)s>
michael@0 453 bool operator()(""" % DEFS
michael@0 454
michael@0 455 tests += Iter(n, 'const T%s& v%s', sep=""",
michael@0 456 """)
michael@0 457
michael@0 458 tests += """) {
michael@0 459 return %(v_sum)s > 0;
michael@0 460 }
michael@0 461 };
michael@0 462 """ % DEFS
michael@0 463
michael@0 464 tests += """
michael@0 465 // A %(arity)s predicate-formatter function.
michael@0 466 template <%(types)s>
michael@0 467 testing::AssertionResult PredFormatFunction%(n)s(""" % DEFS
michael@0 468
michael@0 469 tests += Iter(n, 'const char* e%s', sep=""",
michael@0 470 """)
michael@0 471
michael@0 472 tests += Iter(n, """,
michael@0 473 const T%s& v%s""")
michael@0 474
michael@0 475 tests += """) {
michael@0 476 if (PredFunction%(n)s(%(vs)s))
michael@0 477 return testing::AssertionSuccess();
michael@0 478
michael@0 479 return testing::AssertionFailure()
michael@0 480 << """ % DEFS
michael@0 481
michael@0 482 tests += Iter(n, 'e%s', sep=' << " + " << ')
michael@0 483
michael@0 484 tests += """
michael@0 485 << " is expected to be positive, but evaluates to "
michael@0 486 << %(v_sum)s << ".";
michael@0 487 }
michael@0 488 """ % DEFS
michael@0 489
michael@0 490 tests += """
michael@0 491 // A %(arity)s predicate-formatter functor.
michael@0 492 struct PredFormatFunctor%(n)s {
michael@0 493 template <%(types)s>
michael@0 494 testing::AssertionResult operator()(""" % DEFS
michael@0 495
michael@0 496 tests += Iter(n, 'const char* e%s', sep=""",
michael@0 497 """)
michael@0 498
michael@0 499 tests += Iter(n, """,
michael@0 500 const T%s& v%s""")
michael@0 501
michael@0 502 tests += """) const {
michael@0 503 return PredFormatFunction%(n)s(%(es)s, %(vs)s);
michael@0 504 }
michael@0 505 };
michael@0 506 """ % DEFS
michael@0 507
michael@0 508 tests += """
michael@0 509 // Tests for {EXPECT|ASSERT}_PRED_FORMAT%(n)s.
michael@0 510
michael@0 511 class Predicate%(n)sTest : public testing::Test {
michael@0 512 protected:
michael@0 513 virtual void SetUp() {
michael@0 514 expected_to_finish_ = true;
michael@0 515 finished_ = false;""" % DEFS
michael@0 516
michael@0 517 tests += """
michael@0 518 """ + Iter(n, 'n%s_ = ') + """0;
michael@0 519 }
michael@0 520 """
michael@0 521
michael@0 522 tests += """
michael@0 523 virtual void TearDown() {
michael@0 524 // Verifies that each of the predicate's arguments was evaluated
michael@0 525 // exactly once."""
michael@0 526
michael@0 527 tests += ''.join(["""
michael@0 528 EXPECT_EQ(1, n%s_) <<
michael@0 529 "The predicate assertion didn't evaluate argument %s "
michael@0 530 "exactly once.";""" % (i, i + 1) for i in OneTo(n)])
michael@0 531
michael@0 532 tests += """
michael@0 533
michael@0 534 // Verifies that the control flow in the test function is expected.
michael@0 535 if (expected_to_finish_ && !finished_) {
michael@0 536 FAIL() << "The predicate assertion unexpactedly aborted the test.";
michael@0 537 } else if (!expected_to_finish_ && finished_) {
michael@0 538 FAIL() << "The failed predicate assertion didn't abort the test "
michael@0 539 "as expected.";
michael@0 540 }
michael@0 541 }
michael@0 542
michael@0 543 // true iff the test function is expected to run to finish.
michael@0 544 static bool expected_to_finish_;
michael@0 545
michael@0 546 // true iff the test function did run to finish.
michael@0 547 static bool finished_;
michael@0 548 """ % DEFS
michael@0 549
michael@0 550 tests += Iter(n, """
michael@0 551 static int n%s_;""")
michael@0 552
michael@0 553 tests += """
michael@0 554 };
michael@0 555
michael@0 556 bool Predicate%(n)sTest::expected_to_finish_;
michael@0 557 bool Predicate%(n)sTest::finished_;
michael@0 558 """ % DEFS
michael@0 559
michael@0 560 tests += Iter(n, """int Predicate%%(n)sTest::n%s_;
michael@0 561 """) % DEFS
michael@0 562
michael@0 563 tests += """
michael@0 564 typedef Predicate%(n)sTest EXPECT_PRED_FORMAT%(n)sTest;
michael@0 565 typedef Predicate%(n)sTest ASSERT_PRED_FORMAT%(n)sTest;
michael@0 566 typedef Predicate%(n)sTest EXPECT_PRED%(n)sTest;
michael@0 567 typedef Predicate%(n)sTest ASSERT_PRED%(n)sTest;
michael@0 568 """ % DEFS
michael@0 569
michael@0 570 def GenTest(use_format, use_assert, expect_failure,
michael@0 571 use_functor, use_user_type):
michael@0 572 """Returns the test for a predicate assertion macro.
michael@0 573
michael@0 574 Args:
michael@0 575 use_format: true iff the assertion is a *_PRED_FORMAT*.
michael@0 576 use_assert: true iff the assertion is a ASSERT_*.
michael@0 577 expect_failure: true iff the assertion is expected to fail.
michael@0 578 use_functor: true iff the first argument of the assertion is
michael@0 579 a functor (as opposed to a function)
michael@0 580 use_user_type: true iff the predicate functor/function takes
michael@0 581 argument(s) of a user-defined type.
michael@0 582
michael@0 583 Example:
michael@0 584
michael@0 585 GenTest(1, 0, 0, 1, 0) returns a test that tests the behavior
michael@0 586 of a successful EXPECT_PRED_FORMATn() that takes a functor
michael@0 587 whose arguments have built-in types."""
michael@0 588
michael@0 589 if use_assert:
michael@0 590 assrt = 'ASSERT' # 'assert' is reserved, so we cannot use
michael@0 591 # that identifier here.
michael@0 592 else:
michael@0 593 assrt = 'EXPECT'
michael@0 594
michael@0 595 assertion = assrt + '_PRED'
michael@0 596
michael@0 597 if use_format:
michael@0 598 pred_format = 'PredFormat'
michael@0 599 assertion += '_FORMAT'
michael@0 600 else:
michael@0 601 pred_format = 'Pred'
michael@0 602
michael@0 603 assertion += '%(n)s' % DEFS
michael@0 604
michael@0 605 if use_functor:
michael@0 606 pred_format_type = 'functor'
michael@0 607 pred_format += 'Functor%(n)s()'
michael@0 608 else:
michael@0 609 pred_format_type = 'function'
michael@0 610 pred_format += 'Function%(n)s'
michael@0 611 if not use_format:
michael@0 612 if use_user_type:
michael@0 613 pred_format += 'Bool'
michael@0 614 else:
michael@0 615 pred_format += 'Int'
michael@0 616
michael@0 617 test_name = pred_format_type.title()
michael@0 618
michael@0 619 if use_user_type:
michael@0 620 arg_type = 'user-defined type (Bool)'
michael@0 621 test_name += 'OnUserType'
michael@0 622 if expect_failure:
michael@0 623 arg = 'Bool(n%s_++)'
michael@0 624 else:
michael@0 625 arg = 'Bool(++n%s_)'
michael@0 626 else:
michael@0 627 arg_type = 'built-in type (int)'
michael@0 628 test_name += 'OnBuiltInType'
michael@0 629 if expect_failure:
michael@0 630 arg = 'n%s_++'
michael@0 631 else:
michael@0 632 arg = '++n%s_'
michael@0 633
michael@0 634 if expect_failure:
michael@0 635 successful_or_failed = 'failed'
michael@0 636 expected_or_not = 'expected.'
michael@0 637 test_name += 'Failure'
michael@0 638 else:
michael@0 639 successful_or_failed = 'successful'
michael@0 640 expected_or_not = 'UNEXPECTED!'
michael@0 641 test_name += 'Success'
michael@0 642
michael@0 643 # A map that defines the values used in the test template.
michael@0 644 defs = DEFS.copy()
michael@0 645 defs.update({
michael@0 646 'assert' : assrt,
michael@0 647 'assertion' : assertion,
michael@0 648 'test_name' : test_name,
michael@0 649 'pf_type' : pred_format_type,
michael@0 650 'pf' : pred_format,
michael@0 651 'arg_type' : arg_type,
michael@0 652 'arg' : arg,
michael@0 653 'successful' : successful_or_failed,
michael@0 654 'expected' : expected_or_not,
michael@0 655 })
michael@0 656
michael@0 657 test = """
michael@0 658 // Tests a %(successful)s %(assertion)s where the
michael@0 659 // predicate-formatter is a %(pf_type)s on a %(arg_type)s.
michael@0 660 TEST_F(%(assertion)sTest, %(test_name)s) {""" % defs
michael@0 661
michael@0 662 indent = (len(assertion) + 3)*' '
michael@0 663 extra_indent = ''
michael@0 664
michael@0 665 if expect_failure:
michael@0 666 extra_indent = ' '
michael@0 667 if use_assert:
michael@0 668 test += """
michael@0 669 expected_to_finish_ = false;
michael@0 670 EXPECT_FATAL_FAILURE({ // NOLINT"""
michael@0 671 else:
michael@0 672 test += """
michael@0 673 EXPECT_NONFATAL_FAILURE({ // NOLINT"""
michael@0 674
michael@0 675 test += '\n' + extra_indent + """ %(assertion)s(%(pf)s""" % defs
michael@0 676
michael@0 677 test = test % defs
michael@0 678 test += Iter(n, ',\n' + indent + extra_indent + '%(arg)s' % defs)
michael@0 679 test += ');\n' + extra_indent + ' finished_ = true;\n'
michael@0 680
michael@0 681 if expect_failure:
michael@0 682 test += ' }, "");\n'
michael@0 683
michael@0 684 test += '}\n'
michael@0 685 return test
michael@0 686
michael@0 687 # Generates tests for all 2**6 = 64 combinations.
michael@0 688 tests += ''.join([GenTest(use_format, use_assert, expect_failure,
michael@0 689 use_functor, use_user_type)
michael@0 690 for use_format in [0, 1]
michael@0 691 for use_assert in [0, 1]
michael@0 692 for expect_failure in [0, 1]
michael@0 693 for use_functor in [0, 1]
michael@0 694 for use_user_type in [0, 1]
michael@0 695 ])
michael@0 696
michael@0 697 return tests
michael@0 698
michael@0 699
michael@0 700 def UnitTestPostamble():
michael@0 701 """Returns the postamble for the tests."""
michael@0 702
michael@0 703 return ''
michael@0 704
michael@0 705
michael@0 706 def GenerateUnitTest(n):
michael@0 707 """Returns the tests for up-to n-ary predicate assertions."""
michael@0 708
michael@0 709 GenerateFile(UNIT_TEST,
michael@0 710 UnitTestPreamble()
michael@0 711 + ''.join([TestsForArity(i) for i in OneTo(n)])
michael@0 712 + UnitTestPostamble())
michael@0 713
michael@0 714
michael@0 715 def _Main():
michael@0 716 """The entry point of the script. Generates the header file and its
michael@0 717 unit test."""
michael@0 718
michael@0 719 if len(sys.argv) != 2:
michael@0 720 print __doc__
michael@0 721 print 'Author: ' + __author__
michael@0 722 sys.exit(1)
michael@0 723
michael@0 724 n = int(sys.argv[1])
michael@0 725 GenerateHeader(n)
michael@0 726 GenerateUnitTest(n)
michael@0 727
michael@0 728
michael@0 729 if __name__ == '__main__':
michael@0 730 _Main()

mercurial