[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clangd / unittests / LoggerTests.cpp
blob3d2194d79090d9a32e26a7b220f3c9253fbadd3a
1 //===-- LoggerTests.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "support/Logger.h"
9 #include "llvm/Support/Errc.h"
10 #include "llvm/Support/Error.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
14 namespace clang {
15 namespace clangd {
16 namespace {
18 TEST(ErrorTest, Overloads) {
19 EXPECT_EQ("foo", llvm::toString(error("foo")));
20 // Inconvertible to error code when none is specified.
21 // Don't actually try to convert, it'll crash.
22 handleAllErrors(error("foo"), [&](const llvm::ErrorInfoBase &EI) {
23 EXPECT_EQ(llvm::inconvertibleErrorCode(), EI.convertToErrorCode());
24 });
26 EXPECT_EQ("foo 42", llvm::toString(error("foo {0}", 42)));
27 handleAllErrors(error("foo {0}", 42), [&](const llvm::ErrorInfoBase &EI) {
28 EXPECT_EQ(llvm::inconvertibleErrorCode(), EI.convertToErrorCode());
29 });
31 EXPECT_EQ("foo", llvm::toString(error(llvm::errc::invalid_argument, "foo")));
32 EXPECT_EQ(llvm::errc::invalid_argument,
33 llvm::errorToErrorCode(error(llvm::errc::invalid_argument, "foo")));
35 EXPECT_EQ("foo 42",
36 llvm::toString(error(llvm::errc::invalid_argument, "foo {0}", 42)));
37 EXPECT_EQ(llvm::errc::invalid_argument,
38 llvm::errorToErrorCode(
39 error(llvm::errc::invalid_argument, "foo {0}", 42)));
42 TEST(ErrorTest, Lifetimes) {
43 llvm::Optional<llvm::Error> Err;
45 // Check the error contains the value when error() was called.
46 std::string S = "hello, world";
47 Err = error("S={0}", llvm::StringRef(S));
48 S = "garbage";
50 EXPECT_EQ("S=hello, world", llvm::toString(std::move(*Err)));
53 TEST(ErrorTest, ConsumeError) {
54 llvm::Error Foo = error("foo");
55 llvm::Error Bar = error("bar: {0}", std::move(Foo));
56 EXPECT_EQ("bar: foo", llvm::toString(std::move(Bar)));
57 // No assert for unchecked Foo.
60 } // namespace
61 } // namespace clangd
62 } // namespace clang