Skip to content

Commit 7060c7c

Browse files
committed
Refactorization: Reduced code duplication in testlibrary.cpp
1 parent a837cc4 commit 7060c7c

2 files changed

Lines changed: 26 additions & 59 deletions

File tree

test/testerrorlogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,6 @@ class TestErrorLogger : public TestFixture {
384384
reportUnmatchedSuppressions(suppressions);
385385
ASSERT_EQUALS("[a.c:10]: (information) Unmatched suppression: abc\n", errout.str());
386386
}
387-
388387
};
388+
389389
REGISTER_TEST(TestErrorLogger)

test/testlibrary.cpp

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ class TestLibrary : public TestFixture {
4848
TEST_CASE(version);
4949
}
5050

51+
Library::Error readLibrary(Library& library, const char* xmldata) const {
52+
tinyxml2::XMLDocument doc;
53+
doc.Parse(xmldata);
54+
return library.load(doc);
55+
}
56+
5157
void empty() const {
5258
const char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
53-
tinyxml2::XMLDocument doc;
54-
doc.Parse(xmldata, sizeof(xmldata));
5559

5660
Library library;
57-
library.load(doc);
61+
readLibrary(library, xmldata);
5862
ASSERT(library.use.empty());
5963
ASSERT(library.leakignore.empty());
6064
ASSERT(library.argumentChecks.empty());
@@ -67,16 +71,14 @@ class TestLibrary : public TestFixture {
6771
" <noreturn>false</noreturn>\n"
6872
" </function>\n"
6973
"</def>";
70-
tinyxml2::XMLDocument doc;
71-
doc.Parse(xmldata, sizeof(xmldata));
7274

7375
TokenList tokenList(nullptr);
7476
std::istringstream istr("foo();");
7577
tokenList.createTokens(istr);
7678
tokenList.front()->next()->astOperand1(tokenList.front());
7779

7880
Library library;
79-
library.load(doc);
81+
readLibrary(library, xmldata);
8082
ASSERT(library.use.empty());
8183
ASSERT(library.leakignore.empty());
8284
ASSERT(library.argumentChecks.empty());
@@ -90,16 +92,15 @@ class TestLibrary : public TestFixture {
9092
" <arg nr=\"1\"/>"
9193
" </function>\n"
9294
"</def>";
93-
tinyxml2::XMLDocument doc;
94-
doc.Parse(xmldata, sizeof(xmldata));
95+
96+
Library library;
97+
readLibrary(library, xmldata);
9598

9699
{
97100
TokenList tokenList(nullptr);
98101
std::istringstream istr("fred.foo(123);"); // <- wrong scope, not library function
99102
tokenList.createTokens(istr);
100103

101-
Library library;
102-
library.load(doc);
103104
ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2)));
104105
}
105106

@@ -108,8 +109,6 @@ class TestLibrary : public TestFixture {
108109
std::istringstream istr("Fred::foo(123);"); // <- wrong scope, not library function
109110
tokenList.createTokens(istr);
110111

111-
Library library;
112-
library.load(doc);
113112
ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2)));
114113
}
115114
}
@@ -121,16 +120,14 @@ class TestLibrary : public TestFixture {
121120
" <arg nr=\"1\"/>"
122121
" </function>\n"
123122
"</def>";
124-
tinyxml2::XMLDocument doc;
125-
doc.Parse(xmldata, sizeof(xmldata));
126123

127124
TokenList tokenList(nullptr);
128125
std::istringstream istr("foo();"); // <- too few arguments, not library function
129126
tokenList.createTokens(istr);
130127
tokenList.front()->next()->astOperand1(tokenList.front());
131128

132129
Library library;
133-
library.load(doc);
130+
readLibrary(library, xmldata);
134131
ASSERT(library.isNotLibraryFunction(tokenList.front()));
135132
}
136133

