"Make Chrome your default browser" should now appear as a checkbox at the bottom...
[chromium-blink-merge.git] / base / string_tokenizer_unittest.cc
blobdace5eea9d0f9b4cae071cff2df32067199cb43f
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/string_tokenizer.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 using std::string;
10 namespace {
11 class StringTokenizerTest : public testing::Test {};
14 TEST(StringTokenizerTest, Simple) {
15 string input = "this is a test";
16 StringTokenizer t(input, " ");
18 EXPECT_TRUE(t.GetNext());
19 EXPECT_EQ(string("this"), t.token());
21 EXPECT_TRUE(t.GetNext());
22 EXPECT_EQ(string("is"), t.token());
24 EXPECT_TRUE(t.GetNext());
25 EXPECT_EQ(string("a"), t.token());
27 EXPECT_TRUE(t.GetNext());
28 EXPECT_EQ(string("test"), t.token());
30 EXPECT_FALSE(t.GetNext());
33 TEST(StringTokenizerTest, RetDelims) {
34 string input = "this is a test";
35 StringTokenizer t(input, " ");
36 t.set_options(StringTokenizer::RETURN_DELIMS);
38 EXPECT_TRUE(t.GetNext());
39 EXPECT_EQ(string("this"), t.token());
41 EXPECT_TRUE(t.GetNext());
42 EXPECT_EQ(string(" "), t.token());
44 EXPECT_TRUE(t.GetNext());
45 EXPECT_EQ(string("is"), t.token());
47 EXPECT_TRUE(t.GetNext());
48 EXPECT_EQ(string(" "), t.token());
50 EXPECT_TRUE(t.GetNext());
51 EXPECT_EQ(string("a"), t.token());
53 EXPECT_TRUE(t.GetNext());
54 EXPECT_EQ(string(" "), t.token());
56 EXPECT_TRUE(t.GetNext());
57 EXPECT_EQ(string("test"), t.token());
59 EXPECT_FALSE(t.GetNext());
62 TEST(StringTokenizerTest, ManyDelims) {
63 string input = "this: is, a-test";
64 StringTokenizer t(input, ": ,-");
66 EXPECT_TRUE(t.GetNext());
67 EXPECT_EQ(string("this"), t.token());
69 EXPECT_TRUE(t.GetNext());
70 EXPECT_EQ(string("is"), t.token());
72 EXPECT_TRUE(t.GetNext());
73 EXPECT_EQ(string("a"), t.token());
75 EXPECT_TRUE(t.GetNext());
76 EXPECT_EQ(string("test"), t.token());
78 EXPECT_FALSE(t.GetNext());
81 TEST(StringTokenizerTest, ParseHeader) {
82 string input = "Content-Type: text/html ; charset=UTF-8";
83 StringTokenizer t(input, ": ;=");
84 t.set_options(StringTokenizer::RETURN_DELIMS);
86 EXPECT_TRUE(t.GetNext());
87 EXPECT_FALSE(t.token_is_delim());
88 EXPECT_EQ(string("Content-Type"), t.token());
90 EXPECT_TRUE(t.GetNext());
91 EXPECT_TRUE(t.token_is_delim());
92 EXPECT_EQ(string(":"), t.token());
94 EXPECT_TRUE(t.GetNext());
95 EXPECT_TRUE(t.token_is_delim());
96 EXPECT_EQ(string(" "), t.token());
98 EXPECT_TRUE(t.GetNext());
99 EXPECT_FALSE(t.token_is_delim());
100 EXPECT_EQ(string("text/html"), t.token());
102 EXPECT_TRUE(t.GetNext());
103 EXPECT_TRUE(t.token_is_delim());
104 EXPECT_EQ(string(" "), t.token());
106 EXPECT_TRUE(t.GetNext());
107 EXPECT_TRUE(t.token_is_delim());
108 EXPECT_EQ(string(";"), t.token());
110 EXPECT_TRUE(t.GetNext());
111 EXPECT_TRUE(t.token_is_delim());
112 EXPECT_EQ(string(" "), t.token());
114 EXPECT_TRUE(t.GetNext());
115 EXPECT_FALSE(t.token_is_delim());
116 EXPECT_EQ(string("charset"), t.token());
118 EXPECT_TRUE(t.GetNext());
119 EXPECT_TRUE(t.token_is_delim());
120 EXPECT_EQ(string("="), t.token());
122 EXPECT_TRUE(t.GetNext());
123 EXPECT_FALSE(t.token_is_delim());
124 EXPECT_EQ(string("UTF-8"), t.token());
126 EXPECT_FALSE(t.GetNext());
127 EXPECT_FALSE(t.token_is_delim());
130 TEST(StringTokenizerTest, ParseQuotedString) {
131 string input = "foo bar 'hello world' baz";
132 StringTokenizer t(input, " ");
133 t.set_quote_chars("'");
135 EXPECT_TRUE(t.GetNext());
136 EXPECT_EQ(string("foo"), t.token());
138 EXPECT_TRUE(t.GetNext());
139 EXPECT_EQ(string("bar"), t.token());
141 EXPECT_TRUE(t.GetNext());
142 EXPECT_EQ(string("'hello world'"), t.token());
144 EXPECT_TRUE(t.GetNext());
145 EXPECT_EQ(string("baz"), t.token());
147 EXPECT_FALSE(t.GetNext());
150 TEST(StringTokenizerTest, ParseQuotedString_Malformed) {
151 string input = "bar 'hello wo";
152 StringTokenizer t(input, " ");
153 t.set_quote_chars("'");
155 EXPECT_TRUE(t.GetNext());
156 EXPECT_EQ(string("bar"), t.token());
158 EXPECT_TRUE(t.GetNext());
159 EXPECT_EQ(string("'hello wo"), t.token());
161 EXPECT_FALSE(t.GetNext());
164 TEST(StringTokenizerTest, ParseQuotedString_Multiple) {
165 string input = "bar 'hel\"lo\" wo' baz\"";
166 StringTokenizer t(input, " ");
167 t.set_quote_chars("'\"");
169 EXPECT_TRUE(t.GetNext());
170 EXPECT_EQ(string("bar"), t.token());
172 EXPECT_TRUE(t.GetNext());
173 EXPECT_EQ(string("'hel\"lo\" wo'"), t.token());
175 EXPECT_TRUE(t.GetNext());
176 EXPECT_EQ(string("baz\""), t.token());
178 EXPECT_FALSE(t.GetNext());
181 TEST(StringTokenizerTest, ParseQuotedString_EscapedQuotes) {
182 string input = "foo 'don\\'t do that'";
183 StringTokenizer t(input, " ");
184 t.set_quote_chars("'");
186 EXPECT_TRUE(t.GetNext());
187 EXPECT_EQ(string("foo"), t.token());
189 EXPECT_TRUE(t.GetNext());
190 EXPECT_EQ(string("'don\\'t do that'"), t.token());
192 EXPECT_FALSE(t.GetNext());
195 TEST(StringTokenizerTest, ParseQuotedString_EscapedQuotes2) {
196 string input = "foo='a, b', bar";
197 StringTokenizer t(input, ", ");
198 t.set_quote_chars("'");
200 EXPECT_TRUE(t.GetNext());
201 EXPECT_EQ(string("foo='a, b'"), t.token());
203 EXPECT_TRUE(t.GetNext());
204 EXPECT_EQ(string("bar"), t.token());
206 EXPECT_FALSE(t.GetNext());