1
1
// Copyright (C) 2020 Matthew "strager" Glazar
2
2
// See end of file for extended copyright information.
3
3
4
+ #include < gmock/gmock.h>
4
5
#include < gtest/gtest.h>
5
6
#include < quick-lint-js/error.h>
6
7
#include < string>
7
8
#include < unordered_map>
8
9
9
10
namespace quick_lint_js {
10
11
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[] = {
17
17
#define QLJS_ERROR_TYPE (error_name, error_code, struct_body, format ) \
18
18
{.name = #error_name, .code = error_code},
19
- QLJS_X_ERROR_TYPES
19
+ QLJS_X_ERROR_TYPES
20
20
#undef QLJS_ERROR_TYPE
21
- };
21
+ };
22
22
23
+ TEST (test_error, error_codes_are_unique) {
23
24
std::unordered_map<std::string, const char *> code_to_error_name;
24
25
for (const error_name_and_code& error : all_errors) {
25
26
auto existing_it = code_to_error_name.find (error.code );
@@ -32,6 +33,19 @@ TEST(test_error, error_codes_are_unique) {
32
33
}
33
34
}
34
35
}
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
+ }
35
49
}
36
50
}
37
51
0 commit comments