Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / tests / sdk_util_test / string_util_test.cc
blob6329086fde3201aa960f27a298b095271eee8d61
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.
5 #include <string>
6 #include <vector>
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());
21 r.clear();
23 SplitString("a,b,c", ',', &r);
24 EXPECT_THAT(r, ElementsAre("a", "b", "c"));
25 r.clear();
27 SplitString("a, b, c", ',', &r);
28 EXPECT_THAT(r, ElementsAre("a", " b", " c"));
29 r.clear();
31 SplitString("a,,c", ',', &r);
32 EXPECT_THAT(r, ElementsAre("a", "", "c"));
33 r.clear();
35 SplitString("foo", '*', &r);
36 EXPECT_THAT(r, ElementsAre("foo"));
37 r.clear();
39 SplitString("foo ,", ',', &r);
40 EXPECT_THAT(r, ElementsAre("foo ", ""));
41 r.clear();
43 SplitString(",", ',', &r);
44 EXPECT_THAT(r, ElementsAre("", ""));
45 r.clear();
47 SplitString("\t\ta\t", '\t', &r);
48 EXPECT_THAT(r, ElementsAre("", "", "a", ""));
49 r.clear();
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);
73 EXPECT_THAT(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
79 // 'base' module.
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"));