@@ -141,8 +138,6 @@ class TestLibrary : public TestFixture {
141138
" <arg nr=\"1\"/>"
142139
" </function>\n"
143140
"</def>";
144-
tinyxml2::XMLDocument doc;
145-
doc.Parse(xmldata, sizeof(xmldata));
146141

147142
TokenList tokenList(nullptr);
148143
std::istringstream istr("Fred foo(123);"); // <- Variable declaration, not library function
@@ -151,7 +146,7 @@ class TestLibrary : public TestFixture {
151146
tokenList.front()->next()->varId(1);
152147

153148
Library library;
154-
library.load(doc);
149+
readLibrary(library, xmldata);
155150
ASSERT(library.isNotLibraryFunction(tokenList.front()->next()));
156151
}
157152

@@ -166,11 +161,9 @@ class TestLibrary : public TestFixture {
166161
" <arg nr=\"5\"><not-bool/></arg>\n"
167162
" </function>\n"
168163
"</def>";
169-
tinyxml2::XMLDocument doc;
170-
doc.Parse(xmldata, sizeof(xmldata));
171164

172165
Library library;
173-
library.load(doc);
166+
readLibrary(library, xmldata);
174167
ASSERT_EQUALS(true, library.argumentChecks["foo"][1].notuninit);
175168
ASSERT_EQUALS(true, library.argumentChecks["foo"][2].notnull);
176169
ASSERT_EQUALS(true, library.argumentChecks["foo"][3].formatstr);
@@ -185,11 +178,9 @@ class TestLibrary : public TestFixture {
185178
" <arg nr=\"any\"><not-uninit/></arg>\n"
186179
"</function>\n"
187180
"</def>";
188-
tinyxml2::XMLDocument doc;
189-
doc.Parse(xmldata, sizeof(xmldata));
190181

191182
Library library;
192-
library.load(doc);
183+
readLibrary(library, xmldata);
193184
ASSERT_EQUALS(true, library.argumentChecks["foo"][-1].notuninit);
194185
}
195186

@@ -204,11 +195,9 @@ class TestLibrary : public TestFixture {
204195
" <arg nr=\"5\"><valid>:1,5</valid></arg>\n"
205196
" </function>\n"
206197
"</def>";
207-
tinyxml2::XMLDocument doc;
208-
doc.Parse(xmldata, sizeof(xmldata));
209198

210199
Library library;
211-
library.load(doc);
200+
readLibrary(library, xmldata);
212201

213202
TokenList tokenList(nullptr);
214203
std::istringstream istr("foo(a,b,c,d,e);");
@@ -257,11 +246,9 @@ class TestLibrary : public TestFixture {
257246
" <arg nr=\"3\"/>\n"
258247
" </function>\n"
259248
"</def>";
260-
tinyxml2::XMLDocument doc;
261-
doc.Parse(xmldata, sizeof(xmldata));
262249

263250
Library library;
264-
library.load(doc);
251+
readLibrary(library, xmldata);
265252

266253
TokenList tokenList(nullptr);
267254
std::istringstream istr("foo(a,b,c);");
@@ -296,11 +283,9 @@ class TestLibrary : public TestFixture {
296283
" <noreturn>false</noreturn>\n"
297284
" </function>\n"
298285
"</def>";
299-
tinyxml2::XMLDocument doc;
300-
doc.Parse(xmldata, sizeof(xmldata));
301286

302287
Library library;
303-
library.load(doc);
288+
readLibrary(library, xmldata);
304289
ASSERT(library.use.empty());
305290
ASSERT(library.leakignore.empty());
306291
ASSERT(library.argumentChecks.empty());
@@ -328,11 +313,9 @@ class TestLibrary : public TestFixture {
328313
" <dealloc>DeleteX</dealloc>\n"
329314
" </memory>\n"
330315
"</def>";
331-
tinyxml2::XMLDocument doc;
332-
doc.Parse(xmldata, sizeof(xmldata));
333316

334317
Library library;
335-
library.load(doc);
318+
readLibrary(library, xmldata);
336319
ASSERT(library.use.empty());
337320
ASSERT(library.leakignore.empty());
338321
ASSERT(library.argumentChecks.empty());
@@ -372,11 +355,9 @@ class TestLibrary : public TestFixture {
372355
" <dealloc>DeleteX</dealloc>\n"
373356
" </resource>\n"
374357
"</def>";
375-
tinyxml2::XMLDocument doc;
376-
doc.Parse(xmldata, sizeof(xmldata));
377358

378359
Library library;
379-
library.load(doc);
360+
readLibrary(library, xmldata);
380361
ASSERT(library.use.empty());
381362
ASSERT(library.leakignore.empty());
382363
ASSERT(library.argumentChecks.empty());
@@ -390,11 +371,8 @@ class TestLibrary : public TestFixture {
390371
"<def>\n"
391372
" <podtype name=\"s16\" size=\"2\"/>\n"
392373
"</def>";
393-
tinyxml2::XMLDocument doc;
394-
doc.Parse(xmldata, sizeof(xmldata));
395-
396374
Library library;
397-
library.load(doc);
375+
readLibrary(library, xmldata);
398376

399377
const struct Library::PodType *type = library.podtype("s16");
400378
ASSERT_EQUALS(2U, type ? type->size : 0U);
@@ -432,11 +410,9 @@ class TestLibrary : public TestFixture {
432410
" <access indexOperator=\"array-like\"/>\n"
433411
" </container>\n"
434412
"</def>";
435-
tinyxml2::XMLDocument doc;
436-
doc.Parse(xmldata, sizeof(xmldata));
437413

438414
Library library;
439-
library.load(doc);
415+
readLibrary(library, xmldata);
440416

441417
Library::Container& A = library.containers["A"];
442418
Library::Container& B = library.containers["B"];
@@ -482,33 +458,24 @@ class TestLibrary : public TestFixture {
482458
const char xmldata [] = "<?xml version=\"1.0\"?>\n"
483459
"<def>\n"
484460
"</def>";
485-
tinyxml2::XMLDocument doc;
486-
doc.Parse(xmldata, sizeof(xmldata));
487-
488461
Library library;
489-
Library::Error err = library.load(doc);
462+
Library::Error err = readLibrary(library, xmldata);
490463
ASSERT_EQUALS(err.errorcode, Library::OK);
491464
}
492465
{
493466
const char xmldata [] = "<?xml version=\"1.0\"?>\n"
494467
"<def format=\"1\">\n"
495468
"</def>";
496-
tinyxml2::XMLDocument doc;
497-
doc.Parse(xmldata, sizeof(xmldata));
498-
499469
Library library;
500-
Library::Error err = library.load(doc);
470+
Library::Error err = readLibrary(library, xmldata);
501471
ASSERT_EQUALS(err.errorcode, Library::OK);
502472
}
503473
{
504474
const char xmldata [] = "<?xml version=\"1.0\"?>\n"
505475
"<def format=\"42\">\n"
506476
"</def>";
507-
tinyxml2::XMLDocument doc;
508-
doc.Parse(xmldata, sizeof(xmldata));
509-
510477
Library library;
511-
Library::Error err = library.load(doc);
478+
Library::Error err = readLibrary(library, xmldata);
512479
ASSERT_EQUALS(err.errorcode, Library::UNSUPPORTED_FORMAT);
513480
}
514481
}

0 commit comments

Comments
 (0)