Create Error type (#1137)

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Jon Meow
2022-03-17 10:22:02 -07:00
committed by GitHub
co-authored by Geoff Romer Chandler Carruth
parent 31fa2608d4
commit c546c81d07
7 changed files with 203 additions and 38 deletions
+47
View File
@@ -0,0 +1,47 @@
// 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/error.h"
#include <gtest/gtest.h>
namespace Carbon::Testing {
namespace {
TEST(ErrorTest, Error) {
Error err("test");
EXPECT_EQ(err.message(), "test");
}
TEST(ErrorTest, ErrorEmptyString) {
ASSERT_DEATH({ Error err(""); }, "CHECK failure at");
}
auto IndirectError() -> Error { return Error("test"); }
TEST(ErrorTest, IndirectError) { EXPECT_EQ(IndirectError().message(), "test"); }
TEST(ErrorTest, ErrorOr) {
ErrorOr<int> err(Error("test"));
EXPECT_FALSE(err.ok());
EXPECT_EQ(err.error().message(), "test");
}
TEST(ErrorTest, ErrorOrValue) { EXPECT_TRUE(ErrorOr<int>(0).ok()); }
auto IndirectErrorOrTest() -> ErrorOr<int> { return Error("test"); }
TEST(ErrorTest, IndirectErrorOr) { EXPECT_FALSE(IndirectErrorOrTest().ok()); }
struct Val {
int val;
};
TEST(ErrorTest, ErrorOrArrowOp) {
ErrorOr<Val> err({1});
EXPECT_EQ(err->val, 1);
}
} // namespace
} // namespace Carbon::Testing