1 // Copyright 2013 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.
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
11 #include "sdk_util/string_util.h"
13 using namespace sdk_util
;
14 using namespace testing
;
16 TEST(StringUtilTest
, SplitString
) {
17 std::vector
<std::string
> r
;
19 SplitString(std::string(), ',', &r
);
20 EXPECT_THAT(r
, ElementsAre());
23 SplitString("a,b,c", ',', &r
);
24 EXPECT_THAT(r
, ElementsAre("a", "b", "c"));
27 SplitString("a, b, c", ',', &r
);
28 EXPECT_THAT(r
, ElementsAre("a", " b", " c"));
31 SplitString("a,,c", ',', &r
);
32 EXPECT_THAT(r
, ElementsAre("a", "", "c"));
35 SplitString("foo", '*', &r
);
36 EXPECT_THAT(r
, ElementsAre("foo"));
39 SplitString("foo ,", ',', &r
);
40 EXPECT_THAT(r
, ElementsAre("foo ", ""));
43 SplitString(",", ',', &r
);
44 EXPECT_THAT(r
, ElementsAre("", ""));
47 SplitString("\t\ta\t", '\t', &r
);
48 EXPECT_THAT(r
, ElementsAre("", "", "a", ""));
52 TEST(StringUtilTest
, StringWithNoDelimiter
) {
53 std::vector
<std::string
> results
;
54 SplitString("alongwordwithnodelimiter", 'D', &results
);
55 EXPECT_THAT(results
, ElementsAre("alongwordwithnodelimiter"));
58 TEST(StringUtilTest
, LeadingDelimitersSkipped
) {
59 std::vector
<std::string
> results
;
60 SplitString("DDDoneDtwoDthree", 'D', &results
);
61 EXPECT_THAT(results
, ElementsAre("", "", "", "one", "two", "three"));
64 TEST(StringUtilTest
, ConsecutiveDelimitersSkipped
) {
65 std::vector
<std::string
> results
;
66 SplitString("unoDDDdosDtresDDcuatro", 'D', &results
);
67 EXPECT_THAT(results
, ElementsAre("uno", "", "", "dos", "tres", "", "cuatro"));
70 TEST(StringUtilTest
, TrailingDelimitersSkipped
) {
71 std::vector
<std::string
> results
;
72 SplitString("unDdeuxDtroisDquatreDDD", 'D', &results
);
74 ElementsAre("un", "deux", "trois", "quatre", "", "", ""));
77 // Note: Unlike base/strings/string_split, whitespace is not stripped.
78 // SplitString is expected to behave like SplitStringDontTrim in Chromium's
80 TEST(StringUtilTest
, StringSplitDontTrim
) {
81 std::vector
<std::string
> r
;
83 SplitString(" ", '*', &r
);
84 EXPECT_THAT(r
, ElementsAre(" "));
86 SplitString("\t \ta\t ", '\t', &r
);
87 EXPECT_THAT(r
, ElementsAre("", " ", "a", " "));
89 SplitString("\ta\t\nb\tcc", '\n', &r
);
90 EXPECT_THAT(r
, ElementsAre("\ta\t", "b\tcc"));