Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gtest] Create a shared include directory for gtest utilities.
Many times unit tests for different libraries would like to use the same helper functions for checking common types of errors. This patch adds a common library with helpers for testing things in Support, and introduces helpers in here for integrating the llvm::Error and llvm::Expected<T> classes with gtest and gmock. Normally, we would just be able to write: EXPECT_THAT(someFunction(), succeeded()); but due to some quirks in llvm::Error's move semantics, gmock doesn't make this easy, so two macros EXPECT_THAT_ERROR() and EXPECT_THAT_EXPECTED() are introduced to gloss over the difficulties. Consider this an exception, and possibly only temporary as we look for ways to improve this. Differential Revision: https://reviews.llvm.org/D33059 llvm-svn: 305395
- Loading branch information
Zachary Turner
committed
Jun 14, 2017
1 parent
b6567b1
commit cb30e70
Showing
22 changed files
with
441 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===- llvm/Testing/Support/Error.h ---------------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TESTING_SUPPORT_ERROR_H | ||
#define LLVM_TESTING_SUPPORT_ERROR_H | ||
|
||
#include "llvm/ADT/Optional.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Testing/Support/SupportHelpers.h" | ||
|
||
#include "gmock/gmock.h" | ||
#include <ostream> | ||
|
||
namespace llvm { | ||
namespace detail { | ||
ErrorHolder TakeError(Error Err); | ||
|
||
template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) { | ||
llvm::detail::ExpectedHolder<T> Result; | ||
auto &EH = static_cast<llvm::detail::ErrorHolder &>(Result); | ||
EH = TakeError(Exp.takeError()); | ||
if (Result.Success) | ||
Result.Value = &(*Exp); | ||
return Result; | ||
} | ||
|
||
template <typename T> ExpectedHolder<T> TakeExpected(const Expected<T> &Exp) { | ||
return TakeExpected(const_cast<Expected<T> &>(Exp)); | ||
} | ||
} // namespace detail | ||
|
||
#define EXPECT_THAT_ERROR(Err, Matcher) \ | ||
EXPECT_THAT(llvm::detail::TakeError(Err), Matcher) | ||
#define ASSERT_THAT_ERROR(Err, Matcher) \ | ||
ASSERT_THAT(llvm::detail::TakeError(Err), Matcher) | ||
|
||
#define EXPECT_THAT_EXPECTED(Err, Matcher) \ | ||
EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher) | ||
#define ASSERT_THAT_EXPECTED(Err, Matcher) \ | ||
ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher) | ||
|
||
MATCHER(Succeeded, "") { return arg.Success; } | ||
MATCHER(Failed, "") { return !arg.Success; } | ||
|
||
MATCHER_P(HasValue, value, | ||
"succeeded with value " + testing::PrintToString(value)) { | ||
if (!arg.Success) { | ||
*result_listener << "operation failed"; | ||
return false; | ||
} | ||
|
||
assert(arg.Value.hasValue()); | ||
if (**arg.Value != value) { | ||
*result_listener << "but \"" + testing::PrintToString(**arg.Value) + | ||
"\" != " + testing::PrintToString(value); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===- Testing/Support/SupportHelpers.h -----------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H | ||
#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/Error.h" | ||
|
||
namespace llvm { | ||
namespace detail { | ||
struct ErrorHolder { | ||
bool Success; | ||
std::string Message; | ||
}; | ||
|
||
template <typename T> struct ExpectedHolder : public ErrorHolder { | ||
Optional<T *> Value; | ||
}; | ||
|
||
inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) { | ||
*Out << (Err.Success ? "succeeded" : "failed"); | ||
if (!Err.Success) { | ||
*Out << " (" << StringRef(Err.Message).trim().str() << ")"; | ||
} | ||
} | ||
|
||
template <typename T> | ||
void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) { | ||
if (Item.Success) { | ||
*Out << "succeeded with value \"" << testing::PrintToString(**Item.Value) | ||
<< "\""; | ||
} else { | ||
PrintTo(static_cast<const ErrorHolder &>(Item), Out); | ||
} | ||
} | ||
} // namespace detail | ||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ subdirectories = | |
Support | ||
TableGen | ||
Target | ||
Testing | ||
ToolDrivers | ||
Transforms | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(Support) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
;===- ./lib/Testing/LLVMBuild.txt ------------------------------*- Conf -*--===; | ||
; | ||
; The LLVM Compiler Infrastructure | ||
; | ||
; This file is distributed under the University of Illinois Open Source | ||
; License. See LICENSE.TXT for details. | ||
; | ||
;===------------------------------------------------------------------------===; | ||
; | ||
; This is an LLVMBuild description file for the components in this subdirectory. | ||
; | ||
; For more information on the LLVMBuild system, please see: | ||
; | ||
; http://llvm.org/docs/LLVMBuild.html | ||
; | ||
;===------------------------------------------------------------------------===; | ||
|
||
[common] | ||
subdirectories = Support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
add_llvm_library(LLVMTestingSupport | ||
Error.cpp | ||
|
||
BUILDTREE_ONLY | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${LLVM_MAIN_INCLUDE_DIR}/llvm/Testing/Support | ||
) | ||
|
||
include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include) | ||
include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock/include) | ||
target_link_libraries(LLVMTestingSupport PRIVATE gtest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===- llvm/Testing/Support/Error.cpp -------------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Testing/Support/Error.h" | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
|
||
using namespace llvm; | ||
|
||
llvm::detail::ErrorHolder llvm::detail::TakeError(llvm::Error Err) { | ||
bool Succeeded = !static_cast<bool>(Err); | ||
std::string Message; | ||
if (!Succeeded) | ||
Message = toString(std::move(Err)); | ||
return {Succeeded, Message}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
;===- ./Testing/Support/LLVMBuild.txt --------------------------*- Conf -*--===; | ||
; | ||
; The LLVM Compiler Infrastructure | ||
; | ||
; This file is distributed under the University of Illinois Open Source | ||
; License. See LICENSE.TXT for details. | ||
; | ||
;===------------------------------------------------------------------------===; | ||
; | ||
; This is an LLVMBuild description file for the components in this subdirectory. | ||
; | ||
; For more information on the LLVMBuild system, please see: | ||
; | ||
; http://llvm.org/docs/LLVMBuild.html | ||
; | ||
;===------------------------------------------------------------------------===; | ||
|
||
[component_0] | ||
type = Library | ||
name = TestingSupport | ||
parent = Libraries | ||
required_libraries = Support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
DebugInfoCodeView | ||
) | ||
|
||
set(DebugInfoCodeViewSources | ||
RandomAccessVisitorTest.cpp | ||
TypeIndexDiscoveryTest.cpp | ||
) | ||
|
||
add_llvm_unittest(DebugInfoCodeViewTests | ||
${DebugInfoCodeViewSources} | ||
) | ||
set(LLVM_LINK_COMPONENTS | ||
DebugInfoCodeView | ||
TestingSupport | ||
) | ||
|
||
set(DebugInfoCodeViewSources | ||
RandomAccessVisitorTest.cpp | ||
TypeIndexDiscoveryTest.cpp | ||
) | ||
|
||
add_llvm_unittest(DebugInfoCodeViewTests | ||
${DebugInfoCodeViewSources} | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.