forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinter_test_utils.cc
95 lines (84 loc) · 3.45 KB
/
linter_test_utils.cc
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
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/analysis/linter_test_utils.h"
#include <iostream>
#include <iterator>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/analysis/lint_rule_status.h"
#include "common/text/token_info.h"
#include "common/util/algorithm.h"
namespace verible {
// This function helps find symmetric differences between two sets
// of violations (actual vs. expected).
// By comparing only the left-bound of the corresponding text ranges,
// this assumes that no two violations start at the same location.
// This assumption could be removed later, if needed.
static int CompareViolation(const LintViolation& lhs, const TokenInfo& rhs) {
{
const int delta =
std::distance(rhs.text().begin(), lhs.token.text().begin());
if (delta != 0) {
return delta;
}
}
{
const int delta = std::distance(rhs.text().end(), lhs.token.text().end());
if (delta != 0) {
return delta;
}
}
// Then compare enums, where we only care about equality.
return rhs.token_enum() - lhs.token.token_enum();
}
// TODO(b/151371397): refactor this for re-use for multi-findings style tests.
bool LintTestCase::ExactMatchFindings(
const std::set<LintViolation>& found_violations, absl::string_view base,
std::ostream* diffstream) const {
// Due to the order in which violations are visited, we can assert that
// the reported violations are thus ordered.
// TODO(fangism): It wouldn't be too expensive to verify this assertion.
// The expected violations are also ordered by construction.
const auto expected_violations = FindImportantTokens(base);
// Thus, we can use an algorithm like std::set_symmetric_difference().
std::vector<LintViolation> unmatched_found_violations;
std::vector<TokenInfo> unmatched_expected_violations;
set_symmetric_difference_split(
found_violations.begin(), found_violations.end(),
expected_violations.begin(), expected_violations.end(),
std::back_inserter(unmatched_found_violations),
std::back_inserter(unmatched_expected_violations), &CompareViolation);
bool all_match = true;
// TODO(fangism): plumb through a language-specific token enum translator,
// through a TokenInfo::Context object.
const TokenInfo::Context context(base);
if (!unmatched_found_violations.empty()) {
all_match = false;
*diffstream
<< "The following found violations did not match the expected ones:\n";
for (const auto& violation : unmatched_found_violations) {
violation.token.ToStream(*diffstream, context) << std::endl;
}
}
if (!unmatched_expected_violations.empty()) {
all_match = false;
*diffstream
<< "The following expected violations did not match the ones found:\n";
for (const auto& violation : unmatched_expected_violations) {
violation.ToStream(*diffstream, context) << std::endl;
}
}
return all_match;
}
} // namespace verible