throw_delegate_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <functional>
18 #include <new>
19 #include <stdexcept>
20 
21 #include "gtest/gtest.h"
22 
23 namespace {
24 
36 
37 constexpr const char* what_arg = "The quick brown fox jumps over the lazy dog";
38 
39 template <typename E>
40 void ExpectThrowChar(void (*f)(const char*)) {
41  try {
42  f(what_arg);
43  FAIL() << "Didn't throw";
44  } catch (const E& e) {
45  EXPECT_STREQ(e.what(), what_arg);
46  }
47 }
48 
49 template <typename E>
50 void ExpectThrowString(void (*f)(const std::string&)) {
51  try {
52  f(what_arg);
53  FAIL() << "Didn't throw";
54  } catch (const E& e) {
55  EXPECT_STREQ(e.what(), what_arg);
56  }
57 }
58 
59 template <typename E>
60 void ExpectThrowNoWhat(void (*f)()) {
61  try {
62  f();
63  FAIL() << "Didn't throw";
64  } catch (const E& e) {
65  }
66 }
67 
68 TEST(ThrowHelper, Test) {
69  // Not using EXPECT_THROW because we want to check the .what() message too.
70  ExpectThrowChar<std::logic_error>(ThrowStdLogicError);
71  ExpectThrowChar<std::invalid_argument>(ThrowStdInvalidArgument);
72  ExpectThrowChar<std::domain_error>(ThrowStdDomainError);
73  ExpectThrowChar<std::length_error>(ThrowStdLengthError);
74  ExpectThrowChar<std::out_of_range>(ThrowStdOutOfRange);
75  ExpectThrowChar<std::runtime_error>(ThrowStdRuntimeError);
76  ExpectThrowChar<std::range_error>(ThrowStdRangeError);
77  ExpectThrowChar<std::overflow_error>(ThrowStdOverflowError);
78  ExpectThrowChar<std::underflow_error>(ThrowStdUnderflowError);
79 
80  ExpectThrowString<std::logic_error>(ThrowStdLogicError);
81  ExpectThrowString<std::invalid_argument>(ThrowStdInvalidArgument);
82  ExpectThrowString<std::domain_error>(ThrowStdDomainError);
83  ExpectThrowString<std::length_error>(ThrowStdLengthError);
84  ExpectThrowString<std::out_of_range>(ThrowStdOutOfRange);
85  ExpectThrowString<std::runtime_error>(ThrowStdRuntimeError);
86  ExpectThrowString<std::range_error>(ThrowStdRangeError);
87  ExpectThrowString<std::overflow_error>(ThrowStdOverflowError);
88  ExpectThrowString<std::underflow_error>(ThrowStdUnderflowError);
89 
90  ExpectThrowNoWhat<std::bad_function_call>(ThrowStdBadFunctionCall);
91  ExpectThrowNoWhat<std::bad_alloc>(ThrowStdBadAlloc);
92 }
93 
94 } // namespace
void ThrowStdLogicError(const std::string &what_arg)
void ThrowStdInvalidArgument(const std::string &what_arg)
void ThrowStdLengthError(const std::string &what_arg)
void ThrowStdOutOfRange(const std::string &what_arg)
void ThrowStdDomainError(const std::string &what_arg)
void ThrowStdRuntimeError(const std::string &what_arg)
void ThrowStdRangeError(const std::string &what_arg)
void ThrowStdUnderflowError(const std::string &what_arg)
TEST(Symbolize, Unimplemented)
void ThrowStdOverflowError(const std::string &what_arg)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58