proxygen
gtest_throw_on_failure_ex_test.cc File Reference
#include "gtest/gtest.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdexcept>

Go to the source code of this file.

Functions

void Fail (const char *msg)
 
void TestFailureThrowsRuntimeError ()
 
int main (int argc, char **argv)
 

Function Documentation

void Fail ( const char *  msg)

Definition at line 45 of file gtest_throw_on_failure_ex_test.cc.

45  {
46  printf("FAILURE: %s\n", msg);
47  fflush(stdout);
48  exit(1);
49 }
int main ( int  argc,
char **  argv 
)

Definition at line 81 of file gtest_throw_on_failure_ex_test.cc.

References testing::InitGoogleTest(), and TestFailureThrowsRuntimeError().

81  {
83 
84  // We want to ensure that people can use Google Test assertions in
85  // other testing frameworks, as long as they initialize Google Test
86  // properly and set the thrown-on-failure mode. Therefore, we don't
87  // use Google Test's constructs for defining and running tests
88  // (e.g. TEST and RUN_ALL_TESTS) here.
89 
91  return 0;
92 }
char ** argv
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
void TestFailureThrowsRuntimeError()
void TestFailureThrowsRuntimeError ( )

Definition at line 53 of file gtest_throw_on_failure_ex_test.cc.

References EXPECT_EQ, Fail(), and GTEST_FLAG.

53  {
54  testing::GTEST_FLAG(throw_on_failure) = true;
55 
56  // A successful assertion shouldn't throw.
57  try {
58  EXPECT_EQ(3, 3);
59  } catch(...) {
60  Fail("A successful assertion wrongfully threw.");
61  }
62 
63  // A failed assertion should throw a subclass of std::runtime_error.
64  try {
65  EXPECT_EQ(2, 3) << "Expected failure";
66  } catch(const std::runtime_error& e) {
67  if (strstr(e.what(), "Expected failure") != NULL)
68  return;
69 
70  printf("%s",
71  "A failed assertion did throw an exception of the right type, "
72  "but the message is incorrect. Instead of containing \"Expected "
73  "failure\", it is:\n");
74  Fail(e.what());
75  } catch(...) {
76  Fail("A failed assertion threw the wrong type of exception.");
77  }
78  Fail("A failed assertion should've thrown but didn't.");
79 }
#define GTEST_FLAG(name)
Definition: gtest-port.h:2504
void Fail(const char *msg)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922