1 // Copyright (c) 2014 The Weave 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.
4 
5 #include "src/string_utils.h"
6 
7 #include <list>
8 #include <set>
9 #include <string>
10 #include <vector>
11 
12 #include <gtest/gtest.h>
13 
14 namespace weave {
15 
TEST(StringUtils,Split)16 TEST(StringUtils, Split) {
17   std::vector<std::string> parts;
18 
19   parts = Split("", ",", false, false);
20   EXPECT_EQ(1u, parts.size());
21   EXPECT_EQ("", parts[0]);
22 
23   parts = Split("", ",", false, true);
24   EXPECT_EQ(0u, parts.size());
25 
26   parts = Split("abc", ",", false, false);
27   EXPECT_EQ(1u, parts.size());
28   EXPECT_EQ("abc", parts[0]);
29 
30   parts = Split(",a,bc , d,  ,e, ", ",", true, true);
31   EXPECT_EQ(4u, parts.size());
32   EXPECT_EQ("a", parts[0]);
33   EXPECT_EQ("bc", parts[1]);
34   EXPECT_EQ("d", parts[2]);
35   EXPECT_EQ("e", parts[3]);
36 
37   parts = Split(",a,bc , d,  ,e, ", ",", false, true);
38   EXPECT_EQ(6u, parts.size());
39   EXPECT_EQ("a", parts[0]);
40   EXPECT_EQ("bc ", parts[1]);
41   EXPECT_EQ(" d", parts[2]);
42   EXPECT_EQ("  ", parts[3]);
43   EXPECT_EQ("e", parts[4]);
44   EXPECT_EQ(" ", parts[5]);
45 
46   parts = Split(",a,bc , d,  ,e, ", ",", true, false);
47   EXPECT_EQ(7u, parts.size());
48   EXPECT_EQ("", parts[0]);
49   EXPECT_EQ("a", parts[1]);
50   EXPECT_EQ("bc", parts[2]);
51   EXPECT_EQ("d", parts[3]);
52   EXPECT_EQ("", parts[4]);
53   EXPECT_EQ("e", parts[5]);
54   EXPECT_EQ("", parts[6]);
55 
56   parts = Split(",a,bc , d,  ,e, ", ",", false, false);
57   EXPECT_EQ(7u, parts.size());
58   EXPECT_EQ("", parts[0]);
59   EXPECT_EQ("a", parts[1]);
60   EXPECT_EQ("bc ", parts[2]);
61   EXPECT_EQ(" d", parts[3]);
62   EXPECT_EQ("  ", parts[4]);
63   EXPECT_EQ("e", parts[5]);
64   EXPECT_EQ(" ", parts[6]);
65 
66   parts = Split("abc:=xyz", ":=", false, false);
67   EXPECT_EQ(2u, parts.size());
68   EXPECT_EQ("abc", parts[0]);
69   EXPECT_EQ("xyz", parts[1]);
70 
71   parts = Split("abc", "", false, false);
72   EXPECT_EQ(3u, parts.size());
73   EXPECT_EQ("a", parts[0]);
74   EXPECT_EQ("b", parts[1]);
75   EXPECT_EQ("c", parts[2]);
76 }
77 
TEST(StringUtils,SplitAtFirst)78 TEST(StringUtils, SplitAtFirst) {
79   std::pair<std::string, std::string> pair;
80 
81   pair = SplitAtFirst(" 123 : 4 : 56 : 789 ", ":", true);
82   EXPECT_EQ("123", pair.first);
83   EXPECT_EQ("4 : 56 : 789", pair.second);
84 
85   pair = SplitAtFirst(" 123 : 4 : 56 : 789 ", ":", false);
86   EXPECT_EQ(" 123 ", pair.first);
87   EXPECT_EQ(" 4 : 56 : 789 ", pair.second);
88 
89   pair = SplitAtFirst("", "=", true);
90   EXPECT_EQ("", pair.first);
91   EXPECT_EQ("", pair.second);
92 
93   pair = SplitAtFirst("=", "=", true);
94   EXPECT_EQ("", pair.first);
95   EXPECT_EQ("", pair.second);
96 
97   pair = SplitAtFirst("a=", "=", true);
98   EXPECT_EQ("a", pair.first);
99   EXPECT_EQ("", pair.second);
100 
101   pair = SplitAtFirst("abc=", "=", true);
102   EXPECT_EQ("abc", pair.first);
103   EXPECT_EQ("", pair.second);
104 
105   pair = SplitAtFirst("=a", "=", true);
106   EXPECT_EQ("", pair.first);
107   EXPECT_EQ("a", pair.second);
108 
109   pair = SplitAtFirst("=abc=", "=", true);
110   EXPECT_EQ("", pair.first);
111   EXPECT_EQ("abc=", pair.second);
112 
113   pair = SplitAtFirst("abc", "=", true);
114   EXPECT_EQ("abc", pair.first);
115   EXPECT_EQ("", pair.second);
116 
117   pair = SplitAtFirst("abc:=xyz", ":=", true);
118   EXPECT_EQ("abc", pair.first);
119   EXPECT_EQ("xyz", pair.second);
120 
121   pair = SplitAtFirst("abc", "", true);
122   EXPECT_EQ("", pair.first);
123   EXPECT_EQ("abc", pair.second);
124 }
125 
TEST(StringUtils,Join_String)126 TEST(StringUtils, Join_String) {
127   EXPECT_EQ("", Join(",", std::vector<std::string>{}));
128   EXPECT_EQ("abc", Join(",", std::vector<std::string>{"abc"}));
129   EXPECT_EQ("abc,,xyz", Join(",", std::vector<std::string>{"abc", "", "xyz"}));
130   EXPECT_EQ("abc,defg", Join(",", std::vector<std::string>{"abc", "defg"}));
131   EXPECT_EQ("1 : 2 : 3", Join(" : ", std::vector<std::string>{"1", "2", "3"}));
132   EXPECT_EQ("1:2", Join(":", std::set<std::string>{"1", "2"}));
133   EXPECT_EQ("1:2", Join(":", std::vector<std::string>{"1", "2"}));
134   EXPECT_EQ("1:2", Join(":", std::list<std::string>{"1", "2"}));
135   EXPECT_EQ("123", Join("", std::vector<std::string>{"1", "2", "3"}));
136 }
137 
TEST(StringUtils,Join_Pair)138 TEST(StringUtils, Join_Pair) {
139   EXPECT_EQ("ab,cd", Join(",", "ab", "cd"));
140   EXPECT_EQ("key = value", Join(" = ", "key", "value"));
141 }
142 
143 }  // namespace weave
144