[Workflow] Try to fix code-formatter failing to find changes in some cases.
[llvm-project.git] / lldb / unittests / Utility / StatusTest.cpp
bloba663197ca89deaad34be5ee3dbd5d18fe4f56c4e
1 //===-- StatusTest.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 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/Status.h"
10 #include "gtest/gtest.h"
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
16 using namespace lldb_private;
17 using namespace lldb;
19 TEST(StatusTest, Formatv) {
20 EXPECT_EQ("", llvm::formatv("{0}", Status()).str());
21 EXPECT_EQ("Hello Status", llvm::formatv("{0}", Status("Hello Status")).str());
22 EXPECT_EQ("Hello", llvm::formatv("{0:5}", Status("Hello Error")).str());
25 TEST(StatusTest, ErrorConstructor) {
26 EXPECT_TRUE(Status(llvm::Error::success()).Success());
28 Status eagain(
29 llvm::errorCodeToError(std::error_code(EAGAIN, std::generic_category())));
30 EXPECT_TRUE(eagain.Fail());
31 EXPECT_EQ(eErrorTypePOSIX, eagain.GetType());
32 EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError());
34 Status foo(llvm::make_error<llvm::StringError>(
35 "foo", llvm::inconvertibleErrorCode()));
36 EXPECT_TRUE(foo.Fail());
37 EXPECT_EQ(eErrorTypeGeneric, foo.GetType());
38 EXPECT_STREQ("foo", foo.AsCString());
40 foo = llvm::Error::success();
41 EXPECT_TRUE(foo.Success());
44 TEST(StatusTest, ErrorCodeConstructor) {
45 EXPECT_TRUE(Status(std::error_code()).Success());
47 Status eagain = std::error_code(EAGAIN, std::generic_category());
48 EXPECT_TRUE(eagain.Fail());
49 EXPECT_EQ(eErrorTypePOSIX, eagain.GetType());
50 EXPECT_EQ(Status::ValueType(EAGAIN), eagain.GetError());
53 TEST(StatusTest, ErrorConversion) {
54 EXPECT_FALSE(bool(Status().ToError()));
56 llvm::Error eagain = Status(EAGAIN, ErrorType::eErrorTypePOSIX).ToError();
57 EXPECT_TRUE(bool(eagain));
58 std::error_code ec = llvm::errorToErrorCode(std::move(eagain));
59 EXPECT_EQ(EAGAIN, ec.value());
60 EXPECT_EQ(std::generic_category(), ec.category());
62 llvm::Error foo = Status("foo").ToError();
63 EXPECT_TRUE(bool(foo));
64 EXPECT_EQ("foo", llvm::toString(std::move(foo)));
67 #ifdef _WIN32
68 TEST(StatusTest, ErrorWin32) {
69 auto success = Status(NO_ERROR, ErrorType::eErrorTypeWin32);
70 EXPECT_STREQ(NULL, success.AsCString());
71 EXPECT_FALSE(success.ToError());
72 EXPECT_TRUE(success.Success());
74 WCHAR name[128]{};
75 ULONG nameLen = std::size(name);
76 ULONG langs = 0;
77 GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &langs,
78 reinterpret_cast<PZZWSTR>(&name), &nameLen);
79 // Skip the following tests on non-English, non-US, locales because the
80 // formatted messages will be different.
81 bool skip = wcscmp(L"en-US", name) != 0;
83 auto s = Status(ERROR_ACCESS_DENIED, ErrorType::eErrorTypeWin32);
84 EXPECT_TRUE(s.Fail());
85 if (!skip)
86 EXPECT_STREQ("Access is denied. ", s.AsCString());
88 s.SetError(ERROR_IPSEC_IKE_TIMED_OUT, ErrorType::eErrorTypeWin32);
89 if (!skip)
90 EXPECT_STREQ("Negotiation timed out ", s.AsCString());
92 s.SetError(16000, ErrorType::eErrorTypeWin32);
93 if (!skip)
94 EXPECT_STREQ("unknown error", s.AsCString());
96 #endif