From 5e82f32b3fe6a3862fd3a6d2eda975cbd995e71c Mon Sep 17 00:00:00 2001 From: thk123 Date: Mon, 7 Jan 2019 18:00:47 +0000 Subject: [PATCH 1/4] Add comment to CloseExpression --- scripts/cpplint.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index 2b0e42abcc5..271ab4570c5 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -1745,6 +1745,9 @@ def CloseExpression(clean_lines, linenum, pos): and have CloseExpression be just a simple lookup, but due to preprocessor tricks, this is not so easy. + Note this operates on the *elided lines* - the position will be wrong if you + want strings (or comments) to be included. + Args: clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. From 5c03921143f8896ae914cc69185c72d7a86919d7 Mon Sep 17 00:00:00 2001 From: thk123 Date: Tue, 8 Jan 2019 12:05:52 +0000 Subject: [PATCH 2/4] Add a method that concatenates section of the code Starting at elided_lines[start_line][linepos] take the end of the line until the ending line. --- scripts/cpplint.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index 271ab4570c5..b806d8f640c 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -52,6 +52,7 @@ import string import sys import unicodedata +import itertools _USAGE = """ @@ -6121,6 +6122,32 @@ def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error): 'For C++11-compatibility, omit template arguments from make_pair' ' OR use pair directly OR if appropriate, construct a pair directly') +def AssembleString(clean_lines, start_lineno, start_linepos, end_lineno, end_linepos): + """ + Concatenates all the strings between the starting line position and + the ending line position. + + Note: this operated on the elided (no strings and comments) view of the + code. + + Args: + clean_lines: All the lines + start_lineno: The starting line number + start_linepos: The index of the first character to include + end_lineno: The last line + end_linepos: The index of the first character not to include + """ + if end_lineno < start_lineno: + return '' + + if start_lineno == end_lineno: + return clean_lines.elided[start_lineno][start_linepos : end_linepos] + + full_string = clean_lines.elided[start_lineno][start_linepos:] + for line in itertools.islice(clean_lines.elided, start_lineno + 1, end_lineno): + full_string += line + full_string += clean_lines.elided[end_lineno][:end_linepos] + return full_string def CheckRedundantVirtual(filename, clean_lines, linenum, error): """Check if line contains a redundant "virtual" function-specifier. From e80eec3d3531c353fed75845aca672e506a569ba Mon Sep 17 00:00:00 2001 From: thk123 Date: Tue, 8 Jan 2019 12:06:09 +0000 Subject: [PATCH 3/4] Add test to linter that verifies that all tests have tags --- scripts/cpplint.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index b806d8f640c..dda2d8a9bc0 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -253,6 +253,7 @@ 'runtime/string', 'runtime/threadsafe_fn', 'runtime/vlog', + 'runtime/catch_scenario', 'whitespace/blank_line', 'whitespace/braces', 'whitespace/comma', @@ -6149,6 +6150,25 @@ def AssembleString(clean_lines, start_lineno, start_linepos, end_lineno, end_lin full_string += clean_lines.elided[end_lineno][:end_linepos] return full_string +def CheckScenarioHasTags(filename, clean_lines, linenum, error): + line = clean_lines.elided[linenum] + scenario = Match(r'^SCENARIO\(', line) + if not scenario: return + + closing_line, closing_linenum, closing_pos = CloseExpression(clean_lines, linenum, line.find('(')) + + if closing_pos == -1: + error(filename, linenum, + 'runtime/catch_scenario', 4, "Can't find closing bracket for scenario") + + full_string = AssembleString( + clean_lines, linenum, line.find('(') + 1, closing_linenum, closing_pos - 1) + + if(full_string.find(',') == -1): + error(filename, linenum, + 'runtime/catch_scenario', 4, "Can't find `,\' seperating scenario name and the tags: " + str(clean_lines.lines[linenum])) + + def CheckRedundantVirtual(filename, clean_lines, linenum, error): """Check if line contains a redundant "virtual" function-specifier. @@ -6342,6 +6362,7 @@ def ProcessLine(filename, file_extension, clean_lines, line, CheckInvalidIncrement(filename, clean_lines, line, error) CheckMakePairUsesDeduction(filename, clean_lines, line, error) CheckRedundantVirtual(filename, clean_lines, line, error) + CheckScenarioHasTags(filename, clean_lines, line, error) CheckNamespaceOrUsing(filename, clean_lines, line, error) CheckForEndl(filename, clean_lines, line, error) for check_fn in extra_check_functions: From 1b14892c9278e2fb56624984134d2bfc866b2470 Mon Sep 17 00:00:00 2001 From: thk123 Date: Wed, 9 Jan 2019 10:54:01 +0000 Subject: [PATCH 4/4] Check test cases as well as scenarios --- scripts/cpplint.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/scripts/cpplint.py b/scripts/cpplint.py index dda2d8a9bc0..d34fd63f6b8 100755 --- a/scripts/cpplint.py +++ b/scripts/cpplint.py @@ -253,7 +253,7 @@ 'runtime/string', 'runtime/threadsafe_fn', 'runtime/vlog', - 'runtime/catch_scenario', + 'runtime/catch_test_tags', 'whitespace/blank_line', 'whitespace/braces', 'whitespace/comma', @@ -6150,24 +6150,23 @@ def AssembleString(clean_lines, start_lineno, start_linepos, end_lineno, end_lin full_string += clean_lines.elided[end_lineno][:end_linepos] return full_string -def CheckScenarioHasTags(filename, clean_lines, linenum, error): +def CheckCatchTestHasTags(filename, clean_lines, linenum, error): line = clean_lines.elided[linenum] - scenario = Match(r'^SCENARIO\(', line) - if not scenario: return + test = Match(r'^(SCENARIO|TEST_CASE)\(', line) + if not test: return closing_line, closing_linenum, closing_pos = CloseExpression(clean_lines, linenum, line.find('(')) if closing_pos == -1: error(filename, linenum, - 'runtime/catch_scenario', 4, "Can't find closing bracket for scenario") + 'runtime/catch_test_tags', 4, "Can't find closing bracket for catch test") full_string = AssembleString( clean_lines, linenum, line.find('(') + 1, closing_linenum, closing_pos - 1) if(full_string.find(',') == -1): error(filename, linenum, - 'runtime/catch_scenario', 4, "Can't find `,\' seperating scenario name and the tags: " + str(clean_lines.lines[linenum])) - + 'runtime/catch_test_tags', 4, "Can't find `,\' seperating test name and the tags: " + str(clean_lines.lines[linenum])) def CheckRedundantVirtual(filename, clean_lines, linenum, error): """Check if line contains a redundant "virtual" function-specifier. @@ -6362,7 +6361,7 @@ def ProcessLine(filename, file_extension, clean_lines, line, CheckInvalidIncrement(filename, clean_lines, line, error) CheckMakePairUsesDeduction(filename, clean_lines, line, error) CheckRedundantVirtual(filename, clean_lines, line, error) - CheckScenarioHasTags(filename, clean_lines, line, error) + CheckCatchTestHasTags(filename, clean_lines, line, error) CheckNamespaceOrUsing(filename, clean_lines, line, error) CheckForEndl(filename, clean_lines, line, error) for check_fn in extra_check_functions: