Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gmock-pp-string_test.cc
Go to the documentation of this file.
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.
34 
35 #include <string>
36 
37 #include "gmock/gmock.h"
38 
39 namespace testing {
40 namespace {
41 
42 // Matcher to verify that to strings are identical up to whitespace
43 // Not 100% correct, because it treats "AB" as equal to "A B".
44 ::testing::Matcher<const std::string&> SameExceptSpaces(const std::string& s) {
45  auto remove_spaces = [](std::string to_split) {
46  to_split.erase(std::remove(to_split.begin(), to_split.end(), ' '),
47  to_split.end());
48  return to_split;
49  };
50  return ::testing::ResultOf(remove_spaces, remove_spaces(s));
51 }
52 
53 // Verify that a macro expands to a given text. Ignores whitespace difference.
54 // In MSVC, GMOCK_PP_STRINGIZE() returns nothing, rather than "". So concatenate
55 // with an empty string.
56 #define EXPECT_EXPANSION(Result, Macro) \
57  EXPECT_THAT("" GMOCK_PP_STRINGIZE(Macro), SameExceptSpaces(Result))
58 
59 TEST(Macros, Cat) {
60  EXPECT_EXPANSION("14", GMOCK_PP_CAT(1, 4));
61  EXPECT_EXPANSION("+=", GMOCK_PP_CAT(+, =));
62 }
63 
64 TEST(Macros, Narg) {
69  EXPECT_EXPANSION("4", GMOCK_PP_NARG(x, y, z, w));
70 
74 }
75 
76 TEST(Macros, Comma) {
80 }
81 
82 TEST(Macros, IsEmpty) {
87 
88 #define GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1
90 }
91 
92 TEST(Macros, If) {
93  EXPECT_EXPANSION("1", GMOCK_PP_IF(1, 1, 2));
94  EXPECT_EXPANSION("2", GMOCK_PP_IF(0, 1, 2));
95 }
96 
97 TEST(Macros, HeadTail) {
99  EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1, 2));
100  EXPECT_EXPANSION("1", GMOCK_PP_HEAD(1, 2, 3));
101 
103  EXPECT_EXPANSION("2", GMOCK_PP_TAIL(1, 2));
105 }
106 
107 TEST(Macros, Parentheses) {
113 
119 
120  EXPECT_EXPANSION("1 + 1", GMOCK_PP_REMOVE_PARENS((1 + 1)));
121 }
122 
123 TEST(Macros, Increment) {
129 
130  EXPECT_EXPANSION("16", GMOCK_PP_INC(15));
131 }
132 
133 #define JOINER_CAT(a, b) a##b
134 #define JOINER(_N, _Data, _Elem) JOINER_CAT(_Data, _N) = _Elem
135 
136 TEST(Macros, Repeat) {
138  EXPECT_EXPANSION("X0=", GMOCK_PP_REPEAT(JOINER, X, 1));
139  EXPECT_EXPANSION("X0= X1=", GMOCK_PP_REPEAT(JOINER, X, 2));
140  EXPECT_EXPANSION("X0= X1= X2=", GMOCK_PP_REPEAT(JOINER, X, 3));
141  EXPECT_EXPANSION("X0= X1= X2= X3=", GMOCK_PP_REPEAT(JOINER, X, 4));
142  EXPECT_EXPANSION("X0= X1= X2= X3= X4=", GMOCK_PP_REPEAT(JOINER, X, 5));
143  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5=", GMOCK_PP_REPEAT(JOINER, X, 6));
144  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6=",
145  GMOCK_PP_REPEAT(JOINER, X, 7));
146  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7=",
147  GMOCK_PP_REPEAT(JOINER, X, 8));
148  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8=",
149  GMOCK_PP_REPEAT(JOINER, X, 9));
150  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9=",
151  GMOCK_PP_REPEAT(JOINER, X, 10));
152  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10=",
153  GMOCK_PP_REPEAT(JOINER, X, 11));
154  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11=",
155  GMOCK_PP_REPEAT(JOINER, X, 12));
156  EXPECT_EXPANSION("X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12=",
157  GMOCK_PP_REPEAT(JOINER, X, 13));
159  "X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12= X13=",
160  GMOCK_PP_REPEAT(JOINER, X, 14));
162  "X0= X1= X2= X3= X4= X5= X6= X7= X8= X9= X10= X11= X12= X13= X14=",
163  GMOCK_PP_REPEAT(JOINER, X, 15));
164 }
165 TEST(Macros, ForEach) {
167  EXPECT_EXPANSION("X0=a", GMOCK_PP_FOR_EACH(JOINER, X, (a)));
168  EXPECT_EXPANSION("X0=a X1=b", GMOCK_PP_FOR_EACH(JOINER, X, (a, b)));
169  EXPECT_EXPANSION("X0=a X1=b X2=c", GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c)));
170  EXPECT_EXPANSION("X0=a X1=b X2=c X3=d",
171  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d)));
172  EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e",
173  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e)));
174  EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f",
175  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f)));
176  EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g",
177  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g)));
178  EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h",
179  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h)));
180  EXPECT_EXPANSION("X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i",
181  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i)));
183  "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j",
184  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j)));
186  "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k",
187  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k)));
189  "X0=a X1=b X2=c X3=d X4=e X5=f X6=g X7=h X8=i X9=j X10=k X11=l",
190  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l)));
192  "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",
193  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l, m)));
195  "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 "
196  "X13=n",
197  GMOCK_PP_FOR_EACH(JOINER, X, (a, b, c, d, e, f, g, h, i, j, k, l, m, n)));
199  "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 "
200  "X13=n X14=o",
202  (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)));
203 }
204 
205 } // namespace
206 } // namespace testing
#define EXPECT_EXPANSION(Result, Macro)
#define GMOCK_PP_IS_BEGIN_PARENS(...)
Definition: gmock-pp.h:115
#define GMOCK_PP_HAS_COMMA(...)
Definition: gmock-pp.h:35
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define GMOCK_PP_FOR_EACH(_Macro, _Data, _Tuple)
Definition: gmock-pp.h:131
#define GMOCK_PP_IS_EMPTY(...)
Definition: gmock-pp.h:79
#define GMOCK_PP_REPEAT(_Macro, _Data, _N)
Definition: gmock-pp.h:139
TEST(GTestEnvVarTest, Dummy)
#define JOINER(_N, _Data, _Elem)
#define GMOCK_PP_TAIL(...)
Definition: gmock-pp.h:44
#define GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1
#define GMOCK_PP_CAT(_1, _2)
Definition: gmock-pp.h:5
#define GMOCK_PP_NARG(...)
Definition: gmock-pp.h:29
Uncopyable z
#define GMOCK_PP_REMOVE_PARENS(...)
Definition: gmock-pp.h:125
#define GMOCK_PP_HEAD(...)
Definition: gmock-pp.h:40
#define GMOCK_PP_IF(_Cond, _Then, _Else)
Definition: gmock-pp.h:86
#define GMOCK_PP_INC(_i)
Definition: gmock-pp.h:144
#define GMOCK_PP_NARG0(...)
Definition: gmock-pp.h:110
#define GMOCK_PP_IS_ENCLOSED_PARENS(...)
Definition: gmock-pp.h:120
const double y