xref: /freebsd/contrib/googletest/googlemock/test/gmock-pp-string_test.cc (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 // Copyright 2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Google Mock - a framework for writing C++ mock classes.
31 //
32 // This file tests the internal preprocessor macro library.
33 #include <string>
34 
35 #include "gmock/gmock.h"
36 #include "gmock/internal/gmock-pp.h"
37 
38 namespace testing {
39 namespace {
40 
41 // Matcher to verify that to strings are identical up to whitespace
42 // Not 100% correct, because it treats "AB" as equal to "A B".
43 ::testing::Matcher<const std::string&> SameExceptSpaces(const std::string& s) {
44   auto remove_spaces = [](std::string to_split) {
45     to_split.erase(std::remove(to_split.begin(), to_split.end(), ' '),
46                    to_split.end());
47     return to_split;
48   };
49   return ::testing::ResultOf(remove_spaces, remove_spaces(s));
50 }
51 
52 // Verify that a macro expands to a given text. Ignores whitespace difference.
53 // In MSVC, GMOCK_PP_STRINGIZE() returns nothing, rather than "". So concatenate
54 // with an empty string.
55 #define EXPECT_EXPANSION(Result, Macro) \
56   EXPECT_THAT("" GMOCK_PP_STRINGIZE(Macro), SameExceptSpaces(Result))
57 
58 TEST(Macros, Cat) {
59   EXPECT_EXPANSION("14", GMOCK_PP_CAT(1, 4));
60   EXPECT_EXPANSION("+=", GMOCK_PP_CAT(+, =));
61 }
62 
63 TEST(Macros, Narg) {
64   EXPECT_EXPANSION("1", GMOCK_PP_NARG());
65   EXPECT_EXPANSION("1", GMOCK_PP_NARG(x));
66   EXPECT_EXPANSION("2", GMOCK_PP_NARG(x, y));
67   EXPECT_EXPANSION("3", GMOCK_PP_NARG(x, y, z));
68   EXPECT_EXPANSION("4", GMOCK_PP_NARG(x, y, z, w));
69 
70   EXPECT_EXPANSION("0", GMOCK_PP_NARG0());
71   EXPECT_EXPANSION("1", GMOCK_PP_NARG0(x));
72   EXPECT_EXPANSION("2", GMOCK_PP_NARG0(x, y));
73 }
74 
75 TEST(Macros, Comma) {
76   EXPECT_EXPANSION("0", GMOCK_PP_HAS_COMMA());
77   EXPECT_EXPANSION("1", GMOCK_PP_HAS_COMMA(, ));
78   EXPECT_EXPANSION("0", GMOCK_PP_HAS_COMMA((, )));
79 }
80 
81 TEST(Macros, IsEmpty) {
82   EXPECT_EXPANSION("1", GMOCK_PP_IS_EMPTY());
83   EXPECT_EXPANSION("0", GMOCK_PP_IS_EMPTY(, ));
84   EXPECT_EXPANSION("0", GMOCK_PP_IS_EMPTY(a));
85   EXPECT_EXPANSION("0", GMOCK_PP_IS_EMPTY(()));
86 
87 #define GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1
88   EXPECT_EXPANSION("1", GMOCK_PP_IS_EMPTY(GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1));
89 }
90 
91 TEST(Macros, If) {
92   EXPECT_EXPANSION("1", GMOCK_PP_IF(1, 1, 2));
93   EXPECT_EXPANSION("2", GMOCK_PP_IF(0, 1, 2));
94 }
95 
96 TEST(Macros, HeadTail) {
97   EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1));
98   EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1, 2));
99   EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1, 2, 3));
100 
101   EXPECT_EXPANSION("", GMOCK_PP_TAIL(1));
102   EXPECT_EXPANSION("2", GMOCK_PP_TAIL(1, 2));
103   EXPECT_EXPANSION("2", GMOCK_PP_HEAD(GMOCK_PP_TAIL(1, 2, 3)));
104 }
105 
106 TEST(Macros, Parentheses) {
107   EXPECT_EXPANSION("0", GMOCK_PP_IS_BEGIN_PARENS(sss));
108   EXPECT_EXPANSION("0", GMOCK_PP_IS_BEGIN_PARENS(sss()));
109   EXPECT_EXPANSION("0", GMOCK_PP_IS_BEGIN_PARENS(sss() sss));
110   EXPECT_EXPANSION("1", GMOCK_PP_IS_BEGIN_PARENS((sss)));
111   EXPECT_EXPANSION("1", GMOCK_PP_IS_BEGIN_PARENS((sss)ss));
112 
113   EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS(sss));
114   EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS(sss()));
115   EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS(sss() sss));
116   EXPECT_EXPANSION("1", GMOCK_PP_IS_ENCLOSED_PARENS((sss)));
117   EXPECT_EXPANSION("0", GMOCK_PP_IS_ENCLOSED_PARENS((sss)ss));
118 
119   EXPECT_EXPANSION("1 + 1", GMOCK_PP_REMOVE_PARENS((1 + 1)));
120 }
121 
122 TEST(Macros, Increment) {
123   EXPECT_EXPANSION("1", GMOCK_PP_INC(0));
124   EXPECT_EXPANSION("2", GMOCK_PP_INC(1));
125   EXPECT_EXPANSION("3", GMOCK_PP_INC(2));
126   EXPECT_EXPANSION("4", GMOCK_PP_INC(3));
127   EXPECT_EXPANSION("5", GMOCK_PP_INC(4));
128 
129   EXPECT_EXPANSION("16", GMOCK_PP_INC(15));
130 }
131 
132 #define JOINER_CAT(a, b) a##b
133 #define JOINER(_N, _Data, _Elem) JOINER_CAT(_Data, _N) = _Elem
134 
135 TEST(Macros, Repeat) {
136   EXPECT_EXPANSION("", GMOCK_PP_REPEAT(JOINER, X, 0));
137   EXPECT_EXPANSION("X0=", GMOCK_PP_REPEAT(JOINER, X, 1));
138   EXPECT_EXPANSION("X0= X1=", GMOCK_PP_REPEAT(JOINER, X, 2));
139   EXPECT_EXPANSION("X0= X1= X2=", GMOCK_PP_REPEAT(JOINER, X, 3));
140   EXPECT_EXPANSION("X0= X1= X2= X3=", GMOCK_PP_REPEAT(JOINER, X, 4));
141   EXPECT_EXPANSION("X0= X1= X2= X3= X4=", GMOCK_PP_REPEAT(JOINER, X, 5));
142   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5=", GMOCK_PP_REPEAT(JOINER, X, 6));
143   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6=",
144                    GMOCK_PP_REPEAT(JOINER, X, 7));
145   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7=",
146                    GMOCK_PP_REPEAT(JOINER, X, 8));
147   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8=",
148                    GMOCK_PP_REPEAT(JOINER, X, 9));
149   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9=",
150                    GMOCK_PP_REPEAT(JOINER, X, 10));
151   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10=",
152                    GMOCK_PP_REPEAT(JOINER, X, 11));
153   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11=",
154                    GMOCK_PP_REPEAT(JOINER, X, 12));
155   EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12=",
156                    GMOCK_PP_REPEAT(JOINER, X, 13));
157   EXPECT_EXPANSION(
158       "X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12= X13=",
159       GMOCK_PP_REPEAT(JOINER, X, 14));
160   EXPECT_EXPANSION(
161       "X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12= X13= X14=",
162       GMOCK_PP_REPEAT(JOINER, X, 15));
163 }
164 TEST(Macros, ForEach) {
165   EXPECT_EXPANSION("", GMOCK_PP_FOR_EACH(JOINER, X, ()));
166   EXPECT_EXPANSION("X0=a", GMOCK_PP_FOR_EACH(JOINER, X, (a)));
167   EXPECT_EXPANSION("X0=a X1=b", GMOCK_PP_FOR_EACH(JOINER, X, (a, b)));
168   EXPECT_EXPANSION("X0=a X1=b X2=c", GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c)));
169   EXPECT_EXPANSION("X0=a X1=b X2=c X3=d",
170                    GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d)));
171   EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e",
172                    GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e)));
173   EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f",
174                    GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f)));
175   EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g",
176                    GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g)));
177   EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h",
178                    GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h)));
179   EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i",
180                    GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i)));
181   EXPECT_EXPANSION(
182       "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j",
183       GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j)));
184   EXPECT_EXPANSION(
185       "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k",
186       GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k)));
187   EXPECT_EXPANSION(
188       "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l",
189       GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l)));
190   EXPECT_EXPANSION(
191       "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l X12=m",
192       GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l, m)));
193   EXPECT_EXPANSION(
194       "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l X12=m "
195       "X13=n",
196       GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l, m, n)));
197   EXPECT_EXPANSION(
198       "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l X12=m "
199       "X13=n X14=o",
200       GMOCK_PP_FOR_EACH(JOINER, X,
201                         (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)));
202 }
203 
204 }  // namespace
205 }  // namespace testing
206