|
9 | 9 |
|
10 | 10 | namespace quick_lint_js { |
11 | 11 | namespace { |
12 | | -struct error_name_and_code { |
| 12 | +struct diag_name_and_code { |
13 | 13 | const char* name; |
14 | 14 | const char* code; |
15 | 15 | }; |
16 | | -static constexpr error_name_and_code all_errors[] = { |
17 | | -#define QLJS_DIAG_TYPE(error_name, error_code, severity, struct_body, format) \ |
18 | | - {.name = #error_name, .code = error_code}, |
| 16 | +static constexpr diag_name_and_code all_diags[] = { |
| 17 | +#define QLJS_DIAG_TYPE(diag_name, diag_code, severity, struct_body, format) \ |
| 18 | + {.name = #diag_name, .code = diag_code}, |
19 | 19 | QLJS_X_DIAG_TYPES |
20 | 20 | #undef QLJS_DIAG_TYPE |
21 | 21 | }; |
22 | 22 |
|
23 | | -std::string next_unused_error_code() { |
| 23 | +std::string next_unused_diag_code() { |
24 | 24 | for (int i = 1; i <= 9999; ++i) { |
25 | 25 | char code[7]; |
26 | 26 | std::snprintf(code, sizeof(code), "E%04d", i); |
27 | 27 | auto existing_it = |
28 | | - std::find_if(std::begin(all_errors), std::end(all_errors), |
29 | | - [&](const error_name_and_code& error) { |
30 | | - return std::string_view(error.code) == code; |
| 28 | + std::find_if(std::begin(all_diags), std::end(all_diags), |
| 29 | + [&](const diag_name_and_code& diag) { |
| 30 | + return std::string_view(diag.code) == code; |
31 | 31 | }); |
32 | | - if (existing_it == std::end(all_errors)) { |
| 32 | + if (existing_it == std::end(all_diags)) { |
33 | 33 | return std::string(code); |
34 | 34 | } |
35 | 35 | } |
36 | 36 | QLJS_UNIMPLEMENTED(); |
37 | 37 | } |
38 | 38 |
|
39 | | -TEST(test_error, error_codes_are_unique) { |
40 | | - std::unordered_map<std::string, const char*> code_to_error_name; |
41 | | - for (const error_name_and_code& error : all_errors) { |
42 | | - auto existing_it = code_to_error_name.find(error.code); |
43 | | - if (existing_it == code_to_error_name.end()) { |
44 | | - code_to_error_name.emplace(error.code, error.name); |
| 39 | +TEST(test_diag_code, diag_codes_are_unique) { |
| 40 | + std::unordered_map<std::string, const char*> code_to_diag_name; |
| 41 | + for (const diag_name_and_code& diag : all_diags) { |
| 42 | + auto existing_it = code_to_diag_name.find(diag.code); |
| 43 | + if (existing_it == code_to_diag_name.end()) { |
| 44 | + code_to_diag_name.emplace(diag.code, diag.name); |
45 | 45 | } else { |
46 | | - ADD_FAILURE() << "error code " << error.code |
47 | | - << " used for multiple errors: " << error.name << ", " |
48 | | - << existing_it->second << "\ntry this unused error code: " |
49 | | - << next_unused_error_code(); |
| 46 | + ADD_FAILURE() << "diag code " << diag.code |
| 47 | + << " used for multiple diags: " << diag.name << ", " |
| 48 | + << existing_it->second << "\ntry this unused diag code: " |
| 49 | + << next_unused_diag_code(); |
50 | 50 | } |
51 | 51 | } |
52 | 52 | } |
53 | 53 |
|
54 | | -TEST(test_error, error_codes_are_well_formed) { |
55 | | - for (const error_name_and_code& error : all_errors) { |
| 54 | +TEST(test_diag_code, diag_codes_are_well_formed) { |
| 55 | + for (const diag_name_and_code& diag : all_diags) { |
56 | 56 | #if defined(_WIN32) |
57 | | - constexpr const char* error_pattern = R"(^E\d\d\d\d$)"; |
| 57 | + constexpr const char* code_pattern = R"(^E\d\d\d\d$)"; |
58 | 58 | #else |
59 | | - constexpr const char* error_pattern = R"(^E[0-9][0-9][0-9][0-9]$)"; |
| 59 | + constexpr const char* code_pattern = R"(^E[0-9][0-9][0-9][0-9]$)"; |
60 | 60 | #endif |
61 | 61 | // Wrapping the code in std::string improves gtest diagnostics. |
62 | | - EXPECT_THAT(std::string(error.code), ::testing::MatchesRegex(error_pattern)) |
63 | | - << "error " << error.name << " should have a code like E1234" |
64 | | - << "\ntry this unused error code: " << next_unused_error_code(); |
| 62 | + EXPECT_THAT(std::string(diag.code), ::testing::MatchesRegex(code_pattern)) |
| 63 | + << "diag " << diag.name << " should have a code like E1234" |
| 64 | + << "\ntry this unused diag code: " << next_unused_diag_code(); |
65 | 65 | } |
66 | 66 | } |
67 | 67 | } |
|
0 commit comments