Skip to content

Commit bec790b

Browse files
committed
ckeck error messages in unit tests, fix some typos in error messges
1 parent 6ca6252 commit bec790b

File tree

5 files changed

+1355
-141
lines changed

5 files changed

+1355
-141
lines changed

include/cpplint_state.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class CppLintState {
124124
// Flush buffers for cout and cerr
125125
void FlushThreadStream();
126126

127+
// Get error buffer as string
128+
std::string GetErrorStreamAsStr();
129+
127130
// Print a summary of errors by category, and the total.
128131
void PrintErrorCounts();
129132
void PrintInfo(const std::string& message);

src/cpplint_state.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ thread_local std::ostringstream cout_buffer;
6060
thread_local std::ostringstream cerr_buffer;
6161

6262
// Flush streams when the buffer size is larger than this value
63-
static const std::streampos FLUSH_THRESHOLD = 512;
63+
static const std::streampos FLUSH_THRESHOLD = 1024;
6464

6565
void CppLintState::PrintInfo(const std::string& message) {
6666
// _quiet does not represent --quiet flag.
@@ -161,3 +161,7 @@ void CppLintState::FlushThreadStream() {
161161
}
162162
}
163163

164+
std::string CppLintState::GetErrorStreamAsStr() {
165+
std::lock_guard<std::mutex> lock(m_mtx);
166+
return cerr_buffer.str();
167+
}

src/file_linter.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ void FileLinter::ParseNolintSuppressions(const std::string& raw_line,
171171
} else if (no_lint_type == "BEGIN") {
172172
if (m_error_suppressions.HasOpenBlock()) {
173173
Error(linenum, "readability/nolint", 5,
174-
"NONLINT block already defined on line " +
175-
m_error_suppressions.GetOpenBlockStart());
174+
"NOLINT block already defined on line " +
175+
std::to_string(m_error_suppressions.GetOpenBlockStart()));
176176
}
177177
ProcessCategory = ProcessCategoryBegin;
178178
} else if (no_lint_type == "END") {
@@ -1415,7 +1415,7 @@ void FileLinter::CheckOperatorSpacing(const CleansedLines& clean_lines,
14151415
!(GetMatchStrView(m_re_result, line, 1) == "operator" &&
14161416
StrIsChar(GetMatchStrView(m_re_result, line, 2), ';'))) {
14171417
Error(linenum, "whitespace/operators", 3,
1418-
"Missing spaces around <<");
1418+
"Missing spaces around <<");
14191419
}
14201420

14211421
// We allow no-spaces around >> for almost anything. This is because
@@ -1773,8 +1773,7 @@ void FileLinter::CheckSpacingForFunctionCallBase(const std::string& line,
17731773
RegexCompile(R"(\w\s*\(\s(?!\s*\\$))");
17741774
static const regex_code RE_PATTERN_PARENS =
17751775
RegexCompile(R"(\(\s+(?!(\s*\\)|\())");
1776-
if (GetLastNonSpace(fncall) == '\\' && // This can avoid the next RegexSearch()
1777-
RegexSearch(RE_PATTERN_FUNC_PARENS, fncall)) {
1776+
if (RegexSearch(RE_PATTERN_FUNC_PARENS, fncall)) {
17781777
// a ( used for a fn call
17791778
Error(linenum, "whitespace/parens", 4,
17801779
"Extra space after ( in function call");
@@ -3660,8 +3659,8 @@ void FileLinter::CheckRedundantOverrideOrFinal(const CleansedLines& clean_lines,
36603659
// Check that at most one of "override" or "final" is present, not both
36613660
if (has_error) {
36623661
Error(linenum, "readability/inheritance", 4,
3663-
"\"override\" is redundant since function is "
3664-
"already declared as \"final\"");
3662+
"\"override\" is redundant since function is "
3663+
"already declared as \"final\"");
36653664
}
36663665
}
36673666

@@ -4021,7 +4020,7 @@ void FileLinter::ProcessFileData(std::vector<std::string>& lines) {
40214020
}
40224021
if (m_error_suppressions.HasOpenBlock()) {
40234022
Error(m_error_suppressions.GetOpenBlockStart(), "readability/nolint", 5,
4024-
"NONLINT block never ended");
4023+
"NOLINT block never ended");
40254024
}
40264025
}
40274026

tests/file_test.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,53 @@ class FileLinterTest : public ::testing::Test {
4040
fs::path file = filename;
4141
linter = FileLinter(file, &cpplint_state, options);
4242
}
43+
44+
void ExpectErrorStr(const char* expected, const char* file, int linenum) {
45+
std::string error_str = cpplint_state.GetErrorStreamAsStr();
46+
EXPECT_STREQ(expected, error_str.c_str()) << " line: " << file << "(" << linenum << ")";
47+
}
4348
};
4449

50+
#define EXPECT_ERROR_STR(expected) ExpectErrorStr(expected, __FILE__, __LINE__)
51+
4552
TEST_F(FileLinterTest, NullBytes) {
4653
filename = "./tests/test_files/nullbytes.c";
4754
ProcessFile();
48-
// Line contains NUL byte.
4955
EXPECT_EQ(2, cpplint_state.ErrorCount());
5056
EXPECT_EQ(2, cpplint_state.ErrorCount("readability/nul"));
57+
const char* expected =
58+
"./tests/test_files/nullbytes.c:1: "
59+
"Line contains NUL byte."
60+
" [readability/nul] [5]\n"
61+
"./tests/test_files/nullbytes.c:2: "
62+
"Line contains NUL byte."
63+
" [readability/nul] [5]\n";
64+
EXPECT_ERROR_STR(expected);
5165
}
5266

5367
TEST_F(FileLinterTest, InvalidUTF) {
5468
filename = "./tests/test_files/invalid_utf.c";
5569
ProcessFile();
56-
// Line contains invalid UTF-8
5770
EXPECT_EQ(2, cpplint_state.ErrorCount());
5871
EXPECT_EQ(2, cpplint_state.ErrorCount("readability/utf8"));
72+
const char* expected =
73+
"./tests/test_files/invalid_utf.c:1: "
74+
"Line contains invalid UTF-8 (or Unicode replacement character)."
75+
" [readability/utf8] [5]\n"
76+
"./tests/test_files/invalid_utf.c:2: "
77+
"Line contains invalid UTF-8 (or Unicode replacement character)."
78+
" [readability/utf8] [5]\n";
79+
EXPECT_ERROR_STR(expected);
5980
}
6081

6182
TEST_F(FileLinterTest, Crlf) {
6283
filename = "./tests/test_files/crlf.c";
6384
ProcessFile();
64-
// Unexpected \r (^M) found
6585
EXPECT_EQ(1, cpplint_state.ErrorCount());
6686
EXPECT_EQ(1, cpplint_state.ErrorCount("whitespace/newline"));
87+
const char* expected =
88+
"./tests/test_files/crlf.c:1: "
89+
"Unexpected \\r (^M) found; better to use only \\n"
90+
" [whitespace/newline] [1]\n";
91+
EXPECT_ERROR_STR(expected);
6792
}

0 commit comments

Comments
 (0)