throw_delegate_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/base/internal/throw_delegate.h"
00016 
00017 #include <functional>
00018 #include <new>
00019 #include <stdexcept>
00020 
00021 #include "gtest/gtest.h"
00022 
00023 namespace {
00024 
00025 using absl::base_internal::ThrowStdLogicError;
00026 using absl::base_internal::ThrowStdInvalidArgument;
00027 using absl::base_internal::ThrowStdDomainError;
00028 using absl::base_internal::ThrowStdLengthError;
00029 using absl::base_internal::ThrowStdOutOfRange;
00030 using absl::base_internal::ThrowStdRuntimeError;
00031 using absl::base_internal::ThrowStdRangeError;
00032 using absl::base_internal::ThrowStdOverflowError;
00033 using absl::base_internal::ThrowStdUnderflowError;
00034 using absl::base_internal::ThrowStdBadFunctionCall;
00035 using absl::base_internal::ThrowStdBadAlloc;
00036 
00037 constexpr const char* what_arg = "The quick brown fox jumps over the lazy dog";
00038 
00039 template <typename E>
00040 void ExpectThrowChar(void (*f)(const char*)) {
00041   try {
00042     f(what_arg);
00043     FAIL() << "Didn't throw";
00044   } catch (const E& e) {
00045     EXPECT_STREQ(e.what(), what_arg);
00046   }
00047 }
00048 
00049 template <typename E>
00050 void ExpectThrowString(void (*f)(const std::string&)) {
00051   try {
00052     f(what_arg);
00053     FAIL() << "Didn't throw";
00054   } catch (const E& e) {
00055     EXPECT_STREQ(e.what(), what_arg);
00056   }
00057 }
00058 
00059 template <typename E>
00060 void ExpectThrowNoWhat(void (*f)()) {
00061   try {
00062     f();
00063     FAIL() << "Didn't throw";
00064   } catch (const E& e) {
00065   }
00066 }
00067 
00068 TEST(ThrowHelper, Test) {
00069   // Not using EXPECT_THROW because we want to check the .what() message too.
00070   ExpectThrowChar<std::logic_error>(ThrowStdLogicError);
00071   ExpectThrowChar<std::invalid_argument>(ThrowStdInvalidArgument);
00072   ExpectThrowChar<std::domain_error>(ThrowStdDomainError);
00073   ExpectThrowChar<std::length_error>(ThrowStdLengthError);
00074   ExpectThrowChar<std::out_of_range>(ThrowStdOutOfRange);
00075   ExpectThrowChar<std::runtime_error>(ThrowStdRuntimeError);
00076   ExpectThrowChar<std::range_error>(ThrowStdRangeError);
00077   ExpectThrowChar<std::overflow_error>(ThrowStdOverflowError);
00078   ExpectThrowChar<std::underflow_error>(ThrowStdUnderflowError);
00079 
00080   ExpectThrowString<std::logic_error>(ThrowStdLogicError);
00081   ExpectThrowString<std::invalid_argument>(ThrowStdInvalidArgument);
00082   ExpectThrowString<std::domain_error>(ThrowStdDomainError);
00083   ExpectThrowString<std::length_error>(ThrowStdLengthError);
00084   ExpectThrowString<std::out_of_range>(ThrowStdOutOfRange);
00085   ExpectThrowString<std::runtime_error>(ThrowStdRuntimeError);
00086   ExpectThrowString<std::range_error>(ThrowStdRangeError);
00087   ExpectThrowString<std::overflow_error>(ThrowStdOverflowError);
00088   ExpectThrowString<std::underflow_error>(ThrowStdUnderflowError);
00089 
00090   ExpectThrowNoWhat<std::bad_function_call>(ThrowStdBadFunctionCall);
00091   ExpectThrowNoWhat<std::bad_alloc>(ThrowStdBadAlloc);
00092 }
00093 
00094 }  // namespace


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