forked from quick-lint/quick-lint-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-diagnostic.cpp
125 lines (116 loc) · 5.19 KB
/
test-diagnostic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (C) 2020 Matthew "strager" Glazar
// See end of file for extended copyright information.
#include <cstddef>
#include <gtest/gtest.h>
#include <quick-lint-js/diag/diagnostic-types.h>
#include <quick-lint-js/diag/diagnostic.h>
#include <quick-lint-js/port/char8.h>
#include <string_view>
using namespace std::literals::string_view_literals;
namespace quick_lint_js {
namespace {
template <class Error>
inline const diagnostic_info& diagnostic_info_for_error =
get_diagnostic_info(diag_type_from_type<Error>);
TEST(test_diagnostic, diagnostic_info) {
translator source_code_translator;
source_code_translator.use_messages_from_source_code();
{
const diagnostic_info& info = diagnostic_info_for_error<
diag_expected_parentheses_around_if_condition>;
EXPECT_EQ(info.code, 17);
EXPECT_EQ(info.severity, diagnostic_severity::error);
EXPECT_EQ(source_code_translator.translate(info.message_formats[0]),
u8"if statement needs parentheses around condition"_sv);
EXPECT_EQ(
info.message_args[0][0].offset(),
offsetof(diag_expected_parentheses_around_if_condition, condition));
EXPECT_EQ(info.message_args[0][0].type,
diagnostic_arg_type::source_code_span);
EXPECT_FALSE(info.message_formats[1].valid());
}
{
const diagnostic_info& info = diagnostic_info_for_error<
diag_expected_parenthesis_around_if_condition>;
EXPECT_EQ(info.code, 18);
EXPECT_EQ(info.severity, diagnostic_severity::error);
EXPECT_EQ(source_code_translator.translate(info.message_formats[0]),
u8"if statement is missing '{1}' around condition"_sv);
EXPECT_EQ(info.message_args[0][0].offset(),
offsetof(diag_expected_parenthesis_around_if_condition, where));
EXPECT_EQ(info.message_args[0][0].type,
diagnostic_arg_type::source_code_span);
EXPECT_EQ(info.message_args[0][1].offset(),
offsetof(diag_expected_parenthesis_around_if_condition, token));
EXPECT_EQ(info.message_args[0][1].type, diagnostic_arg_type::char8);
EXPECT_FALSE(info.message_formats[1].valid());
}
{
const diagnostic_info& info = diagnostic_info_for_error<
diag_function_call_before_declaration_in_block_scope>;
EXPECT_EQ(info.code, 77);
EXPECT_EQ(info.severity, diagnostic_severity::warning);
EXPECT_EQ(source_code_translator.translate(info.message_formats[0]),
u8"function called before declaration in block scope: {0}"_sv);
EXPECT_EQ(
info.message_args[0][0].offset(),
offsetof(diag_function_call_before_declaration_in_block_scope, use));
EXPECT_EQ(info.message_args[0][0].type, diagnostic_arg_type::identifier);
EXPECT_EQ(source_code_translator.translate(info.message_formats[1]),
u8"function declared here"_sv);
EXPECT_EQ(info.message_args[1][0].offset(),
offsetof(diag_function_call_before_declaration_in_block_scope,
declaration));
EXPECT_EQ(info.message_args[1][0].type, diagnostic_arg_type::identifier);
}
{
const diagnostic_info& info =
diagnostic_info_for_error<diag_class_statement_not_allowed_in_body>;
EXPECT_EQ(info.code, 149);
EXPECT_EQ(info.severity, diagnostic_severity::error);
EXPECT_EQ(source_code_translator.translate(info.message_formats[0]),
u8"missing body for {1:headlinese}"_sv);
EXPECT_EQ(
info.message_args[0][0].offset(),
offsetof(diag_class_statement_not_allowed_in_body, expected_body));
EXPECT_EQ(info.message_args[0][0].type,
diagnostic_arg_type::source_code_span);
EXPECT_EQ(
info.message_args[0][1].offset(),
offsetof(diag_class_statement_not_allowed_in_body, kind_of_statement));
EXPECT_EQ(info.message_args[0][1].type,
diagnostic_arg_type::statement_kind);
EXPECT_EQ(
source_code_translator.translate(info.message_formats[1]),
u8"a class statement is not allowed as the body of {1:singular}"_sv);
EXPECT_EQ(
info.message_args[1][0].offset(),
offsetof(diag_class_statement_not_allowed_in_body, class_keyword));
EXPECT_EQ(info.message_args[1][0].type,
diagnostic_arg_type::source_code_span);
EXPECT_EQ(
info.message_args[1][1].offset(),
offsetof(diag_class_statement_not_allowed_in_body, kind_of_statement));
EXPECT_EQ(info.message_args[1][1].type,
diagnostic_arg_type::statement_kind);
}
}
}
}
// quick-lint-js finds bugs in JavaScript programs.
// Copyright (C) 2020 Matthew "strager" Glazar
//
// This file is part of quick-lint-js.
//
// quick-lint-js is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// quick-lint-js is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.