File: Backward.md

package info (click to toggle)
trompeloeil-cpp 43-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,540 kB
  • sloc: cpp: 15,524; sh: 114; makefile: 17
file content (540 lines) | stat: -rw-r--r-- 15,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# Backward compatibility with earlier versions of C\+\+

- [Introduction](#introduction)
- [C\+\+11 compromises](#cxx11_compromises)
  - [C\+\+11 API](#cxx11_api)
    - [`REQUIRE_CALL` example](#cxx11_require_call)
    - [`NAMED_REQUIRE_CALL` example](#cxx11_named_require_call)
    - [`ALLOW_CALL` example](#cxx11_allow_call)
    - [`NAMED_ALLOW_CALL` example](#cxx11_named_allow_call)
    - [`FORBID_CALL` example](#cxx11_forbid_call)
    - [`NAMED_FORBID_CALL` example](#cxx11_named_forbid_call)
    - [Migrating from C\+\+11 API to C\+\+14 API](#migrate_to_cxx14)
  - [Implicit conversion of `RETURN` modifier expression](#cxx11_return_implicit_conversion)
  - [Macro expansion of `ANY` matcher before stringizing](#cxx11_any_stringizing)
- [G\+\+ 4.8.x limitations](#gxx48x_limitations)
  - [Compiler defects](#gxx48x_compiler)
    - [Rvalue reference failures](gxx48x_compiler_rvalue_ref)
    - [Overload failures](#gxx48x_compiler_overload)
    - [! matcher failures](#gxx48x_compiler_neg_matcher)
    - [ANY matcher failures](#gxx48x_compiler_any_matcher)
    - [Summary of compiler matcher failures](#gxx48x_compiler_matcher_summary)
  - [Standard library defects](#gxx48x_library)
    - [Regular expressions](#gxx48x_library_regex)
    - [`<tuple>`](#gxx48x_library_tuple)

## <A name="introduction"/> Introduction

Trompeloeil is a C\+\+14-first library and looks forward to the future
where C\+\+17-and-beyond language features and standard library
further improve its utility and performance.

Sometimes though, a project comes along where instead of looking forward
one has to look backward.  How far backward?  All the way back to
`g++-4.8.4` with a vintage `libstdc++-v3` library and requirements for
compliance with the features available in the C\+\+11 standard without
compiler extensions.  (Indeed, it might be much worse: you might have
a project forced to use `g++-4.8.3` with further constraints.)

To allow you to consider Trompeloeil as your mock object framework
for a project with these constraints, we have back ported Trompeloeil to
`g++-4.8.4` (C\+\+11, `libstdc++-v3`) with an API that may be used with
a C\+\+11 dialect selected.  The C\+\+11 API remains supported when a C\+\+14
(or later) dialect is selected.

## <A name="cxx11_compromises"/> C\+\+11 compromises

Two major changes to functionality occur when compiling with the C\+\+11 dialect.

First, the API for defining expectations is different from the API used in
C\+\+14 and later dialects.

Second, the argument to a [**`RETURN`**](reference.md/#RETURN) modifier
undergoes an implicit conversion before semantic analysis.  This results in
a different error message when the expression cannot be implicitly converted
to the return type of the function for which the expectation is being defined.

A minor change is that the expansion of the [**`ANY`**](reference.md/#ANY)
matcher in arguments to expectations is stringized differently in the
C\+\+11 API when compared to the C\+\+14 API.

### <A name="cxx11_api"/> C\+\+11 API

It has not been possible to replicate the macro syntax that is used in
the C\+\+14 API.  The cause is an inability to find a mechanism to
communicate sufficient type information between expectation macros and
modifier macros.

Our approach has been to redefine expectation macros as taking the
modifiers, if any, as an argument to the expectation macro.

The C\+\+11 API has a similar name to the corresponding C\+\+14 API

```text
C++14 API                       C++11 API
------------------------------  --------------------------------
TROMPELOEIL_REQUIRE_CALL        TROMPELOEIL_REQUIRE_CALL_V
TROMPELOEIL_NAMED_REQUIRE_CALL  TROMPELOEIL_NAMED_REQUIRE_CALL_V
TROMPELOEIL_ALLOW_CALL          TROMPELOEIL_ALLOW_CALL_V
TROMPELOEIL_NAMED_ALLOW_CALL    TROMPELOEIL_NAMED_ALLOW_CALL_V
TROMPELOEIL_FORBID_CALL         TROMPELOEIL_FORBID_CALL_V
TROMPELOEIL_NAMED_FORBID_CALL   TROMPELOEIL_NAMED_FORBID_CALL_V
```

Short names for these macros have also been defined.

```text
Short macro name            Long macro name
--------------------        --------------------------------
REQUIRE_CALL_V              TROMPELOEIL_REQUIRE_CALL_V
NAMED_REQUIRE_CALL_V        TROMPELOEIL_NAMED_REQUIRE_CALL_V
ALLOW_CALL_V                TROMPELOEIL_ALLOW_CALL_V
NAMED_ALLOW_CALL_V          TROMPELOEIL_NAMED_ALLOW_CALL_V
FORBID_CALL_V               TROMPELOEIL_FORBID_CALL_V
NAMED_FORBID_CALL_V         TROMPELOEIL_NAMED_FORBID_CALL_V
```

In the C\+\+11 API, the expectation macros accept two or more
arguments, the first two being object and function and the remainder
being the modifiers, if any.  Therefore the `_V` suffix
may be read as an abbreviation of the word "variadic".

All documentation in Trompeloeil outside of this note uses the C\+\+14 API.
The examples below show how to translate from the C\+\+14 API to the C\+\+11
API.

#### <A name="cxx11_require_call"/> `REQUIRE_CALL` example

Given mock class `mock_c` with mock function `int getter(int)`,

```cpp
struct mock_c
{
  MAKE_MOCK1(getter, int(int));
};
```

and this expectation defined using the C\+\+14 API,

```cpp
  mock_c obj;

  // Two macros adjacent to each other in the program text.
  REQUIRE_CALL(obj, getter(ANY(int)))
    .RETURN(_1);
```

the corresponding expectation implemented using the C\+\+11 API is

```cpp
  mock_c obj;

  // One macro with three arguments.
  REQUIRE_CALL_V(obj, getter(ANY(int)),
    .RETURN(_1));
```

See also: [**`REQUIRE_CALL(...)`**](reference.md/#REQUIRE_CALL).

#### <A name="cxx11_named_require_call"/> `NAMED_REQUIRE_CALL` example

Given mock class `mock_c` with mock function `int getter(int)`,

```cpp
struct mock_c
{
  MAKE_MOCK1(getter, int(int));
};
```

and this expectation defined using the C\+\+14 API,

```cpp
  mock_c obj;

  // Two macros adjacent to each other in the program text.
  auto e = NAMED_REQUIRE_CALL(obj, getter(ANY(int)))
    .RETURN(0);
```

the corresponding expectation implemented using the C\+\+11 API is

```cpp
  mock_c obj;

  // One macro with three arguments.
  auto e = NAMED_REQUIRE_CALL_V(obj, getter(ANY(int)),
    .RETURN(0));
```

See also: [**`NAMED_REQUIRE_CALL(...)`**](reference.md/#NAMED_REQUIRE_CALL).

#### <A name="cxx11_allow_call"/> `ALLOW_CALL` example

Given mock class `mock_c` with mock function `int count()`,

```cpp
struct mock_c
{
  MAKE_MOCK0(count, int());
};
```

and this expectation defined using the C\+\+14 API,

```cpp
  mock_c obj;

  // Two macros adjacent to each other in the program text.
  ALLOW_CALL(obj, count())
    .RETURN(1);
```

the corresponding expectation implemented using the C\+\+11 API is,

```cpp
  mock_c obj;

  // One macro with three arguments.
  ALLOW_CALL_V(obj, count(),
    .RETURN(1));
```

See also: [**`ALLOW_CALL(...)`**](reference.md/#ALLOW_CALL).

#### <A name="cxx11_named_allow_call"/> `NAMED_ALLOW_CALL` example

Given mock class `mock_c` with mock function `int count()`,

```cpp
struct mock_c
{
  MAKE_MOCK0(count, int());
};
```

and this expectation defined using the C\+\+14 API,

```cpp
  mock_c obj;

  // Two macros adjacent to each other in the program text.
  auto e = NAMED_ALLOW_CALL(obj, count())
    .RETURN(1);
```

the corresponding expectation implemented using the C\+\+11 API is,

```cpp
  mock_c obj;

  // One macro with three arguments.
  auto e = NAMED_ALLOW_CALL_V(obj, count(),
    .RETURN(1));
```

See also: [**`NAMED_ALLOW_CALL(...)`**](reference.md/#NAMED_ALLOW_CALL).

#### <A name="cxx11_forbid_call"/> `FORBID_CALL` example

Given mock class `mock_c` with mock function `int count()`,

```cpp
struct mock_c
{
  MAKE_MOCK0(count, int());
};
```

and this expectation defined using the C\+\+14 API,

```cpp
  mock_c obj;

  FORBID_CALL(obj, count());
```

the corresponding expectation implemented using the C\+\+11 API is,

```cpp
  mock_c obj;

  FORBID_CALL_V(obj, count());
```

See also: [**`FORBID_CALL(...)`**](reference.md/#FORBID_CALL).

#### <A name="cxx11_named_forbid_call"/> `NAMED_FORBID_CALL` example

Given mock class `mock_c` with mock function `int count()`,

```cpp
struct mock_c
{
  MAKE_MOCK0(count, int());
};
```

and this expectation defined using the C\+\+14 API,

```cpp
  mock_c obj;

  auto e = NAMED_FORBID_CALL(obj, count());
```

the corresponding expectation implemented using the C\+\+11 API is,

```cpp
  mock_c obj;

  auto e = NAMED_FORBID_CALL_V(obj, count());
```

See also: [**`NAMED_FORBID_CALL(...)`**](reference.md/#NAMED_FORBID_CALL).

#### <A name="migrate_to_cxx14"/> Migrating from C\+\+11 API to C\+\+14 API

The C\+\+11 API remains available when changing your C\+\+ dialect to C\+\+14
or later.  Therefore existing test cases may be migrated incrementally
to the C\+\+14 API while using the C\+\+14 API for new test cases.

Steps:

- Remove the `_V` suffix from the expectation macro.
- Move the modifiers, if any, outside the argument list of the
  expectation macro.
- Remove workarounds such as
  - Changes to regular expressions that may be testing messages generated
    with the `ANY` matcher.
  - Any of the workarounds required to use Trompeloeil with `g++ 4.8.3`.

For example, given this expectation using the C\+\+11 API,

```cpp
  trompeloeil::sequence seq;

  auto thrown = false;

  REQUIRE_CALL_V(obj, getter(ANY(int)),
    .IN_SEQUENCE(seq)
    .LR_SIDE_EFFECT(thrown = true)
    .THROW(_1));
```

the corresponding C\+\+14 API implementation is,

```cpp
  trompeloeil::sequence seq;

  auto thrown = false;

  REQUIRE_CALL(obj, getter(ANY(int)))
    .IN_SEQUENCE(seq)
    .LR_SIDE_EFFECT(thrown = true)
    .THROW(_1);
```

### <A name="cxx11_return_implicit_conversion"/> Implicit conversion of `RETURN` modifier expression

In the C\+\+14 API, the [**`RETURN(...)`**](reference.md/#RETURN)
modifier stores the expression passed as an argument to the
modifier and forwards it to the Trompeloeil implementation
for semantic analysis.

In the C\+\+11 API, not only is the expression stored but also an
implicit conversion to the return type of the mocked function
is performed.  This implicit conversion may fail before
semantic checks are performed.

The result is that instead of a helpful message generated by
a `static_assert`, a less helpful error message is generated
by the compiler.

For example,

```cpp
struct MS
{
  MAKE_MOCK0(f, char&());
};

int main()
{
  MS obj;

  REQUIRE_CALL_V(obj, f(),
    .RETURN('a'));
}
```

The C\+\+11 error message is,

```text
error: invalid initialization of non-const reference of type
‘MS::trompeloeil_l_tag_type_trompeloeil_20::return_of_t {aka char&}’
from an rvalue of type ‘char’
```

The C\+\+14 error message is,

```text
error: static assertion failed:
RETURN non-reference from function returning reference
```

Other error messages that may not be generated while compiling using
a C\+\+11 dialect include:

```text
RETURN const* from function returning pointer to non-const

RETURN const& from function returning non-const reference

RETURN value is not convertible to the return type of the function
```

This is a quality of implementation issue that wasn't resolved before
the C\+\+11 API became available.

### <A name="cxx11_any_stringizing"/> Macro expansion of `ANY` matcher before stringizing

Macro expansion differences between C\+\+11 and C\+\+14 expectation macros
mean macros like the [**`ANY(...)`**](reference.md/#ANY_MACRO) matcher
used in the parameter list of the function to match are expanded in
C\+\+11 before they are stringized.

For example, a regular expression written in a C\+\+14 dialect, or later,

```cpp
  auto re = R":(Sequence expectations not met at destruction of sequence object "s":
  missing obj\.getter\(ANY\(int\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*
  missing obj\.foo\(_\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*
):";
```

won't match the string generated when running in a C\+\+11 dialect.
The `ANY(int)` matcher is actually expanded to some Trompeloeil-internal data.

A helper macro, such as `CXX11_AS_STRING()`, may be defined to give a
stringized form correct for C\+\+11:

```cpp
#define CXX11_AS_STRING_IMPL(x) #x
#define CXX11_AS_STRING(x) CXX11_AS_STRING_IMPL(x)
```

It may be used in constructing larger strings.  Rewriting the example above
illustrates its use:

```cpp
  auto re = R":(Sequence expectations not met at destruction of sequence object "s":
  missing obj\.getter\():" +
  escape_parens(CXX11_AS_STRING(ANY(int))) +
  R":(\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*
  missing obj\.foo\(_\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*
):";
```

where `escape_parens()` performs escaping of parentheses in the input string,

```cpp
  std::string escape_parens(const std::string& s)
  {
    constexpr auto backslash = '\\';

    std::string tmp;

    for (auto& c : s)
    {
      if (c == '(' || c == ')')
      {
        tmp += backslash;
      }

      tmp += c;
    }

    return tmp;
  }
```

This is a quality of implementation issue that wasn't resolved before
the C\+\+11 API became available.

## <A name="gxx48x_limitations"/> G\+\+ 4.8.x limitations

Trompeloeil's support for `g++ 4.8.x` is limited by compiler and standard
library defects.

### <A name="gxx48x_compiler"/> Compiler defects

`g++-4.8.3` and older suffers from [bug 58714](
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58714) which causes the
wrong overload to be selected when overloads for mock functions exists
for both `T&&` and `const T&` parameters.

Example:

```Cpp
struct S
{
    MAKE_MOCK1(func, void(const int&));
    MAKE_MOCK1(func, void(int&&));
};

void test()
{
  S s;
  REQUIRE_CALL_V(s, func(ANY(const int&)));

  const int i = 0;
  s.func(i);
}
```

With `g++-4.8.3` and older, this is reported as:

```text
No match for call of func with signature void(const int&) with.

  param  _1 == 1
```

because the expectation is wrongly placed on the `int&&` overload.

Consider moving to a more modern compiler.

### <A name="gxx48x_library"/> Standard library defects

#### <A name="gxx48x_library_regex"/> Regular expressions

`g++ 4.8.x` lacks support for regular expressions in `libstdc++-v3`.

As Jonathan Wakely said,
> *sigh* \<regex\> is not implemented prior to GCC 4.9.0,
> I thought the whole world was aware of that by now.

See: GCC Bugzilla, "Bug 61582 - C\+\+11 regex memory corruption,"
23 June 2014. \
Available: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61582> \
Accessed: 9 November 2017

As a consequence, avoid the [**`re(...)`**](reference.md/#re) matcher
in test cases when compiling with a C\+\+11 dialect.

#### <A name="gxx48x_library_tuple"/> `<tuple>`

`g++-4.8.3` has a defect in `<tuple>` in `libstdc++-v3`.

See: GCC Bugzilla, "Bug 61947 - [4.8/4.9 Regression] Ambiguous calls when constructing std::tuple,"
29 July 2014. \
Available: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61947> \
Accessed: 8 November 2017

This is fixed in `g++-4.8.4`, `g++-4.9.3` and later releases.

One workaround for this defect is to copy a version of `<tuple>` from a
release of `g++-4.8.4` or `g++-4.8.5` and arrange to include it
ahead of the buggy version.  Then work hard to move forward to a more recent
compiler release.