• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cpputest / cpputest / 15141709115

20 May 2025 03:31PM UTC coverage: 99.281%. Remained the same
15141709115

push

github

web-flow
Merge pull request #1859 from mtfurlan/ci/run-docker

ci: actually run docker builds

6765 of 6814 relevant lines covered (99.28%)

46692.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.07
/src/CppUTestExt/MockExpectedCallsList.cpp
1
/*
2
 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *     * Redistributions of source code must retain the above copyright
8
 *       notice, this list of conditions and the following disclaimer.
9
 *     * Redistributions in binary form must reproduce the above copyright
10
 *       notice, this list of conditions and the following disclaimer in the
11
 *       documentation and/or other materials provided with the distribution.
12
 *     * Neither the name of the <organization> nor the
13
 *       names of its contributors may be used to endorse or promote products
14
 *       derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ''AS IS'' AND ANY
17
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27

28
#include "CppUTest/TestHarness.h"
29
#include "CppUTestExt/MockExpectedCallsList.h"
30
#include "CppUTestExt/MockCheckedExpectedCall.h"
31

32
MockExpectedCallsList::MockExpectedCallsList() : head_(NULLPTR)
788✔
33
{
34
}
788✔
35

36
MockExpectedCallsList::~MockExpectedCallsList()
848✔
37
{
38
    while (head_) {
1,060✔
39
        MockExpectedCallsListNode* next = head_->next_;
272✔
40
        delete head_;
272✔
41
        head_ = next;
272✔
42
    }
43
}
848✔
44

45
bool MockExpectedCallsList::hasCallsOutOfOrder() const
901✔
46
{
47
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
1,287✔
48
        if (p->expectedCall_->isOutOfOrder())
392✔
49
            return true;
6✔
50
    return false;
895✔
51
}
52

53
unsigned int MockExpectedCallsList::size() const
11✔
54
{
55
    unsigned int count = 0;
11✔
56
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
23✔
57
        count++;
12✔
58
    return count;
11✔
59
}
60

61
bool MockExpectedCallsList::isEmpty() const
730✔
62
{
63
    return head_ == NULLPTR;
730✔
64
}
65

66
unsigned int MockExpectedCallsList::amountOfActualCallsFulfilledFor(const SimpleString& name) const
66✔
67
{
68
    unsigned int count = 0;
66✔
69
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_) {
109✔
70
        if (p->expectedCall_->relatesTo(name)) {
43✔
71
            count += p->expectedCall_->getActualCallsFulfilled();
37✔
72
        }
73
    }
74
    return count;
66✔
75
}
76

77
unsigned int MockExpectedCallsList::amountOfUnfulfilledExpectations() const
3✔
78
{
79
    unsigned int count = 0;
3✔
80
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
9✔
81
        if (! p->expectedCall_->isFulfilled()) count++;
6✔
82
    return count;
3✔
83
}
84

85
bool MockExpectedCallsList::hasFinalizedMatchingExpectations() const
49✔
86
{
87
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_) {
110✔
88
        if (p->expectedCall_->isMatchingActualCallAndFinalized()) {
63✔
89
            return true;
2✔
90
        }
91
    }
92
    return false;
47✔
93
}
94

95
bool MockExpectedCallsList::hasUnfulfilledExpectations() const
881✔
96
{
97
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_) {
1,240✔
98
        if (!p->expectedCall_->isFulfilled()) {
383✔
99
            return true;
24✔
100
        }
101
    }
102
    return false;
857✔
103
}
104

105
bool MockExpectedCallsList::hasExpectationWithName(const SimpleString& name) const
58✔
106
{
107
        for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
115✔
108
            if (p->expectedCall_->relatesTo(name))
80✔
109
                return true;
23✔
110
    return false;
35✔
111
}
112

113
void MockExpectedCallsList::addExpectedCall(MockCheckedExpectedCall* call)
1,198✔
114
{
115
    MockExpectedCallsListNode* newCall = new MockExpectedCallsListNode(call);
1,198✔
116

117
    if (head_ == NULLPTR)
1,198✔
118
        head_ = newCall;
894✔
119
    else {
120
        MockExpectedCallsListNode* lastCall = head_;
304✔
121
        while (lastCall->next_) lastCall = lastCall->next_;
535✔
122
        lastCall->next_ = newCall;
304✔
123
    }
124
}
1,198✔
125

126
void MockExpectedCallsList::addPotentiallyMatchingExpectations(const MockExpectedCallsList& list)
421✔
127
{
128
    for (MockExpectedCallsListNode* p = list.head_; p; p = p->next_)
1,192✔
129
        if (p->expectedCall_->canMatchActualCalls())
771✔
130
            addExpectedCall(p->expectedCall_);
476✔
131
}
421✔
132

133
void MockExpectedCallsList::addExpectationsRelatedTo(const SimpleString& name, const MockExpectedCallsList& list)
94✔
134
{
135
    for (MockExpectedCallsListNode* p = list.head_; p; p = p->next_)
215✔
136
        if (p->expectedCall_->relatesTo(name))
121✔
137
            addExpectedCall(p->expectedCall_);
110✔
138
}
94✔
139

140
void MockExpectedCallsList::addExpectations(const MockExpectedCallsList& list)
33✔
141
{
142
    for (MockExpectedCallsListNode* p = list.head_; p; p = p->next_)
81✔
143
        addExpectedCall(p->expectedCall_);
48✔
144
}
33✔
145

146
void MockExpectedCallsList::onlyKeepExpectationsRelatedTo(const SimpleString& name)
423✔
147
{
148
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
907✔
149
        if (! p->expectedCall_->relatesTo(name))
484✔
150
            p->expectedCall_ = NULLPTR;
46✔
151

152
    pruneEmptyNodeFromList();
423✔
153
}
423✔
154

155
void MockExpectedCallsList::onlyKeepOutOfOrderExpectations()
9✔
156
{
157
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
31✔
158
        if (!p->expectedCall_->isOutOfOrder())
22✔
159
            p->expectedCall_ = NULLPTR;
5✔
160
    pruneEmptyNodeFromList();
9✔
161
}
9✔
162

163
void MockExpectedCallsList::onlyKeepUnmatchingExpectations()
272✔
164
{
165
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
589✔
166
        if (p->expectedCall_->isMatchingActualCallAndFinalized())
317✔
167
        {
168
            p->expectedCall_->resetActualCallMatchingState();
2✔
169
            p->expectedCall_ = NULLPTR;
2✔
170
        }
171

172
    pruneEmptyNodeFromList();
272✔
173
}
272✔
174

175
void MockExpectedCallsList::onlyKeepExpectationsWithInputParameterName(const SimpleString& name)
29✔
176
{
177
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
62✔
178
        if (! p->expectedCall_->hasInputParameterWithName(name))
33✔
179
            p->expectedCall_ = NULLPTR;
9✔
180
    pruneEmptyNodeFromList();
29✔
181
}
29✔
182

183
void MockExpectedCallsList::onlyKeepExpectationsWithOutputParameterName(const SimpleString& name)
8✔
184
{
185
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
18✔
186
        if (! p->expectedCall_->hasOutputParameterWithName(name))
10✔
187
            p->expectedCall_ = NULLPTR;
8✔
188
    pruneEmptyNodeFromList();
8✔
189
}
8✔
190

191
void MockExpectedCallsList::onlyKeepExpectationsWithInputParameter(const MockNamedValue& parameter)
230✔
192
{
193
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
500✔
194
        if (! p->expectedCall_->hasInputParameter(parameter))
270✔
195
            p->expectedCall_ = NULLPTR;
27✔
196
    pruneEmptyNodeFromList();
230✔
197
}
230✔
198

199
void MockExpectedCallsList::onlyKeepExpectationsWithOutputParameter(const MockNamedValue& parameter)
42✔
200
{
201
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
90✔
202
        if (! p->expectedCall_->hasOutputParameter(parameter))
48✔
203
            p->expectedCall_ = NULLPTR;
3✔
204
    pruneEmptyNodeFromList();
42✔
205
}
42✔
206

207
void MockExpectedCallsList::onlyKeepExpectationsOnObject(const void* objectPtr)
7✔
208
{
209
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
12✔
210
        if (! p->expectedCall_->relatesToObject(objectPtr))
5✔
211
            p->expectedCall_ = NULLPTR;
1✔
212
    pruneEmptyNodeFromList();
7✔
213
}
7✔
214

215
MockCheckedExpectedCall* MockExpectedCallsList::removeFirstFinalizedMatchingExpectation()
644✔
216
{
217
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_) {
1,021✔
218
        if (p->expectedCall_->isMatchingActualCallAndFinalized()) {
703✔
219
            MockCheckedExpectedCall* matchingCall = p->expectedCall_;
326✔
220
            p->expectedCall_ = NULLPTR;
326✔
221
            pruneEmptyNodeFromList();
326✔
222
            return matchingCall;
326✔
223
        }
224
    }
225
    return NULLPTR;
318✔
226
}
227

228
MockCheckedExpectedCall* MockExpectedCallsList::getFirstMatchingExpectation()
318✔
229
{
230
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_) {
614✔
231
        if (p->expectedCall_->isMatchingActualCall()) {
358✔
232
            return p->expectedCall_;
62✔
233
        }
234
    }
235
    return NULLPTR;
256✔
236
}
237

238
MockCheckedExpectedCall* MockExpectedCallsList::removeFirstMatchingExpectation()
43✔
239
{
240
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_) {
54✔
241
        if (p->expectedCall_->isMatchingActualCall()) {
45✔
242
            MockCheckedExpectedCall* matchingCall = p->expectedCall_;
34✔
243
            p->expectedCall_ = NULLPTR;
34✔
244
            pruneEmptyNodeFromList();
34✔
245
            return matchingCall;
34✔
246
        }
247
    }
248
    return NULLPTR;
9✔
249
}
250

251
void MockExpectedCallsList::pruneEmptyNodeFromList()
1,380✔
252
{
253
    MockExpectedCallsListNode* current = head_;
1,380✔
254
    MockExpectedCallsListNode* previous = NULLPTR;
1,380✔
255
    MockExpectedCallsListNode* toBeDeleted = NULLPTR;
1,380✔
256

257
    while (current) {
2,962✔
258
        if (current->expectedCall_ == NULLPTR) {
1,582✔
259
            toBeDeleted = current;
461✔
260
            if (previous == NULLPTR)
461✔
261
                head_ = current = current->next_;
420✔
262
            else
263
                current = previous->next_ = current->next_;
41✔
264
            delete toBeDeleted;
461✔
265
        }
266
        else {
267
            previous = current;
1,121✔
268
            current = current->next_;
1,121✔
269
        }
270
    }
271
}
1,380✔
272

273
void MockExpectedCallsList::deleteAllExpectationsAndClearList()
1,056✔
274
{
275
    while (head_) {
1,521✔
276
        MockExpectedCallsListNode* next = head_->next_;
465✔
277
        delete head_->expectedCall_;
465✔
278
        delete head_;
465✔
279
        head_ = next;
465✔
280
    }
281
}
1,056✔
282

283
void MockExpectedCallsList::resetActualCallMatchingState()
367✔
284
{
285
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
401✔
286
        p->expectedCall_->resetActualCallMatchingState();
34✔
287
}
367✔
288

289
void MockExpectedCallsList::wasPassedToObject()
6✔
290
{
291
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
10✔
292
        p->expectedCall_->wasPassedToObject();
4✔
293
}
6✔
294

295

296
void MockExpectedCallsList::parameterWasPassed(const SimpleString& parameterName)
215✔
297
{
298
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
456✔
299
        p->expectedCall_->inputParameterWasPassed(parameterName);
241✔
300
}
215✔
301

302
void MockExpectedCallsList::outputParameterWasPassed(const SimpleString& parameterName)
39✔
303
{
304
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
84✔
305
        p->expectedCall_->outputParameterWasPassed(parameterName);
45✔
306
}
39✔
307

308
static SimpleString stringOrNoneTextWhenEmpty(const SimpleString& inputString, const SimpleString& linePrefix)
325✔
309
{
310
    SimpleString str = inputString;
325✔
311
    if (str == "") {
325✔
312
        str += linePrefix;
177✔
313
        str += "<none>";
177✔
314
    }
315
    return str;
325✔
316
}
×
317

318
static SimpleString appendStringOnANewLine(const SimpleString& inputString, const SimpleString& linePrefix, const SimpleString& stringToAppend)
192✔
319
{
320
    SimpleString str = inputString;
192✔
321
    if (str != "") str += "\n";
192✔
322
    str += linePrefix;
192✔
323
    str += stringToAppend;
192✔
324
    return str;
192✔
325
}
×
326

327
SimpleString MockExpectedCallsList::unfulfilledCallsToString(const SimpleString& linePrefix) const
156✔
328
{
329
    SimpleString str;
156✔
330
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
315✔
331
        if (!p->expectedCall_->isFulfilled())
159✔
332
            str = appendStringOnANewLine(str, linePrefix, p->expectedCall_->callToString());
105✔
333
    return stringOrNoneTextWhenEmpty(str, linePrefix);
312✔
334
}
156✔
335

336
SimpleString MockExpectedCallsList::fulfilledCallsToString(const SimpleString& linePrefix) const
155✔
337
{
338
    SimpleString str;
155✔
339

340
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
313✔
341
        if (p->expectedCall_->isFulfilled())
158✔
342
            str = appendStringOnANewLine(str, linePrefix, p->expectedCall_->callToString());
55✔
343

344
    return stringOrNoneTextWhenEmpty(str, linePrefix);
310✔
345
}
155✔
346

347
SimpleString MockExpectedCallsList::callsWithMissingParametersToString(const SimpleString& linePrefix, 
14✔
348
                                                                       const SimpleString& missingParametersPrefix) const
349
{
350
    SimpleString str;
14✔
351
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
30✔
352
    {
353
        str = appendStringOnANewLine(str, linePrefix, p->expectedCall_->callToString());
16✔
354
        str = appendStringOnANewLine(str, linePrefix + missingParametersPrefix, p->expectedCall_->missingParametersToString());
16✔
355
    }
356

357
    return stringOrNoneTextWhenEmpty(str, linePrefix);
28✔
358
}
14✔
359

360
bool MockExpectedCallsList::hasUnmatchingExpectationsBecauseOfMissingParameters() const
9✔
361
{
362
    for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
14✔
363
        if (! p->expectedCall_->areParametersMatchingActualCall())
11✔
364
            return true;
6✔
365
    return false;
3✔
366
}
367

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc