diff options
author | amatanhead <amatanhead@yandex-team.ru> | 2022-02-10 16:50:04 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:04 +0300 |
commit | b6f3a80f7c2c8b7dbb0c01b056fdc1fd8cd820e9 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/testing | |
parent | 8879605a63ac17539be5b3bd41b529791f4d4b02 (diff) | |
download | ydb-b6f3a80f7c2c8b7dbb0c01b056fdc1fd8cd820e9.tar.gz |
Restoring authorship annotation for <amatanhead@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/testing')
17 files changed, 722 insertions, 722 deletions
diff --git a/library/cpp/testing/gmock_in_unittest/gmock.h b/library/cpp/testing/gmock_in_unittest/gmock.h index 3afb6ecd09..31f6aee1c3 100644 --- a/library/cpp/testing/gmock_in_unittest/gmock.h +++ b/library/cpp/testing/gmock_in_unittest/gmock.h @@ -1,5 +1,5 @@ #pragma once -#include <library/cpp/testing/gtest_extensions/gtest_extensions.h> +#include <library/cpp/testing/gtest_extensions/gtest_extensions.h> #include <gmock/gmock.h> diff --git a/library/cpp/testing/gmock_in_unittest/ya.make b/library/cpp/testing/gmock_in_unittest/ya.make index 9c9779b06c..5de68ad98d 100644 --- a/library/cpp/testing/gmock_in_unittest/ya.make +++ b/library/cpp/testing/gmock_in_unittest/ya.make @@ -5,7 +5,7 @@ OWNER(galaxycrab) PEERDIR( contrib/restricted/googletest/googlemock contrib/restricted/googletest/googletest - library/cpp/testing/gtest_extensions + library/cpp/testing/gtest_extensions library/cpp/testing/unittest ) diff --git a/library/cpp/testing/gtest_extensions/README.md b/library/cpp/testing/gtest_extensions/README.md index ab9b3dfe4f..5445c7a464 100644 --- a/library/cpp/testing/gtest_extensions/README.md +++ b/library/cpp/testing/gtest_extensions/README.md @@ -1,5 +1,5 @@ -# Extensions for Gtest and Gmock - -Extensions that enable better support of util types in gtest and gmock: pretty printers, matchers, some convenience macros. - +# Extensions for Gtest and Gmock + +Extensions that enable better support of util types in gtest and gmock: pretty printers, matchers, some convenience macros. + If you're using `GTEST`, include `library/cpp/testing/gtest/gtest.h` and it will automatically enable these extensions. This is the preferred way to include gtest and gmock as opposed to including gtest, gmock and extensions directly. It eliminates chances of forgetting to include extensions. diff --git a/library/cpp/testing/gtest_extensions/assertions.cpp b/library/cpp/testing/gtest_extensions/assertions.cpp index 14416daed3..f390409d1b 100644 --- a/library/cpp/testing/gtest_extensions/assertions.cpp +++ b/library/cpp/testing/gtest_extensions/assertions.cpp @@ -1,90 +1,90 @@ -#include "assertions.h" - -#include <util/string/builder.h> -#include <util/string/split.h> +#include "assertions.h" + +#include <util/string/builder.h> +#include <util/string/split.h> #include <util/system/type_name.h> - -namespace NGTest::NInternal { - namespace { - void FormatActual(const std::exception& err, const TBackTrace* bt, TStringBuilder& out) { + +namespace NGTest::NInternal { + namespace { + void FormatActual(const std::exception& err, const TBackTrace* bt, TStringBuilder& out) { out << "an exception of type " << TypeName(err) << " " - << "with message " << TString(err.what()).Quote() << "."; - if (bt) { - out << "\n Trace: "; - for (auto& line: StringSplitter(bt->PrintToString()).Split('\n')) { - out << " " << line.Token() << "\n"; - } - } - } - - void FormatActual(TStringBuilder& out) { - out << " Actual: it throws "; - auto exceptionPtr = std::current_exception(); - if (exceptionPtr) { - try { - std::rethrow_exception(exceptionPtr); - } catch (const yexception& err) { - FormatActual(err, err.BackTrace(), out); - return; - } catch (const std::exception& err) { - FormatActual(err, nullptr, out); - return; - } catch (...) { - out << "an unknown exception."; - return; - } - } - out << "nothing."; - } - - void FormatExpected(const char* statement, const char* type, const TString& contains, TStringBuilder& out) { - out << "Expected: "; - if (TStringBuf(statement).size() > 80) { - out << "statement"; - } else { - out << statement; - } - out << " throws an exception of type " << type; - - if (!contains.empty()) { - out << " with message containing " << contains.Quote(); - } - - out << "."; - } - } - - TString FormatErrorWrongException(const char* statement, const char* type) { - return FormatErrorWrongException(statement, type, ""); - } - - TString FormatErrorWrongException(const char* statement, const char* type, TString contains) { - TStringBuilder out; - - FormatExpected(statement, type, contains, out); - out << "\n"; - FormatActual(out); - - return out; - } - - TString FormatErrorUnexpectedException(const char* statement) { - TStringBuilder out; - - out << "Expected: "; - if (TStringBuf(statement).size() > 80) { - out << "statement"; - } else { - out << statement; - } - out << " doesn't throw an exception.\n "; - - FormatActual(out); - - return out; - } - - bool ExceptionMessageContains(const std::exception& err, TString contains) { - return TStringBuf(err.what()).Contains(contains); - } -} + << "with message " << TString(err.what()).Quote() << "."; + if (bt) { + out << "\n Trace: "; + for (auto& line: StringSplitter(bt->PrintToString()).Split('\n')) { + out << " " << line.Token() << "\n"; + } + } + } + + void FormatActual(TStringBuilder& out) { + out << " Actual: it throws "; + auto exceptionPtr = std::current_exception(); + if (exceptionPtr) { + try { + std::rethrow_exception(exceptionPtr); + } catch (const yexception& err) { + FormatActual(err, err.BackTrace(), out); + return; + } catch (const std::exception& err) { + FormatActual(err, nullptr, out); + return; + } catch (...) { + out << "an unknown exception."; + return; + } + } + out << "nothing."; + } + + void FormatExpected(const char* statement, const char* type, const TString& contains, TStringBuilder& out) { + out << "Expected: "; + if (TStringBuf(statement).size() > 80) { + out << "statement"; + } else { + out << statement; + } + out << " throws an exception of type " << type; + + if (!contains.empty()) { + out << " with message containing " << contains.Quote(); + } + + out << "."; + } + } + + TString FormatErrorWrongException(const char* statement, const char* type) { + return FormatErrorWrongException(statement, type, ""); + } + + TString FormatErrorWrongException(const char* statement, const char* type, TString contains) { + TStringBuilder out; + + FormatExpected(statement, type, contains, out); + out << "\n"; + FormatActual(out); + + return out; + } + + TString FormatErrorUnexpectedException(const char* statement) { + TStringBuilder out; + + out << "Expected: "; + if (TStringBuf(statement).size() > 80) { + out << "statement"; + } else { + out << statement; + } + out << " doesn't throw an exception.\n "; + + FormatActual(out); + + return out; + } + + bool ExceptionMessageContains(const std::exception& err, TString contains) { + return TStringBuf(err.what()).Contains(contains); + } +} diff --git a/library/cpp/testing/gtest_extensions/assertions.h b/library/cpp/testing/gtest_extensions/assertions.h index 9532b5ed96..e8ea07b5df 100644 --- a/library/cpp/testing/gtest_extensions/assertions.h +++ b/library/cpp/testing/gtest_extensions/assertions.h @@ -1,111 +1,111 @@ -#pragma once - -#include <util/generic/string.h> - +#pragma once + +#include <util/generic/string.h> + #include <gtest/gtest.h> #include <gmock/gmock.h> - -/** - * Check that the given statement throws an exception of the given type, - * and that the thrown exception message contains the given substring. - */ -#define EXPECT_THROW_MESSAGE_HAS_SUBSTR(statement, expectedException, substring) \ - _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, GTEST_NONFATAL_FAILURE_) - -/** - * Check that the given statement throws an exception of the given type, - * and that the thrown exception message contains the given substring. - */ -#define ASSERT_THROW_MESSAGE_HAS_SUBSTR(statement, expectedException, substring) \ - _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, GTEST_FATAL_FAILURE_) - - -// Improve default macros. New implementation shows better exception messages. -// See https://github.com/google/googletest/issues/2878 - -#undef EXPECT_THROW -#define EXPECT_THROW(statement, expectedException) \ - _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, GTEST_NONFATAL_FAILURE_) - -#undef ASSERT_THROW -#define ASSERT_THROW(statement, expectedException) \ - _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, GTEST_FATAL_FAILURE_) - -#undef EXPECT_NO_THROW -#define EXPECT_NO_THROW(statement) \ - _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, GTEST_NONFATAL_FAILURE_) - -#undef ASSERT_NO_THROW -#define ASSERT_NO_THROW(statement) \ - _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, GTEST_FATAL_FAILURE_) - - -// Implementation details - -namespace NGTest::NInternal { - TString FormatErrorWrongException(const char* statement, const char* type); - TString FormatErrorWrongException(const char* statement, const char* type, TString contains); - TString FormatErrorUnexpectedException(const char* statement); - bool ExceptionMessageContains(const std::exception& err, TString contains); -} - -#define _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \ - bool gtestCaughtExpected = false; \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } catch (expectedException const&) { \ - gtestCaughtExpected = true; \ - } catch (...) { \ - gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ - #statement, #expectedException); \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ - } if (!gtestCaughtExpected) { \ - gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ - #statement, #expectedException); \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ - fail(gtestMsg.c_str()) - -#define _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \ - bool gtestCaughtExpected = false; \ - ::TString gtestSubstring{substring}; \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } catch (expectedException const& gtestError) { \ - if (!::NGTest::NInternal::ExceptionMessageContains(gtestError, gtestSubstring)) { \ - gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ - #statement, #expectedException, gtestSubstring); \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \ - } \ - gtestCaughtExpected = true; \ - } catch (...) { \ - gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ - #statement, #expectedException, gtestSubstring); \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \ - } if (!gtestCaughtExpected) { \ - gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ - #statement, #expectedException, gtestSubstring); \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__): \ - fail(gtestMsg.c_str()) - -#define _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } catch (...) { \ - gtestMsg = ::NGTest::NInternal::FormatErrorUnexpectedException(#statement); \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ - fail(gtestMsg.c_str()) + +/** + * Check that the given statement throws an exception of the given type, + * and that the thrown exception message contains the given substring. + */ +#define EXPECT_THROW_MESSAGE_HAS_SUBSTR(statement, expectedException, substring) \ + _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, GTEST_NONFATAL_FAILURE_) + +/** + * Check that the given statement throws an exception of the given type, + * and that the thrown exception message contains the given substring. + */ +#define ASSERT_THROW_MESSAGE_HAS_SUBSTR(statement, expectedException, substring) \ + _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, GTEST_FATAL_FAILURE_) + + +// Improve default macros. New implementation shows better exception messages. +// See https://github.com/google/googletest/issues/2878 + +#undef EXPECT_THROW +#define EXPECT_THROW(statement, expectedException) \ + _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, GTEST_NONFATAL_FAILURE_) + +#undef ASSERT_THROW +#define ASSERT_THROW(statement, expectedException) \ + _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, GTEST_FATAL_FAILURE_) + +#undef EXPECT_NO_THROW +#define EXPECT_NO_THROW(statement) \ + _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, GTEST_NONFATAL_FAILURE_) + +#undef ASSERT_NO_THROW +#define ASSERT_NO_THROW(statement) \ + _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, GTEST_FATAL_FAILURE_) + + +// Implementation details + +namespace NGTest::NInternal { + TString FormatErrorWrongException(const char* statement, const char* type); + TString FormatErrorWrongException(const char* statement, const char* type, TString contains); + TString FormatErrorUnexpectedException(const char* statement); + bool ExceptionMessageContains(const std::exception& err, TString contains); +} + +#define _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \ + bool gtestCaughtExpected = false; \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } catch (expectedException const&) { \ + gtestCaughtExpected = true; \ + } catch (...) { \ + gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ + #statement, #expectedException); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } if (!gtestCaughtExpected) { \ + gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ + #statement, #expectedException); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ + fail(gtestMsg.c_str()) + +#define _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \ + bool gtestCaughtExpected = false; \ + ::TString gtestSubstring{substring}; \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } catch (expectedException const& gtestError) { \ + if (!::NGTest::NInternal::ExceptionMessageContains(gtestError, gtestSubstring)) { \ + gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ + #statement, #expectedException, gtestSubstring); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \ + } \ + gtestCaughtExpected = true; \ + } catch (...) { \ + gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ + #statement, #expectedException, gtestSubstring); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \ + } if (!gtestCaughtExpected) { \ + gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \ + #statement, #expectedException, gtestSubstring); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__): \ + fail(gtestMsg.c_str()) + +#define _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } catch (...) { \ + gtestMsg = ::NGTest::NInternal::FormatErrorUnexpectedException(#statement); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ + fail(gtestMsg.c_str()) diff --git a/library/cpp/testing/gtest_extensions/gtest_extensions.cpp b/library/cpp/testing/gtest_extensions/gtest_extensions.cpp index d8631e97ea..1277a804bc 100644 --- a/library/cpp/testing/gtest_extensions/gtest_extensions.cpp +++ b/library/cpp/testing/gtest_extensions/gtest_extensions.cpp @@ -1 +1 @@ -#include "gtest_extensions.h" +#include "gtest_extensions.h" diff --git a/library/cpp/testing/gtest_extensions/gtest_extensions.h b/library/cpp/testing/gtest_extensions/gtest_extensions.h index c90a59bf13..e20532241e 100644 --- a/library/cpp/testing/gtest_extensions/gtest_extensions.h +++ b/library/cpp/testing/gtest_extensions/gtest_extensions.h @@ -1,6 +1,6 @@ #pragma once -#include "assertions.h" -#include "matchers.h" -#include "pretty_printers.h" +#include "assertions.h" +#include "matchers.h" +#include "pretty_printers.h" #include "probe.h" diff --git a/library/cpp/testing/gtest_extensions/matchers.cpp b/library/cpp/testing/gtest_extensions/matchers.cpp index 7eff491aec..7da7be8b3c 100644 --- a/library/cpp/testing/gtest_extensions/matchers.cpp +++ b/library/cpp/testing/gtest_extensions/matchers.cpp @@ -1 +1 @@ -#include "matchers.h" +#include "matchers.h" diff --git a/library/cpp/testing/gtest_extensions/matchers.h b/library/cpp/testing/gtest_extensions/matchers.h index 042baa0764..044c1c3ee4 100644 --- a/library/cpp/testing/gtest_extensions/matchers.h +++ b/library/cpp/testing/gtest_extensions/matchers.h @@ -1,132 +1,132 @@ -#pragma once - -#include <util/generic/string.h> - +#pragma once + +#include <util/generic/string.h> + #include <gtest/gtest.h> #include <gmock/gmock.h> - -namespace testing { - /** - * When matching `const TStringBuf&`, implicitly convert other strings and string views to `Eq` matchers. - */ - template <typename T, typename TT> - class Matcher<const TBasicStringBuf<T, TT>&>: public internal::MatcherBase<const TBasicStringBuf<T, TT>&> { - public: - Matcher() { - } - - explicit Matcher(const MatcherInterface<const TBasicStringBuf<T, TT>&>* impl) - : internal::MatcherBase<const TBasicStringBuf<T, TT>&>(impl) { - } - + +namespace testing { + /** + * When matching `const TStringBuf&`, implicitly convert other strings and string views to `Eq` matchers. + */ + template <typename T, typename TT> + class Matcher<const TBasicStringBuf<T, TT>&>: public internal::MatcherBase<const TBasicStringBuf<T, TT>&> { + public: + Matcher() { + } + + explicit Matcher(const MatcherInterface<const TBasicStringBuf<T, TT>&>* impl) + : internal::MatcherBase<const TBasicStringBuf<T, TT>&>(impl) { + } + template <typename M, typename = typename std::remove_reference<M>::type::is_gtest_matcher> Matcher(M&& m) : internal::MatcherBase<const TBasicStringBuf<T, TT>&>(std::forward<M>(m)) { } - Matcher(const TBasicString<T, TT>& s) { - *this = Eq(TBasicStringBuf<T, TT>(s)); - } - - Matcher(const T* s) { - *this = Eq(TBasicStringBuf<T, TT>(s)); - } - - Matcher(TBasicStringBuf<T, TT> s) { - *this = Eq(s); - } - }; - - /** - * When matching `TBasicBuf`, implicitly convert other strings and string views to `Eq` matchers. - */ - template <typename T, typename TT> - class Matcher<TBasicStringBuf<T, TT>>: public internal::MatcherBase<TBasicStringBuf<T, TT>> { - public: - Matcher() { - } - - explicit Matcher(const MatcherInterface <TBasicStringBuf<T, TT>>* impl) - : internal::MatcherBase<TBasicStringBuf<T, TT>>(impl) { - } - - explicit Matcher(const MatcherInterface<const TBasicStringBuf<T, TT>&>* impl) - : internal::MatcherBase<TBasicStringBuf<T, TT>>(impl) { - } - + Matcher(const TBasicString<T, TT>& s) { + *this = Eq(TBasicStringBuf<T, TT>(s)); + } + + Matcher(const T* s) { + *this = Eq(TBasicStringBuf<T, TT>(s)); + } + + Matcher(TBasicStringBuf<T, TT> s) { + *this = Eq(s); + } + }; + + /** + * When matching `TBasicBuf`, implicitly convert other strings and string views to `Eq` matchers. + */ + template <typename T, typename TT> + class Matcher<TBasicStringBuf<T, TT>>: public internal::MatcherBase<TBasicStringBuf<T, TT>> { + public: + Matcher() { + } + + explicit Matcher(const MatcherInterface <TBasicStringBuf<T, TT>>* impl) + : internal::MatcherBase<TBasicStringBuf<T, TT>>(impl) { + } + + explicit Matcher(const MatcherInterface<const TBasicStringBuf<T, TT>&>* impl) + : internal::MatcherBase<TBasicStringBuf<T, TT>>(impl) { + } + template <typename M, typename = typename std::remove_reference<M>::type::is_gtest_matcher> Matcher(M&& m) : internal::MatcherBase<TBasicStringBuf<T, TT>>(std::forward<M>(m)) { } - Matcher(const TBasicString<T, TT>& s) { - *this = Eq(TBasicString<T, TT>(s)); - } - - Matcher(const T* s) { - *this = Eq(TBasicString<T, TT>(s)); - } - - Matcher(TBasicStringBuf<T, TT> s) { - *this = Eq(s); - } - }; - - /** - * When matching `const TString&`, implicitly convert other strings and string views to `Eq` matchers. - */ - template <typename T, typename TT> - class Matcher<const TBasicString<T, TT>&>: public internal::MatcherBase<const TBasicString<T, TT>&> { - public: - Matcher() { - } - - explicit Matcher(const MatcherInterface<const TBasicString<T, TT>&>* impl) - : internal::MatcherBase<const TBasicString<T, TT>&>(impl) { - } - - Matcher(const TBasicString<T, TT>& s) { - *this = Eq(s); - } - + Matcher(const TBasicString<T, TT>& s) { + *this = Eq(TBasicString<T, TT>(s)); + } + + Matcher(const T* s) { + *this = Eq(TBasicString<T, TT>(s)); + } + + Matcher(TBasicStringBuf<T, TT> s) { + *this = Eq(s); + } + }; + + /** + * When matching `const TString&`, implicitly convert other strings and string views to `Eq` matchers. + */ + template <typename T, typename TT> + class Matcher<const TBasicString<T, TT>&>: public internal::MatcherBase<const TBasicString<T, TT>&> { + public: + Matcher() { + } + + explicit Matcher(const MatcherInterface<const TBasicString<T, TT>&>* impl) + : internal::MatcherBase<const TBasicString<T, TT>&>(impl) { + } + + Matcher(const TBasicString<T, TT>& s) { + *this = Eq(s); + } + template <typename M, typename = typename std::remove_reference<M>::type::is_gtest_matcher> Matcher(M&& m) : internal::MatcherBase<const TBasicString<T, TT>&>(std::forward<M>(m)) { } - Matcher(const T* s) { - *this = Eq(TBasicString<T, TT>(s)); - } - }; - - /** - * When matching `TString`, implicitly convert other strings and string views to `Eq` matchers. - */ - template <typename T, typename TT> - class Matcher<TBasicString<T, TT>>: public internal::MatcherBase<TBasicString<T, TT>> { - public: - Matcher() { - } - - explicit Matcher(const MatcherInterface <TBasicString<T, TT>>* impl) - : internal::MatcherBase<TBasicString<T, TT>>(impl) { - } - - explicit Matcher(const MatcherInterface<const TBasicString<T, TT>&>* impl) - : internal::MatcherBase<TBasicString<T, TT>>(impl) { - } - + Matcher(const T* s) { + *this = Eq(TBasicString<T, TT>(s)); + } + }; + + /** + * When matching `TString`, implicitly convert other strings and string views to `Eq` matchers. + */ + template <typename T, typename TT> + class Matcher<TBasicString<T, TT>>: public internal::MatcherBase<TBasicString<T, TT>> { + public: + Matcher() { + } + + explicit Matcher(const MatcherInterface <TBasicString<T, TT>>* impl) + : internal::MatcherBase<TBasicString<T, TT>>(impl) { + } + + explicit Matcher(const MatcherInterface<const TBasicString<T, TT>&>* impl) + : internal::MatcherBase<TBasicString<T, TT>>(impl) { + } + template <typename M, typename = typename std::remove_reference<M>::type::is_gtest_matcher> Matcher(M&& m) : internal::MatcherBase<TBasicString<T, TT>>(std::forward<M>(m)) { } - Matcher(const TBasicString<T, TT>& s) { - *this = Eq(s); - } - - Matcher(const T* s) { - *this = Eq(TBasicString<T, TT>(s)); - } - }; -} + Matcher(const TBasicString<T, TT>& s) { + *this = Eq(s); + } + + Matcher(const T* s) { + *this = Eq(TBasicString<T, TT>(s)); + } + }; +} diff --git a/library/cpp/testing/gtest_extensions/pretty_printers.cpp b/library/cpp/testing/gtest_extensions/pretty_printers.cpp index 588584303b..401745cbcb 100644 --- a/library/cpp/testing/gtest_extensions/pretty_printers.cpp +++ b/library/cpp/testing/gtest_extensions/pretty_printers.cpp @@ -1 +1 @@ -#include "pretty_printers.h" +#include "pretty_printers.h" diff --git a/library/cpp/testing/gtest_extensions/pretty_printers.h b/library/cpp/testing/gtest_extensions/pretty_printers.h index 28e51700f6..14d8284446 100644 --- a/library/cpp/testing/gtest_extensions/pretty_printers.h +++ b/library/cpp/testing/gtest_extensions/pretty_printers.h @@ -1,84 +1,84 @@ -#pragma once - -#include <util/generic/string.h> -#include <util/generic/strbuf.h> -#include <util/generic/maybe.h> -#include <util/generic/variant.h> -#include <util/stream/output.h> -#include <util/stream/str.h> -#include <util/datetime/base.h> - +#pragma once + +#include <util/generic/string.h> +#include <util/generic/strbuf.h> +#include <util/generic/maybe.h> +#include <util/generic/variant.h> +#include <util/stream/output.h> +#include <util/stream/str.h> +#include <util/datetime/base.h> + #include <gtest/gtest.h> #include <gmock/gmock.h> - -/** - * Automatically define GTest pretty printer for type that can print itself to util's `IOutputStream`. - * - * Note that this macro should be instantiated in the same namespace as the type you're printing, otherwise - * ADL will not find it. - * - * Example: - * - * We define a struct `TMyContainer` and an output operator that works with `IOutputStream`. We then use this macro - * to automatically define GTest pretty printer: - * - * ``` - * namespace NMy { - * struct TMyContainer { - * int x, y; - * }; - * } - * - * template <> - * inline void Out<NMy::TMyContainer>(IOutputStream& stream, TTypeTraits<NMy::TMyContainer>::TFuncParam value) { - * stream << "{ x=" << value.x << ", y=" << value.y << " }"; - * } - * - * namespace NMy { - * Y_GTEST_ARCADIA_PRINTER(TMyContainer) - * } - * ``` - */ + +/** + * Automatically define GTest pretty printer for type that can print itself to util's `IOutputStream`. + * + * Note that this macro should be instantiated in the same namespace as the type you're printing, otherwise + * ADL will not find it. + * + * Example: + * + * We define a struct `TMyContainer` and an output operator that works with `IOutputStream`. We then use this macro + * to automatically define GTest pretty printer: + * + * ``` + * namespace NMy { + * struct TMyContainer { + * int x, y; + * }; + * } + * + * template <> + * inline void Out<NMy::TMyContainer>(IOutputStream& stream, TTypeTraits<NMy::TMyContainer>::TFuncParam value) { + * stream << "{ x=" << value.x << ", y=" << value.y << " }"; + * } + * + * namespace NMy { + * Y_GTEST_ARCADIA_PRINTER(TMyContainer) + * } + * ``` + */ #define Y_GTEST_ARCADIA_PRINTER(T) \ void PrintTo(const T& value, std::ostream* stream) { \ - ::TString ss; \ - ::TStringOutput s{ss}; \ - s << value; \ - *stream << ss; \ - } - - + ::TString ss; \ + ::TStringOutput s{ss}; \ + s << value; \ + *stream << ss; \ + } + + template <typename TCharType, typename TCharTraits> void PrintTo(const TBasicString<TCharType, TCharTraits>& value, std::ostream* stream) { - *stream << value.Quote().c_str(); -} - + *stream << value.Quote().c_str(); +} + template <typename TCharType, typename TCharTraits> void PrintTo(TBasicStringBuf<TCharType, TCharTraits> value, std::ostream* stream) { *stream << TBasicString<TCharType, TCharTraits>{value}.Quote().c_str(); -} - -template <typename T, typename P> +} + +template <typename T, typename P> void PrintTo(const TMaybe<T, P>& value, std::ostream* stream) { - if (value.Defined()) { - ::testing::internal::UniversalPrint(value.GetRef(), stream); - } else { - *stream << "nothing"; - } -} - + if (value.Defined()) { + ::testing::internal::UniversalPrint(value.GetRef(), stream); + } else { + *stream << "nothing"; + } +} + inline void PrintTo(TNothing /* value */, std::ostream* stream) { - *stream << "nothing"; -} - + *stream << "nothing"; +} + inline void PrintTo(std::monostate /* value */, std::ostream* stream) { - *stream << "monostate"; -} - -inline void PrintTo(TInstant value, std::ostream* stream) { - *stream << value.ToString(); -} - -inline void PrintTo(TDuration value, std::ostream* stream) { - *stream << value.ToString(); -} + *stream << "monostate"; +} + +inline void PrintTo(TInstant value, std::ostream* stream) { + *stream << value.ToString(); +} + +inline void PrintTo(TDuration value, std::ostream* stream) { + *stream << value.ToString(); +} diff --git a/library/cpp/testing/gtest_extensions/ut/README.md b/library/cpp/testing/gtest_extensions/ut/README.md index 71fff4e8d3..ee8d212c18 100644 --- a/library/cpp/testing/gtest_extensions/ut/README.md +++ b/library/cpp/testing/gtest_extensions/ut/README.md @@ -1 +1 @@ -Note: integration tests are located in */devtools/ya/test/tests/gtest_beta*. Launch them as well after changing this library. +Note: integration tests are located in */devtools/ya/test/tests/gtest_beta*. Launch them as well after changing this library. diff --git a/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp b/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp index de39aeeb2e..81cdfd0427 100644 --- a/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp +++ b/library/cpp/testing/gtest_extensions/ut/gtest_extensions_ut.cpp @@ -1,9 +1,9 @@ -#include <library/cpp/testing/gtest/gtest.h> +#include <library/cpp/testing/gtest/gtest.h> -#include <util/generic/string.h> -#include <util/generic/maybe.h> -#include <util/stream/output.h> -#include <util/stream/str.h> +#include <util/generic/string.h> +#include <util/generic/maybe.h> +#include <util/stream/output.h> +#include <util/stream/str.h> namespace { class IMock { @@ -24,323 +24,323 @@ namespace { } -TEST(MatchersSpecializations, String) { - TSampleMock mock; +TEST(MatchersSpecializations, String) { + TSampleMock mock; - TStringBuf simpleStringBuf = "SimpleStringBuf"; - const TStringBuf constSimpleStringBuf = "ConstSimpleStringBuf"; + TStringBuf simpleStringBuf = "SimpleStringBuf"; + const TStringBuf constSimpleStringBuf = "ConstSimpleStringBuf"; - TString simpleString = "SimpleString"; - const TString constSimpleString = "ConstSimpleString"; + TString simpleString = "SimpleString"; + const TString constSimpleString = "ConstSimpleString"; - EXPECT_CALL(mock, M1("ConstSimpleStringBuf")).Times(1); - EXPECT_CALL(mock, M2("SimpleStringBuf")).Times(1); - EXPECT_CALL(mock, M3("ConstSimpleString")).Times(1); - EXPECT_CALL(mock, M4("SimpleString")).Times(1); + EXPECT_CALL(mock, M1("ConstSimpleStringBuf")).Times(1); + EXPECT_CALL(mock, M2("SimpleStringBuf")).Times(1); + EXPECT_CALL(mock, M3("ConstSimpleString")).Times(1); + EXPECT_CALL(mock, M4("SimpleString")).Times(1); - mock.M1(constSimpleStringBuf); - mock.M2(simpleStringBuf); - mock.M3(constSimpleString); - mock.M4(simpleString); -} + mock.M1(constSimpleStringBuf); + mock.M2(simpleStringBuf); + mock.M3(constSimpleString); + mock.M4(simpleString); +} + +template <typename T, typename M> +std::pair<bool, std::string> Match(T&& t, M&& m) { + testing::StringMatchResultListener listener; + auto matches = testing::SafeMatcherCast<T>(std::forward<M>(m)).MatchAndExplain(std::forward<T>(t), &listener); + return {matches, listener.str()}; +} -template <typename T, typename M> -std::pair<bool, std::string> Match(T&& t, M&& m) { - testing::StringMatchResultListener listener; - auto matches = testing::SafeMatcherCast<T>(std::forward<M>(m)).MatchAndExplain(std::forward<T>(t), &listener); - return {matches, listener.str()}; -} - -TEST(Matchers, Throws) { +TEST(Matchers, Throws) { auto matcher = testing::Throws<std::runtime_error>(); - - { - std::stringstream ss; - testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); - auto explanation = ss.str(); - - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); - EXPECT_TRUE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw 10; }, matcher); - EXPECT_FALSE(matched); + + { + std::stringstream ss; + testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); + auto explanation = ss.str(); + + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); + EXPECT_TRUE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw 10; }, matcher); + EXPECT_FALSE(matched); EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type")); - } - - { - auto [matched, explanation] = Match([]() { (void)0; }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); - } -} - -TEST(Matchers, ThrowsMessage) { + } + + { + auto [matched, explanation] = Match([]() { (void)0; }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); + } +} + +TEST(Matchers, ThrowsMessage) { auto matcher = testing::ThrowsMessage<std::runtime_error>(testing::HasSubstr("error message")); - - { - std::stringstream ss; - testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); - auto explanation = ss.str(); - - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); - EXPECT_TRUE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw 10; }, matcher); - EXPECT_FALSE(matched); + + { + std::stringstream ss; + testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); + auto explanation = ss.str(); + + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); + EXPECT_TRUE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw 10; }, matcher); + EXPECT_FALSE(matched); EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type")); - } - - { - auto [matched, explanation] = Match([]() { (void)0; }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); - } -} - -TEST(Matchers, ThrowsMessageHasSubstr) { + } + + { + auto [matched, explanation] = Match([]() { (void)0; }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); + } +} + +TEST(Matchers, ThrowsMessageHasSubstr) { auto matcher = testing::ThrowsMessage<std::runtime_error>(testing::HasSubstr("error message")); - - { - std::stringstream ss; - testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); - auto explanation = ss.str(); - - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); - EXPECT_TRUE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw 10; }, matcher); - EXPECT_FALSE(matched); + + { + std::stringstream ss; + testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); + auto explanation = ss.str(); + + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); + EXPECT_TRUE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw 10; }, matcher); + EXPECT_FALSE(matched); EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type")); - } - - { - auto [matched, explanation] = Match([]() { (void)0; }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); - } -} - -TEST(Matchers, ThrowsCondition) { + } + + { + auto [matched, explanation] = Match([]() { (void)0; }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); + } +} + +TEST(Matchers, ThrowsCondition) { auto matcher = testing::Throws<std::runtime_error>( - testing::Property(&std::exception::what, testing::HasSubstr("error message"))); - - { - std::stringstream ss; - testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); - auto explanation = ss.str(); - - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); - EXPECT_TRUE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - } - - { - auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"message error\"")); - } - - { - auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); - EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); - } - - { - auto [matched, explanation] = Match([]() { throw 10; }, matcher); - EXPECT_FALSE(matched); + testing::Property(&std::exception::what, testing::HasSubstr("error message"))); + + { + std::stringstream ss; + testing::SafeMatcherCast<void(*)()>(matcher).DescribeTo(&ss); + auto explanation = ss.str(); + + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("error message"); }, matcher); + EXPECT_TRUE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + } + + { + auto [matched, explanation] = Match([]() { throw std::runtime_error("message error"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"message error\"")); + } + + { + auto [matched, explanation] = Match([]() { throw std::logic_error("error message"); }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("std::logic_error")); + EXPECT_THAT(explanation, testing::HasSubstr("\"error message\"")); + } + + { + auto [matched, explanation] = Match([]() { throw 10; }, matcher); + EXPECT_FALSE(matched); EXPECT_THAT(explanation, testing::HasSubstr("throws an exception of an unknown type")); - } - - { - auto [matched, explanation] = Match([]() { (void)0; }, matcher); - EXPECT_FALSE(matched); - EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); - } -} - -template <typename T> -std::string GtestPrint(T&& v) { - std::stringstream ss; - testing::internal::UniversalPrint(std::forward<T>(v), &ss); - return ss.str(); -} - -struct TThrowsOnMove { - TThrowsOnMove() = default; - TThrowsOnMove(TThrowsOnMove&&) { - ythrow yexception() << "move failed"; } -}; - -TEST(PrettyPrinters, String) { - EXPECT_EQ(GtestPrint(TString("hello world")), "\"hello world\""); - EXPECT_EQ(GtestPrint(TStringBuf("hello world")), "\"hello world\""); -} - -TEST(PrettyPrinters, Maybe) { - EXPECT_EQ(GtestPrint(TMaybe<TString>("hello world")), "\"hello world\""); - EXPECT_EQ(GtestPrint(TMaybe<TString>()), "nothing"); - EXPECT_EQ(GtestPrint(Nothing()), "nothing"); -} - -struct T1 { - int x; -}; - + + { + auto [matched, explanation] = Match([]() { (void)0; }, matcher); + EXPECT_FALSE(matched); + EXPECT_THAT(explanation, testing::HasSubstr("does not throw any exception")); + } +} + +template <typename T> +std::string GtestPrint(T&& v) { + std::stringstream ss; + testing::internal::UniversalPrint(std::forward<T>(v), &ss); + return ss.str(); +} + +struct TThrowsOnMove { + TThrowsOnMove() = default; + TThrowsOnMove(TThrowsOnMove&&) { + ythrow yexception() << "move failed"; + } +}; + +TEST(PrettyPrinters, String) { + EXPECT_EQ(GtestPrint(TString("hello world")), "\"hello world\""); + EXPECT_EQ(GtestPrint(TStringBuf("hello world")), "\"hello world\""); +} + +TEST(PrettyPrinters, Maybe) { + EXPECT_EQ(GtestPrint(TMaybe<TString>("hello world")), "\"hello world\""); + EXPECT_EQ(GtestPrint(TMaybe<TString>()), "nothing"); + EXPECT_EQ(GtestPrint(Nothing()), "nothing"); +} + +struct T1 { + int x; +}; + void PrintTo(T1 value, std::ostream* stream) { - *stream << "T1{" << value.x << "}"; -} - -struct T2 { - int x; -}; - -Y_DECLARE_OUT_SPEC(inline, T2, stream, value) { - stream << "T2{" << value.x << "}"; -} - -Y_GTEST_ARCADIA_PRINTER(T2) - -TEST(PrettyPrinters, Custom) { - EXPECT_EQ(GtestPrint(T1{10}), "T1{10}"); -} - -TEST(PrettyPrinters, CustomArcadia) { - EXPECT_EQ(GtestPrint(T2{10}), "T2{10}"); -} - -TEST(Exceptions, ExpectThrow) { - EXPECT_THROW(ythrow yexception() << "msg", yexception); -} - -TEST(Exceptions, ExpectThrowStructuredBindings) { - auto [a, b] = std::make_pair("a", "b"); - EXPECT_THROW(throw yexception() << a << "-" << b, yexception); -} - -TEST(Exceptions, ExpectThrowSkipInThrowTest) { - // this test should be skipped, not failed - EXPECT_THROW(GTEST_SKIP(), yexception); -} - -TEST(Exceptions, AssertThrow) { - ASSERT_THROW(ythrow yexception() << "msg", yexception); -} - -TEST(Exceptions, AssertThrowStructuredBindings) { - auto [a, b] = std::make_pair("a", "b"); - ASSERT_THROW(throw yexception() << a << "-" << b, yexception); -} - -TEST(Exceptions, AssertThrowSkipInThrowTest) { - // this test should be skipped, not failed - ASSERT_THROW(GTEST_SKIP(), yexception); -} - -TEST(Exceptions, ExpectThrowMessageHasSubstr) { - EXPECT_THROW_MESSAGE_HAS_SUBSTR(ythrow yexception() << "msg", yexception, "msg"); -} - -TEST(Exceptions, ExpectThrowMessageHasSubstrStructuredBindings) { - auto [a, b] = std::make_pair("a", "b"); - EXPECT_THROW_MESSAGE_HAS_SUBSTR(throw yexception() << a << "-" << b, yexception, "-"); -} - -TEST(Exceptions, ExpectThrowMessageHasSubstrSkipInThrowTest) { - // this test should be skipped, not failed - EXPECT_THROW_MESSAGE_HAS_SUBSTR(GTEST_SKIP(), yexception, "-"); -} - -TEST(Exceptions, AssertThrowMessageHasSubstr) { - ASSERT_THROW_MESSAGE_HAS_SUBSTR(ythrow yexception() << "msg", yexception, "msg"); -} - -TEST(Exceptions, AssertThrowMessageHasSubstrStructuredBindings) { - auto [a, b] = std::make_pair("a", "b"); - ASSERT_THROW_MESSAGE_HAS_SUBSTR(throw yexception() << a << "-" << b, yexception, "-"); -} - -TEST(Exceptions, AssertThrowMessageHasSubstrSkipInThrowTest) { - // this test should be skipped, not failed - ASSERT_THROW_MESSAGE_HAS_SUBSTR(GTEST_SKIP(), yexception, "-"); -} - -TEST(Exceptions, ExpectNoThrow) { - EXPECT_NO_THROW((void)0); -} - -TEST(Exceptions, AssertNoThrow) { - ASSERT_NO_THROW((void)0); -} - -TEST(Exceptions, ExpectAnyThrow) { - EXPECT_ANY_THROW(ythrow yexception() << "msg"); -} - -TEST(Exceptions, AssertAnyThrow) { - ASSERT_ANY_THROW(ythrow yexception() << "msg"); -} + *stream << "T1{" << value.x << "}"; +} + +struct T2 { + int x; +}; + +Y_DECLARE_OUT_SPEC(inline, T2, stream, value) { + stream << "T2{" << value.x << "}"; +} + +Y_GTEST_ARCADIA_PRINTER(T2) + +TEST(PrettyPrinters, Custom) { + EXPECT_EQ(GtestPrint(T1{10}), "T1{10}"); +} + +TEST(PrettyPrinters, CustomArcadia) { + EXPECT_EQ(GtestPrint(T2{10}), "T2{10}"); +} + +TEST(Exceptions, ExpectThrow) { + EXPECT_THROW(ythrow yexception() << "msg", yexception); +} + +TEST(Exceptions, ExpectThrowStructuredBindings) { + auto [a, b] = std::make_pair("a", "b"); + EXPECT_THROW(throw yexception() << a << "-" << b, yexception); +} + +TEST(Exceptions, ExpectThrowSkipInThrowTest) { + // this test should be skipped, not failed + EXPECT_THROW(GTEST_SKIP(), yexception); +} + +TEST(Exceptions, AssertThrow) { + ASSERT_THROW(ythrow yexception() << "msg", yexception); +} + +TEST(Exceptions, AssertThrowStructuredBindings) { + auto [a, b] = std::make_pair("a", "b"); + ASSERT_THROW(throw yexception() << a << "-" << b, yexception); +} + +TEST(Exceptions, AssertThrowSkipInThrowTest) { + // this test should be skipped, not failed + ASSERT_THROW(GTEST_SKIP(), yexception); +} + +TEST(Exceptions, ExpectThrowMessageHasSubstr) { + EXPECT_THROW_MESSAGE_HAS_SUBSTR(ythrow yexception() << "msg", yexception, "msg"); +} + +TEST(Exceptions, ExpectThrowMessageHasSubstrStructuredBindings) { + auto [a, b] = std::make_pair("a", "b"); + EXPECT_THROW_MESSAGE_HAS_SUBSTR(throw yexception() << a << "-" << b, yexception, "-"); +} + +TEST(Exceptions, ExpectThrowMessageHasSubstrSkipInThrowTest) { + // this test should be skipped, not failed + EXPECT_THROW_MESSAGE_HAS_SUBSTR(GTEST_SKIP(), yexception, "-"); +} + +TEST(Exceptions, AssertThrowMessageHasSubstr) { + ASSERT_THROW_MESSAGE_HAS_SUBSTR(ythrow yexception() << "msg", yexception, "msg"); +} + +TEST(Exceptions, AssertThrowMessageHasSubstrStructuredBindings) { + auto [a, b] = std::make_pair("a", "b"); + ASSERT_THROW_MESSAGE_HAS_SUBSTR(throw yexception() << a << "-" << b, yexception, "-"); +} + +TEST(Exceptions, AssertThrowMessageHasSubstrSkipInThrowTest) { + // this test should be skipped, not failed + ASSERT_THROW_MESSAGE_HAS_SUBSTR(GTEST_SKIP(), yexception, "-"); +} + +TEST(Exceptions, ExpectNoThrow) { + EXPECT_NO_THROW((void)0); +} + +TEST(Exceptions, AssertNoThrow) { + ASSERT_NO_THROW((void)0); +} + +TEST(Exceptions, ExpectAnyThrow) { + EXPECT_ANY_THROW(ythrow yexception() << "msg"); +} + +TEST(Exceptions, AssertAnyThrow) { + ASSERT_ANY_THROW(ythrow yexception() << "msg"); +} diff --git a/library/cpp/testing/gtest_extensions/ut/ya.make b/library/cpp/testing/gtest_extensions/ut/ya.make index b68cb4645b..39b41cecfd 100644 --- a/library/cpp/testing/gtest_extensions/ut/ya.make +++ b/library/cpp/testing/gtest_extensions/ut/ya.make @@ -1,20 +1,20 @@ GTEST() OWNER( - amatanhead + amatanhead bulatman dancingqueue - prettyboy - thegeorg + prettyboy + thegeorg g:cpp-contrib ) SRCS( - gtest_extensions_ut.cpp + gtest_extensions_ut.cpp probe_ut.cpp ) PEERDIR( - library/cpp/testing/gtest_extensions + library/cpp/testing/gtest_extensions ) END() diff --git a/library/cpp/testing/gtest_extensions/ya.make b/library/cpp/testing/gtest_extensions/ya.make index b4871724bb..e24e81e8bd 100644 --- a/library/cpp/testing/gtest_extensions/ya.make +++ b/library/cpp/testing/gtest_extensions/ya.make @@ -1,10 +1,10 @@ LIBRARY() OWNER( - amatanhead + amatanhead bulatman dancingqueue - prettyboy - thegeorg + prettyboy + thegeorg g:cpp-contrib ) @@ -14,10 +14,10 @@ PEERDIR( ) SRCS( - assertions.cpp - gtest_extensions.cpp - matchers.cpp - pretty_printers.cpp + assertions.cpp + gtest_extensions.cpp + matchers.cpp + pretty_printers.cpp probe.cpp ) diff --git a/library/cpp/testing/unittest/gtest.h b/library/cpp/testing/unittest/gtest.h index 28e40e177d..b6768b1bf0 100644 --- a/library/cpp/testing/unittest/gtest.h +++ b/library/cpp/testing/unittest/gtest.h @@ -1,10 +1,10 @@ #pragma once -// WARNING: this is a legacy header that tries to mimic the gtest interface while using unittest -// under the hood. Avoid using this interface -- use the genuine gtest instead (the GTEST macro). -// If you're already using GTEST macro and you've found yourself here, you probably meant -// to include `library/cpp/testing/gtest/gtest.h`. - +// WARNING: this is a legacy header that tries to mimic the gtest interface while using unittest +// under the hood. Avoid using this interface -- use the genuine gtest instead (the GTEST macro). +// If you're already using GTEST macro and you've found yourself here, you probably meant +// to include `library/cpp/testing/gtest/gtest.h`. + #include "registar.h" #include <util/generic/ymath.h> diff --git a/library/cpp/testing/ya.make b/library/cpp/testing/ya.make index f743b8ae46..6a57ac2ee6 100644 --- a/library/cpp/testing/ya.make +++ b/library/cpp/testing/ya.make @@ -11,10 +11,10 @@ RECURSE( gmock gmock_in_unittest gmock_in_unittest/example_ut - gtest + gtest gtest_boost_extensions - gtest_extensions - gtest_main + gtest_extensions + gtest_main gtest_protobuf hook mock_server |