forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_info_json_test.cc
88 lines (73 loc) · 2.36 KB
/
token_info_json_test.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
// 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/text/token_info_json.h"
#include <ostream>
#include "absl/strings/string_view.h"
#include "common/text/constants.h"
#include "common/text/token_info.h"
#include "gtest/gtest.h"
#include "nlohmann/json.hpp"
namespace verible {
namespace {
TEST(TokenInfoToJsonTest, ToJsonEOF) {
constexpr absl::string_view base; // empty
const TokenInfo::Context context(base);
const TokenInfo token_info(TK_EOF, base);
EXPECT_EQ(ToJson(token_info, context), nlohmann::json::parse(R"({
"start": 0,
"end": 0,
"tag": "0"
})"));
EXPECT_EQ(ToJson(token_info, context, true), nlohmann::json::parse(R"({
"start": 0,
"end": 0,
"tag": "0",
"text": ""
})"));
}
TEST(TokenInfoToJsonTest, ToJsonWithBase) {
constexpr absl::string_view base("basement cat");
const TokenInfo::Context context(base);
const TokenInfo token_info(7, base.substr(9, 3));
EXPECT_EQ(ToJson(token_info, context), nlohmann::json::parse(R"({
"start": 9,
"end": 12,
"tag": "7"
})"));
EXPECT_EQ(ToJson(token_info, context, true), nlohmann::json::parse(R"({
"start": 9,
"end": 12,
"tag": "7",
"text": "cat"
})"));
}
TEST(TokenInfoToJsonTest, ToJsonWithTokenEnumTranslator) {
constexpr absl::string_view text("string of length 19");
const TokenInfo token_info(143, text);
const verible::TokenInfo::Context context(
text, [](std::ostream &stream, int e) { stream << "token enum " << e; });
EXPECT_EQ(ToJson(token_info, context), nlohmann::json::parse(R"({
"start": 0,
"end": 19,
"tag": "token enum 143"
})"));
EXPECT_EQ(ToJson(token_info, context, true), nlohmann::json::parse(R"({
"start": 0,
"end": 19,
"tag": "token enum 143",
"text": "string of length 19"
})"));
}
} // namespace
} // namespace verible