416 """Returns the tests for n-ary predicate assertions."""
417
418
419 DEFS = {
420 'n' : n,
421 'es' :
Iter(n,
'e%s', sep=
', '),
422 'vs' :
Iter(n,
'v%s', sep=
', '),
423 'vts' :
Iter(n,
'#v%s', sep=
', '),
424 'tvs' :
Iter(n,
'T%s v%s', sep=
', '),
425 'int_vs' :
Iter(n,
'int v%s', sep=
', '),
426 'Bool_vs' :
Iter(n,
'Bool v%s', sep=
', '),
427 'types' :
Iter(n,
'typename T%s', sep=
', '),
428 'v_sum' :
Iter(n,
'v%s', sep=
' + '),
431 }
432
433 tests = (
434 """// Sample functions/functors for testing %(arity)s predicate assertions.
435
436// A %(arity)s predicate function.
437template <%(types)s>
438bool PredFunction%(n)s(%(tvs)s) {
439 return %(v_sum)s > 0;
440}
441
442// The following two functions are needed because a compiler doesn't have
443// a context yet to know which template function must be instantiated.
444bool PredFunction%(n)sInt(%(int_vs)s) {
445 return %(v_sum)s > 0;
446}
447bool PredFunction%(n)sBool(%(Bool_vs)s) {
448 return %(v_sum)s > 0;
449}
450""" % DEFS)
451
452 tests += """
453// A %(arity)s predicate functor.
454struct PredFunctor%(n)s {
455 template <%(types)s>
456 bool operator()(""" % DEFS
457
458 tests += Iter(n, 'const T%s& v%s', sep=
""",
459 """)
460
461 tests += """) {
462 return %(v_sum)s > 0;
463 }
464};
465""" % DEFS
466
467 tests += """
468// A %(arity)s predicate-formatter function.
469template <%(types)s>
470testing::AssertionResult PredFormatFunction%(n)s(""" % DEFS
471
472 tests +=
Iter(n,
'const char* e%s', sep=
""",
473 """)
474
475 tests += Iter(n, """,
476 const T%s& v%s""")
477
478 tests += """) {
479 if (PredFunction%(n)s(%(vs)s))
481
483 << """ % DEFS
484
485 tests += Iter(n, 'e%s', sep=
' << " + " << ')
486
487 tests += """
488 << " is expected to be positive, but evaluates to "
489 << %(v_sum)s << ".";
490}
491""" % DEFS
492
493 tests += """
494// A %(arity)s predicate-formatter functor.
495struct PredFormatFunctor%(n)s {
496 template <%(types)s>
497 testing::AssertionResult operator()(""" % DEFS
498
499 tests += Iter(n, 'const char* e%s', sep=
""",
500 """)
501
502 tests += Iter(n, """,
503 const T%s& v%s""")
504
505 tests += """) const {
506 return PredFormatFunction%(n)s(%(es)s, %(vs)s);
507 }
508};
509""" % DEFS
510
511 tests += """
512// Tests for {EXPECT|ASSERT}_PRED_FORMAT%(n)s.
513
514class Predicate%(n)sTest : public testing::Test {
515 protected:
516 void SetUp() override {
517 expected_to_finish_ = true;
518 finished_ = false;""" % DEFS
519
520 tests += """
521 """ + Iter(n, 'n%s_ = ') + """0;
522 }
523"""
524
525 tests += """
526 void TearDown() override {
527 // Verifies that each of the predicate's arguments was evaluated
528 // exactly once."""
529
530 tests += ''.join(["""
532 "The predicate assertion didn't evaluate argument %s "
533 "exactly once.";""" % (i, i + 1) for i in OneTo(n)])
534
535 tests += """
536
537 // Verifies that the control flow in the test function is expected.
538 if (expected_to_finish_ && !finished_) {
539 FAIL() <<
"The predicate assertion unexpactedly aborted the test.";
540 } else if (!expected_to_finish_ && finished_) {
541 FAIL() <<
"The failed predicate assertion didn't abort the test "
542 "as expected.";
543 }
544 }
545
546 // true if and only if the test function is expected to run to finish.
547 static bool expected_to_finish_;
548
549 // true if and only if the test function did run to finish.
550 static bool finished_;
551""" % DEFS
552
554 static int n%s_;""")
555
556 tests += """
557};
558
559bool Predicate%(n)sTest::expected_to_finish_;
560bool Predicate%(n)sTest::finished_;
561""" % DEFS
562
563 tests +=
Iter(n,
"""int Predicate%%(n)sTest::n%s_;
564""") % DEFS
565
566 tests += """
567typedef Predicate%(n)sTest EXPECT_PRED_FORMAT%(n)sTest;
568typedef Predicate%(n)sTest ASSERT_PRED_FORMAT%(n)sTest;
569typedef Predicate%(n)sTest EXPECT_PRED%(n)sTest;
570typedef Predicate%(n)sTest ASSERT_PRED%(n)sTest;
571""" % DEFS
572
573 def GenTest(use_format, use_assert, expect_failure,
574 use_functor, use_user_type):
575 """Returns the test for a predicate assertion macro.
576
577 Args:
578 use_format: true if and only if the assertion is a *_PRED_FORMAT*.
579 use_assert: true if and only if the assertion is a ASSERT_*.
580 expect_failure: true if and only if the assertion is expected to fail.
581 use_functor: true if and only if the first argument of the assertion is
582 a functor (as opposed to a function)
583 use_user_type: true if and only if the predicate functor/function takes
584 argument(s) of a user-defined type.
585
586 Example:
587
588 GenTest(1, 0, 0, 1, 0) returns a test that tests the behavior
589 of a successful EXPECT_PRED_FORMATn() that takes a functor
590 whose arguments have built-in types."""
591
592 if use_assert:
593 assrt = 'ASSERT'
594
595 else:
596 assrt = 'EXPECT'
597
598 assertion = assrt + '_PRED'
599
600 if use_format:
601 pred_format = 'PredFormat'
602 assertion += '_FORMAT'
603 else:
604 pred_format = 'Pred'
605
606 assertion += '%(n)s' % DEFS
607
608 if use_functor:
609 pred_format_type = 'functor'
610 pred_format += 'Functor%(n)s()'
611 else:
612 pred_format_type = 'function'
613 pred_format += 'Function%(n)s'
614 if not use_format:
615 if use_user_type:
616 pred_format += 'Bool'
617 else:
618 pred_format += 'Int'
619
620 test_name = pred_format_type.title()
621
622 if use_user_type:
623 arg_type = 'user-defined type (Bool)'
624 test_name += 'OnUserType'
625 if expect_failure:
626 arg = 'Bool(n%s_++)'
627 else:
628 arg = 'Bool(++n%s_)'
629 else:
630 arg_type = 'built-in type (int)'
631 test_name += 'OnBuiltInType'
632 if expect_failure:
633 arg = 'n%s_++'
634 else:
635 arg = '++n%s_'
636
637 if expect_failure:
638 successful_or_failed = 'failed'
639 expected_or_not = 'expected.'
640 test_name += 'Failure'
641 else:
642 successful_or_failed = 'successful'
643 expected_or_not = 'UNEXPECTED!'
644 test_name += 'Success'
645
646
647 defs = DEFS.copy()
648 defs.update({
649 'assert' : assrt,
650 'assertion' : assertion,
651 'test_name' : test_name,
652 'pf_type' : pred_format_type,
653 'pf' : pred_format,
654 'arg_type' : arg_type,
655 'arg' : arg,
656 'successful' : successful_or_failed,
657 'expected' : expected_or_not,
658 })
659
660 test = """
661// Tests a %(successful)s %(assertion)s where the
662// predicate-formatter is a %(pf_type)s on a %(arg_type)s.
663TEST_F(%(assertion)sTest, %(test_name)s) {""" % defs
664
665 indent = (len(assertion) + 3)*' '
666 extra_indent = ''
667
668 if expect_failure:
669 extra_indent = ' '
670 if use_assert:
671 test += """
672 expected_to_finish_ = false;
673 EXPECT_FATAL_FAILURE({ // NOLINT"""
674 else:
675 test += """
676 EXPECT_NONFATAL_FAILURE({ // NOLINT"""
677
678 test += '\n' + extra_indent + """ %(assertion)s(%(pf)s""" % defs
679
680 test = test % defs
681 test += Iter(n, ',\n' + indent + extra_indent +
'%(arg)s' % defs)
682 test += ');\n' + extra_indent + ' finished_ = true;\n'
683
684 if expect_failure:
685 test += ' }, "");\n'
686
687 test += '}\n'
688 return test
689
690
691 tests += ''.join([GenTest(use_format, use_assert, expect_failure,
692 use_functor, use_user_type)
693 for use_format in [0, 1]
694 for use_assert in [0, 1]
695 for expect_failure in [0, 1]
696 for use_functor in [0, 1]
697 for use_user_type in [0, 1]
698 ])
699
700 return tests
701
702
#define EXPECT_EQ(val1, val2)
AssertionResult AssertionFailure()