Files
carbon-lang/common/error_test.cpp
T
c546c81d07 Create Error type (#1137)
Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-03-17 10:22:02 -07:00

48 lines
1.1 KiB
C++

// 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