diff --git a/common/BUILD b/common/BUILD index 6952dd3143a1..7b460ef7c0e3 100644 --- a/common/BUILD +++ b/common/BUILD @@ -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"], diff --git a/common/error_test.cpp b/common/error_test.cpp index 0d9e836fd26f..2a08ed213735 100644 --- a/common/error_test.cpp +++ b/common/error_test.cpp @@ -97,5 +97,17 @@ TEST(ErrorTest, AssignOrReturnHasErrorInExpected) { EXPECT_EQ(result.error().message(), "error"); } +TEST(ErrorTest, ErrorBuilderOperatorImplicitCast) { + ErrorOr result1 = ErrorBuilder() << "msg"; + ASSERT_FALSE(result1.ok()); + EXPECT_EQ(result1.error().message(), "msg"); + + auto result2 = static_cast(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 diff --git a/common/metaprogramming_test.cpp b/common/metaprogramming_test.cpp new file mode 100644 index 000000000000..4a1d29a26465 --- /dev/null +++ b/common/metaprogramming_test.cpp @@ -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 + +#include + +#include "llvm/Support/raw_ostream.h" + +namespace Carbon::Testing { +namespace { + +TEST(MetaProgrammingTest, RequiresBasic) { + bool result = Requires([](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( + [](auto&& t, auto&& out) -> decltype(t.Print(out)) {}); + EXPECT_TRUE(result); +} + +} // namespace +} // namespace Carbon::Testing