Skip to content

Commit 11ce523

Browse files
committed
Ensure error codes are well-formed
Prevent error code typos by testing that error codes are 'E' followed by three digits.
1 parent 9202bef commit 11ce523

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

test/test-error.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
// Copyright (C) 2020 Matthew "strager" Glazar
22
// See end of file for extended copyright information.
33

4+
#include <gmock/gmock.h>
45
#include <gtest/gtest.h>
56
#include <quick-lint-js/error.h>
67
#include <string>
78
#include <unordered_map>
89

910
namespace quick_lint_js {
1011
namespace {
11-
TEST(test_error, error_codes_are_unique) {
12-
struct error_name_and_code {
13-
const char* name;
14-
const char* code;
15-
};
16-
static constexpr error_name_and_code all_errors[] = {
12+
struct error_name_and_code {
13+
const char* name;
14+
const char* code;
15+
};
16+
static constexpr error_name_and_code all_errors[] = {
1717
#define QLJS_ERROR_TYPE(error_name, error_code, struct_body, format) \
1818
{.name = #error_name, .code = error_code},
19-
QLJS_X_ERROR_TYPES
19+
QLJS_X_ERROR_TYPES
2020
#undef QLJS_ERROR_TYPE
21-
};
21+
};
2222

23+
TEST(test_error, error_codes_are_unique) {
2324
std::unordered_map<std::string, const char*> code_to_error_name;
2425
for (const error_name_and_code& error : all_errors) {
2526
auto existing_it = code_to_error_name.find(error.code);
@@ -32,6 +33,19 @@ TEST(test_error, error_codes_are_unique) {
3233
}
3334
}
3435
}
36+
37+
TEST(test_error, error_codes_are_well_formed) {
38+
for (const error_name_and_code& error : all_errors) {
39+
#if defined(_WIN32)
40+
constexpr const char* error_pattern = R"(^E\d\d\d$)";
41+
#else
42+
constexpr const char* error_pattern = R"(^E[0-9][0-9][0-9]$)";
43+
#endif
44+
// Wrapping the code in std::string improves gtest diagnostics.
45+
EXPECT_THAT(std::string(error.code), ::testing::MatchesRegex(error_pattern))
46+
<< "error " << error.name << " should have a code like E123";
47+
}
48+
}
3549
}
3650
}
3751

0 commit comments

Comments
 (0)