forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_partition_tree_test_utils.h
116 lines (99 loc) · 4.32 KB
/
token_partition_tree_test_utils.h
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
// 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.
#ifndef VERIBLE_COMMON_FORMATTING_TOKEN_PARTITION_TREE_TEST_UTILS_H_
#define VERIBLE_COMMON_FORMATTING_TOKEN_PARTITION_TREE_TEST_UTILS_H_
#include <utility>
#include <vector>
#include "common/formatting/format_token.h"
#include "common/formatting/token_partition_tree.h"
#include "common/formatting/unwrapped_line.h"
#include "gtest/gtest.h"
namespace verible {
// Helper class for creating TokenPartitionTree hierarchy using compact and easy
// to read/write/modify syntax. Its main advantage is token range deduction from
// child nodes and specification of token range using indexes instead of
// iterators.
//
// Example use:
//
// using TPT = TokenPartitionTreeBuilder;
// const auto tree =
// TPT(PartitionPolicyEnum::kAlwaysExpand,
// {
// TPT({0, 1}, PartitionPolicyEnum::kAlreadyFormatted),
// TPT(4, {1, 3}, PartitionPolicyEnum::kAlreadyFormatted),
// TPT(4, {3, 5}, PartitionPolicyEnum::kAlreadyFormatted),
// TPT(4, {5, 7}, PartitionPolicyEnum::kAlreadyFormatted),
// })
// .build(pre_format_tokens_);
//
class TokenPartitionTreeBuilder {
public:
TokenPartitionTreeBuilder(
int indent, std::pair<int, int> token_indexes_range,
PartitionPolicyEnum policy,
std::initializer_list<TokenPartitionTreeBuilder> children = {})
: indent_(indent),
token_indexes_range_(token_indexes_range),
policy_(policy),
children_(children) {}
TokenPartitionTreeBuilder(
int indent, PartitionPolicyEnum policy,
std::initializer_list<TokenPartitionTreeBuilder> children = {})
: indent_(indent), policy_(policy), children_(children) {}
TokenPartitionTreeBuilder(
int indent, std::pair<int, int> token_indexes_range,
std::initializer_list<TokenPartitionTreeBuilder> children = {})
: indent_(indent),
token_indexes_range_(token_indexes_range),
children_(children) {}
TokenPartitionTreeBuilder(
std::pair<int, int> token_indexes_range, PartitionPolicyEnum policy,
std::initializer_list<TokenPartitionTreeBuilder> children = {})
: token_indexes_range_(token_indexes_range),
policy_(policy),
children_(children) {}
explicit TokenPartitionTreeBuilder(
std::pair<int, int> token_indexes_range,
std::initializer_list<TokenPartitionTreeBuilder> children = {})
: token_indexes_range_(token_indexes_range), children_(children) {}
TokenPartitionTreeBuilder(
PartitionPolicyEnum policy,
std::initializer_list<TokenPartitionTreeBuilder> children)
: policy_(policy), children_(children) {}
explicit TokenPartitionTreeBuilder(
std::initializer_list<TokenPartitionTreeBuilder> children)
: children_(children) {}
// Builds TokenPartitionTree. Token indexes used during construction are
// replaced with iterators from 'tokens' vector.
TokenPartitionTree build(
const std::vector<verible::PreFormatToken>& tokens) const;
private:
int indent_ = 0;
std::pair<int, int> token_indexes_range_ = {-1, -1};
PartitionPolicyEnum policy_ = PartitionPolicyEnum::kUninitialized;
const std::vector<TokenPartitionTreeBuilder> children_;
};
// Tests whetherTokenPartitionTrees 'actual' and 'expected' are equal. The
// function compares the trees structure and values of all corresponding nodes.
// Intended for use with EXPECT_PRED_FORMAT2(), e.g.:
//
// EXPECT_PRED_FORMAT2(TokenPartitionTreesEqualPredFormat, actual_tree,
// expected_tree);
//
::testing::AssertionResult TokenPartitionTreesEqualPredFormat(
const char* actual_expr, const char* expected_expr,
const TokenPartitionTree& actual, const TokenPartitionTree& expected);
} // namespace verible
#endif // VERIBLE_COMMON_FORMATTING_TOKEN_PARTITION_TREE_TEST_UTILS_H_