gtest_throw_on_failure_ex_test.cc revision 13481
12141SN/A// Copyright 2009, Google Inc.
21376SN/A// All rights reserved.
31376SN/A//
41376SN/A// Redistribution and use in source and binary forms, with or without
51376SN/A// modification, are permitted provided that the following conditions are
61376SN/A// met:
71376SN/A//
81376SN/A//     * Redistributions of source code must retain the above copyright
91376SN/A// notice, this list of conditions and the following disclaimer.
101376SN/A//     * Redistributions in binary form must reproduce the above
111376SN/A// copyright notice, this list of conditions and the following disclaimer
121376SN/A// in the documentation and/or other materials provided with the
131376SN/A// distribution.
141376SN/A//     * Neither the name of Google Inc. nor the names of its
151376SN/A// contributors may be used to endorse or promote products derived from
161376SN/A// this software without specific prior written permission.
171376SN/A//
181376SN/A// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191376SN/A// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201376SN/A// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211376SN/A// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221376SN/A// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231376SN/A// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241376SN/A// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251376SN/A// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261376SN/A// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271376SN/A// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281376SN/A// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291428SN/A//
301428SN/A// Author: wan@google.com (Zhanyong Wan)
315618Snate@binkert.org
325618Snate@binkert.org// Tests Google Test's throw-on-failure mode with exceptions enabled.
331881SN/A
341881SN/A#include "gtest/gtest.h"
351881SN/A
361881SN/A#include <stdlib.h>
371881SN/A#include <stdio.h>
385467Snate@binkert.org#include <string.h>
391881SN/A#include <stdexcept>
401881SN/A
411881SN/A// Prints the given failure message and exits the program with
421881SN/A// non-zero.  We use this instead of a Google Test assertion to
431881SN/A// indicate a failure, as the latter is been tested and cannot be
445618Snate@binkert.org// relied on.
455618Snate@binkert.orgvoid Fail(const char* msg) {
465618Snate@binkert.org  printf("FAILURE: %s\n", msg);
475618Snate@binkert.org  fflush(stdout);
485618Snate@binkert.org  exit(1);
495618Snate@binkert.org}
505618Snate@binkert.org
515618Snate@binkert.org// Tests that an assertion failure throws a subclass of
525618Snate@binkert.org// std::runtime_error.
535618Snate@binkert.orgvoid TestFailureThrowsRuntimeError() {
545618Snate@binkert.org  testing::GTEST_FLAG(throw_on_failure) = true;
555618Snate@binkert.org
565618Snate@binkert.org  // A successful assertion shouldn't throw.
575618Snate@binkert.org  try {
585618Snate@binkert.org    EXPECT_EQ(3, 3);
595618Snate@binkert.org  } catch(...) {
605618Snate@binkert.org    Fail("A successful assertion wrongfully threw.");
615618Snate@binkert.org  }
625618Snate@binkert.org
635618Snate@binkert.org  // A failed assertion should throw a subclass of std::runtime_error.
645618Snate@binkert.org  try {
655618Snate@binkert.org    EXPECT_EQ(2, 3) << "Expected failure";
665618Snate@binkert.org  } catch(const std::runtime_error& e) {
675467Snate@binkert.org    if (strstr(e.what(), "Expected failure") != NULL)
685467Snate@binkert.org      return;
691881SN/A
701881SN/A    printf("%s",
711881SN/A           "A failed assertion did throw an exception of the right type, "
721881SN/A           "but the message is incorrect.  Instead of containing \"Expected "
731881SN/A           "failure\", it is:\n");
741881SN/A    Fail(e.what());
751881SN/A  } catch(...) {
761881SN/A    Fail("A failed assertion threw the wrong type of exception.");
771881SN/A  }
785467Snate@binkert.org  Fail("A failed assertion should've thrown but didn't.");
795467Snate@binkert.org}
805467Snate@binkert.org
815467Snate@binkert.orgint main(int argc, char** argv) {
825467Snate@binkert.org  testing::InitGoogleTest(&argc, argv);
831881SN/A
841881SN/A  // We want to ensure that people can use Google Test assertions in
855467Snate@binkert.org  // other testing frameworks, as long as they initialize Google Test
865467Snate@binkert.org  // properly and set the thrown-on-failure mode.  Therefore, we don't
875467Snate@binkert.org  // use Google Test's constructs for defining and running tests
885467Snate@binkert.org  // (e.g. TEST and RUN_ALL_TESTS) here.
895467Snate@binkert.org
905467Snate@binkert.org  TestFailureThrowsRuntimeError();
915467Snate@binkert.org  return 0;
925467Snate@binkert.org}
935467Snate@binkert.org