1 // Copyright (c) 2006-2008 The Chromium 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 "base/strings/string_tokenizer.h"
7 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(StringTokenizerTest
, Simple
) {
16 string input
= "this is a test";
17 StringTokenizer
t(input
, " ");
19 EXPECT_TRUE(t
.GetNext());
20 EXPECT_EQ(string("this"), t
.token());
22 EXPECT_TRUE(t
.GetNext());
23 EXPECT_EQ(string("is"), t
.token());
25 EXPECT_TRUE(t
.GetNext());
26 EXPECT_EQ(string("a"), t
.token());
28 EXPECT_TRUE(t
.GetNext());
29 EXPECT_EQ(string("test"), t
.token());
31 EXPECT_FALSE(t
.GetNext());
34 TEST(StringTokenizerTest
, Reset
) {
35 string input
= "this is a test";
36 StringTokenizer
t(input
, " ");
38 for (int i
= 0; i
< 2; ++i
) {
39 EXPECT_TRUE(t
.GetNext());
40 EXPECT_EQ(string("this"), t
.token());
42 EXPECT_TRUE(t
.GetNext());
43 EXPECT_EQ(string("is"), t
.token());
45 EXPECT_TRUE(t
.GetNext());
46 EXPECT_EQ(string("a"), t
.token());
48 EXPECT_TRUE(t
.GetNext());
49 EXPECT_EQ(string("test"), t
.token());
51 EXPECT_FALSE(t
.GetNext());
56 TEST(StringTokenizerTest
, RetDelims
) {
57 string input
= "this is a test";
58 StringTokenizer
t(input
, " ");
59 t
.set_options(StringTokenizer::RETURN_DELIMS
);
61 EXPECT_TRUE(t
.GetNext());
62 EXPECT_EQ(string("this"), t
.token());
64 EXPECT_TRUE(t
.GetNext());
65 EXPECT_EQ(string(" "), t
.token());
67 EXPECT_TRUE(t
.GetNext());
68 EXPECT_EQ(string("is"), t
.token());
70 EXPECT_TRUE(t
.GetNext());
71 EXPECT_EQ(string(" "), t
.token());
73 EXPECT_TRUE(t
.GetNext());
74 EXPECT_EQ(string("a"), t
.token());
76 EXPECT_TRUE(t
.GetNext());
77 EXPECT_EQ(string(" "), t
.token());
79 EXPECT_TRUE(t
.GetNext());
80 EXPECT_EQ(string("test"), t
.token());
82 EXPECT_FALSE(t
.GetNext());
85 TEST(StringTokenizerTest
, ManyDelims
) {
86 string input
= "this: is, a-test";
87 StringTokenizer
t(input
, ": ,-");
89 EXPECT_TRUE(t
.GetNext());
90 EXPECT_EQ(string("this"), t
.token());
92 EXPECT_TRUE(t
.GetNext());
93 EXPECT_EQ(string("is"), t
.token());
95 EXPECT_TRUE(t
.GetNext());
96 EXPECT_EQ(string("a"), t
.token());
98 EXPECT_TRUE(t
.GetNext());
99 EXPECT_EQ(string("test"), t
.token());
101 EXPECT_FALSE(t
.GetNext());
104 TEST(StringTokenizerTest
, ParseHeader
) {
105 string input
= "Content-Type: text/html ; charset=UTF-8";
106 StringTokenizer
t(input
, ": ;=");
107 t
.set_options(StringTokenizer::RETURN_DELIMS
);
109 EXPECT_TRUE(t
.GetNext());
110 EXPECT_FALSE(t
.token_is_delim());
111 EXPECT_EQ(string("Content-Type"), t
.token());
113 EXPECT_TRUE(t
.GetNext());
114 EXPECT_TRUE(t
.token_is_delim());
115 EXPECT_EQ(string(":"), t
.token());
117 EXPECT_TRUE(t
.GetNext());
118 EXPECT_TRUE(t
.token_is_delim());
119 EXPECT_EQ(string(" "), t
.token());
121 EXPECT_TRUE(t
.GetNext());
122 EXPECT_FALSE(t
.token_is_delim());
123 EXPECT_EQ(string("text/html"), t
.token());
125 EXPECT_TRUE(t
.GetNext());
126 EXPECT_TRUE(t
.token_is_delim());
127 EXPECT_EQ(string(" "), t
.token());
129 EXPECT_TRUE(t
.GetNext());
130 EXPECT_TRUE(t
.token_is_delim());
131 EXPECT_EQ(string(";"), t
.token());
133 EXPECT_TRUE(t
.GetNext());
134 EXPECT_TRUE(t
.token_is_delim());
135 EXPECT_EQ(string(" "), t
.token());
137 EXPECT_TRUE(t
.GetNext());
138 EXPECT_FALSE(t
.token_is_delim());
139 EXPECT_EQ(string("charset"), t
.token());
141 EXPECT_TRUE(t
.GetNext());
142 EXPECT_TRUE(t
.token_is_delim());
143 EXPECT_EQ(string("="), t
.token());
145 EXPECT_TRUE(t
.GetNext());
146 EXPECT_FALSE(t
.token_is_delim());
147 EXPECT_EQ(string("UTF-8"), t
.token());
149 EXPECT_FALSE(t
.GetNext());
150 EXPECT_FALSE(t
.token_is_delim());
153 TEST(StringTokenizerTest
, ParseQuotedString
) {
154 string input
= "foo bar 'hello world' baz";
155 StringTokenizer
t(input
, " ");
156 t
.set_quote_chars("'");
158 EXPECT_TRUE(t
.GetNext());
159 EXPECT_EQ(string("foo"), t
.token());
161 EXPECT_TRUE(t
.GetNext());
162 EXPECT_EQ(string("bar"), t
.token());
164 EXPECT_TRUE(t
.GetNext());
165 EXPECT_EQ(string("'hello world'"), t
.token());
167 EXPECT_TRUE(t
.GetNext());
168 EXPECT_EQ(string("baz"), t
.token());
170 EXPECT_FALSE(t
.GetNext());
173 TEST(StringTokenizerTest
, ParseQuotedString_Malformed
) {
174 string input
= "bar 'hello wo";
175 StringTokenizer
t(input
, " ");
176 t
.set_quote_chars("'");
178 EXPECT_TRUE(t
.GetNext());
179 EXPECT_EQ(string("bar"), t
.token());
181 EXPECT_TRUE(t
.GetNext());
182 EXPECT_EQ(string("'hello wo"), t
.token());
184 EXPECT_FALSE(t
.GetNext());
187 TEST(StringTokenizerTest
, ParseQuotedString_Multiple
) {
188 string input
= "bar 'hel\"lo\" wo' baz\"";
189 StringTokenizer
t(input
, " ");
190 t
.set_quote_chars("'\"");
192 EXPECT_TRUE(t
.GetNext());
193 EXPECT_EQ(string("bar"), t
.token());
195 EXPECT_TRUE(t
.GetNext());
196 EXPECT_EQ(string("'hel\"lo\" wo'"), t
.token());
198 EXPECT_TRUE(t
.GetNext());
199 EXPECT_EQ(string("baz\""), t
.token());
201 EXPECT_FALSE(t
.GetNext());
204 TEST(StringTokenizerTest
, ParseQuotedString_EscapedQuotes
) {
205 string input
= "foo 'don\\'t do that'";
206 StringTokenizer
t(input
, " ");
207 t
.set_quote_chars("'");
209 EXPECT_TRUE(t
.GetNext());
210 EXPECT_EQ(string("foo"), t
.token());
212 EXPECT_TRUE(t
.GetNext());
213 EXPECT_EQ(string("'don\\'t do that'"), t
.token());
215 EXPECT_FALSE(t
.GetNext());
218 TEST(StringTokenizerTest
, ParseQuotedString_EscapedQuotes2
) {
219 string input
= "foo='a, b', bar";
220 StringTokenizer
t(input
, ", ");
221 t
.set_quote_chars("'");
223 EXPECT_TRUE(t
.GetNext());
224 EXPECT_EQ(string("foo='a, b'"), t
.token());
226 EXPECT_TRUE(t
.GetNext());
227 EXPECT_EQ(string("bar"), t
.token());
229 EXPECT_FALSE(t
.GetNext());