Add unittest for covering ErrorBuilder implicit cast and metaprogramming (#2882)

- update unittest of Error, cover the casting of ErrorBuilder.
- add unittest of metaprogramming.h
This commit is contained in:
CanftIn
2023-06-08 09:31:17 -07:00
committed by GitHub
parent e066e96464
commit 9eb040d1c0
3 changed files with 55 additions and 0 deletions
+11
View File
@@ -115,6 +115,17 @@ cc_library(
hdrs = ["metaprogramming.h"],
)
cc_test(
name = "metaprogramming_test",
srcs = ["metaprogramming_test.cpp"],
deps = [
":metaprogramming",
"//common:gtest_main",
"@com_google_googletest//:gtest",
"@llvm-project//llvm:Support",
],
)
cc_library(
name = "ostream",
hdrs = ["ostream.h"],
+12
View File
@@ -97,5 +97,17 @@ TEST(ErrorTest, AssignOrReturnHasErrorInExpected) {
EXPECT_EQ(result.error().message(), "error");
}
TEST(ErrorTest, ErrorBuilderOperatorImplicitCast) {
ErrorOr<int> result1 = ErrorBuilder() << "msg";
ASSERT_FALSE(result1.ok());
EXPECT_EQ(result1.error().message(), "msg");
auto result2 = static_cast<Error>(ErrorBuilder("TestFunc") << "msg");
std::string result2_output;
llvm::raw_string_ostream oss(result2_output);
result2.Print(oss);
EXPECT_EQ(oss.str(), "TestFunc: msg");
}
} // namespace
} // namespace Carbon::Testing
+32
View File
@@ -0,0 +1,32 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common/metaprogramming.h"
#include <gtest/gtest.h>
#include <string>
#include "llvm/Support/raw_ostream.h"
namespace Carbon::Testing {
namespace {
TEST(MetaProgrammingTest, RequiresBasic) {
bool result = Requires<int, int>([](int a, int b) { return a + b; });
EXPECT_TRUE(result);
}
struct TypeWithPrint {
void Print(llvm::raw_ostream& os) const { os << "Test"; }
};
TEST(MetaProgrammingTest, RequiresPrintMethod) {
bool result = Requires<const TypeWithPrint, llvm::raw_ostream>(
[](auto&& t, auto&& out) -> decltype(t.Print(out)) {});
EXPECT_TRUE(result);
}
} // namespace
} // namespace Carbon::Testing