Flutter Engine
 
Loading...
Searching...
No Matches
comments_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
5#include "gtest/gtest.h"
6
7#include <sstream>
8
9TEST(CommentsTest, Simple) {
10 std::string test = R"test(
11// Hello
12)test";
13
14 std::vector<std::string> comments;
15 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
16 comments.push_back(std::string(comment));
17 });
18
19 ASSERT_EQ(comments.size(), 1u);
20 EXPECT_EQ(comments[0], "Hello");
21}
22
23TEST(CommentsTest, Nothing) {
24 std::string test = R"test(
25hello world
26)test";
27
28 std::vector<std::string> comments;
29 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
30 comments.push_back(std::string(comment));
31 });
32
33 ASSERT_EQ(comments.size(), 0u);
34}
35
36TEST(CommentsTest, Multiline) {
37 std::string test = R"test(
38/*
39hello world
40*/
41dfdd
42)test";
43
44 std::vector<std::string> comments;
45 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
46 comments.push_back(std::string(comment));
47 });
48
49 ASSERT_EQ(comments.size(), 1u);
50 EXPECT_EQ(comments[0], "hello world\n");
51}
52
53TEST(CommentsTest, MultilineCpp) {
54 std::string test = R"test(
55doo
56// hello
57// world
58daa
59)test";
60
61 std::vector<std::string> comments;
62 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
63 comments.push_back(std::string(comment));
64 });
65
66 ASSERT_EQ(comments.size(), 1u);
67 EXPECT_EQ(comments[0], "hello\nworld");
68}
69
70TEST(CommentsTest, CWithLeadingStars) {
71 std::string test = R"test(
72/*************
73 * hello
74 * world
75 */
76)test";
77
78 std::vector<std::string> comments;
79 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
80 comments.push_back(std::string(comment));
81 });
82
83 ASSERT_EQ(comments.size(), 1u);
84 EXPECT_EQ(comments[0], "hello\nworld\n");
85}
86
87TEST(CommentsTest, CWithTrailingStars) {
88 std::string test = R"test(
89/*************
90 * hello *
91 * world *
92 */
93)test";
94
95 std::vector<std::string> comments;
96 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
97 comments.push_back(std::string(comment));
98 });
99
100 ASSERT_EQ(comments.size(), 1u);
101 EXPECT_EQ(comments[0], "hello\nworld\n");
102}
103
104TEST(CommentsTest, CTextOnEndingLine) {
105 std::string test = R"test(
106/*hello
107world*/
108)test";
109
110 std::vector<std::string> comments;
111 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
112 comments.push_back(std::string(comment));
113 });
114
115 ASSERT_EQ(comments.size(), 1u);
116 EXPECT_EQ(comments[0], "hello\nworld");
117}
118
119TEST(CommentsTest, HashComments) {
120 std::string test = R"test(
121# Hello
122# World
123)test";
124
125 std::vector<std::string> comments;
126 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
127 comments.push_back(std::string(comment));
128 });
129
130 ASSERT_EQ(comments.size(), 1u);
131 EXPECT_EQ(comments[0], "Hello\nWorld");
132}
133
134TEST(CommentsTest, JoinCComments) {
135 std::string test = R"test(
136/*Hello*/
137/*World*/
138)test";
139
140 std::vector<std::string> comments;
141 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
142 comments.push_back(std::string(comment));
143 });
144
145 ASSERT_EQ(comments.size(), 1u);
146 EXPECT_EQ(comments[0], "Hello\nWorld");
147}
148
149TEST(CommentsTest, JoinCCommentsNegative) {
150 std::string test = R"test(
151/*Hello*/
152
153/*World*/
154)test";
155
156 std::vector<std::string> comments;
157 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
158 comments.push_back(std::string(comment));
159 });
160
161 ASSERT_EQ(comments.size(), 2u);
162 EXPECT_EQ(comments[0], "Hello");
163 EXPECT_EQ(comments[1], "World");
164}
165
166TEST(CommentsTest, MixmatchComments) {
167 std::string test = R"test(
168// Hello
169/*
170World
171*/
172)test";
173
174 std::vector<std::string> comments;
175 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
176 comments.push_back(std::string(comment));
177 });
178
179 ASSERT_EQ(comments.size(), 1u);
180 EXPECT_EQ(comments[0], "Hello\nWorld\n");
181}
182
183TEST(CommentsTest, MixmatchCommentsWithWhitespace) {
184 std::string test = R"test(
185// Hello
186
187/*
188World
189*/
190)test";
191
192 std::vector<std::string> comments;
193 IterateComments(test.c_str(), test.size(), [&](std::string_view comment) {
194 comments.push_back(std::string(comment));
195 });
196
197 ASSERT_EQ(comments.size(), 2u);
198 EXPECT_EQ(comments[0], "Hello");
199 EXPECT_EQ(comments[1], "World\n");
200}
void IterateComments(const char *buffer, size_t size, std::function< void(std::string_view)> callback)
TEST(CommentsTest, Simple)