Skip to content

Commit 36bdbfb

Browse files
Tests: print the error message insead of just a number (or nothing)
Some QCOMPARE did convert to int() so we would see an error number, but not all comparisons did. For those those that didn't, QCOMPARE would just print that the values differed, but not show what. So try to harmonize on printing the error message. I may not have caught all uses... Signed-off-by: Thiago Macieira <[email protected]>
1 parent 345ffce commit 36bdbfb

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

tests/encoder/tst_encoder.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#endif
3131

3232
Q_DECLARE_METATYPE(CborError)
33+
namespace QTest {
34+
template<> char *toString<CborError>(const CborError &err)
35+
{
36+
return qstrdup(cbor_error_string(err));
37+
}
38+
}
3339

3440
class tst_Encoder : public QObject
3541
{
@@ -269,7 +275,7 @@ void compare(const QVariant &input, const QByteArray &output)
269275
CborEncoder encoder;
270276
cbor_encoder_init(&encoder, bufptr, buffer.length(), 0);
271277

272-
QCOMPARE(int(encodeVariant(&encoder, input)), int(CborNoError));
278+
QCOMPARE(encodeVariant(&encoder, input), CborNoError);
273279
QCOMPARE(encoder.added, size_t(1));
274280
QCOMPARE(cbor_encoder_get_extra_bytes_needed(&encoder), size_t(0));
275281

@@ -635,9 +641,9 @@ void tst_Encoder::tooShortArrays()
635641
CborEncoder encoder, container;
636642
cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
637643
QCOMPARE(cbor_encoder_create_array(&encoder, &container, 2), CborNoError);
638-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
644+
QCOMPARE(encodeVariant(&container, input), CborNoError);
639645
QCOMPARE(container.added, size_t(1));
640-
QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooFewItems));
646+
QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooFewItems);
641647
}
642648

643649
void tst_Encoder::tooShortMaps()
@@ -649,9 +655,9 @@ void tst_Encoder::tooShortMaps()
649655
CborEncoder encoder, container;
650656
cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
651657
QCOMPARE(cbor_encoder_create_map(&encoder, &container, 2), CborNoError);
652-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
658+
QCOMPARE(encodeVariant(&container, input), CborNoError);
653659
QCOMPARE(container.added, size_t(1));
654-
QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooFewItems));
660+
QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooFewItems);
655661
}
656662

657663
void tst_Encoder::tooBigArrays()
@@ -663,10 +669,10 @@ void tst_Encoder::tooBigArrays()
663669
CborEncoder encoder, container;
664670
cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
665671
QCOMPARE(cbor_encoder_create_array(&encoder, &container, 1), CborNoError);
666-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
667-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
672+
QCOMPARE(encodeVariant(&container, input), CborNoError);
673+
QCOMPARE(encodeVariant(&container, input), CborNoError);
668674
QCOMPARE(container.added, size_t(2));
669-
QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooManyItems));
675+
QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooManyItems);
670676
}
671677

672678
void tst_Encoder::tooBigMaps()
@@ -678,11 +684,11 @@ void tst_Encoder::tooBigMaps()
678684
CborEncoder encoder, container;
679685
cbor_encoder_init(&encoder, reinterpret_cast<quint8 *>(buffer.data()), buffer.length(), 0);
680686
QCOMPARE(cbor_encoder_create_map(&encoder, &container, 1), CborNoError);
681-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
682-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
683-
QCOMPARE(int(encodeVariant(&container, input)), int(CborNoError));
687+
QCOMPARE(encodeVariant(&container, input), CborNoError);
688+
QCOMPARE(encodeVariant(&container, input), CborNoError);
689+
QCOMPARE(encodeVariant(&container, input), CborNoError);
684690
QCOMPARE(container.added, size_t(3));
685-
QCOMPARE(int(cbor_encoder_close_container_checked(&encoder, &container)), int(CborErrorTooManyItems));
691+
QCOMPARE(cbor_encoder_close_container_checked(&encoder, &container), CborErrorTooManyItems);
686692
}
687693

688694
void tst_Encoder::illegalSimpleType_data()

tests/parser/tst_parser.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ extern "C" FILE *open_memstream(char **bufptr, size_t *sizeptr);
3333
#endif
3434

3535
Q_DECLARE_METATYPE(CborError)
36+
namespace QTest {
37+
template<> char *toString<CborError>(const CborError &err)
38+
{
39+
return qstrdup(cbor_error_string(err));
40+
}
41+
}
3642

3743
class tst_Parser : public QObject
3844
{
@@ -233,7 +239,7 @@ void compareOne_real(const QByteArray &data, const QString &expected, int line,
233239
QCOMPARE(decoded, expected);
234240

235241
// check that the errors are the same
236-
QCOMPARE(int(err2), int(err));
242+
QCOMPARE(err2, err);
237243

238244
// check that we consumed everything
239245
QCOMPARE((void*)cbor_value_get_next_byte(&first), (void*)data.constEnd());
@@ -1261,15 +1267,15 @@ void tst_Parser::checkedIntegers()
12611267
int64_t v;
12621268
err = cbor_value_get_int64_checked(&value, &v);
12631269
if (result.isNull()) {
1264-
QCOMPARE(int(err), int(CborErrorDataTooLarge));
1270+
QCOMPARE(err, CborErrorDataTooLarge);
12651271
} else {
12661272
QCOMPARE(v, expected);
12671273
}
12681274

12691275
int v2;
12701276
err = cbor_value_get_int_checked(&value, &v2);
12711277
if (result.isNull() || expected < std::numeric_limits<int>::min() || expected > std::numeric_limits<int>::max()) {
1272-
QCOMPARE(int(err), int(CborErrorDataTooLarge));
1278+
QCOMPARE(err, CborErrorDataTooLarge);
12731279
} else {
12741280
QCOMPARE(int64_t(v2), expected);
12751281
}
@@ -1534,10 +1540,10 @@ void tst_Parser::validation()
15341540
CborError err2 = cbor_value_validate_basic(&first);
15351541
CborError err3 = cbor_value_validate(&first, CborValidateBasic);
15361542
err = parseOne(&first, &decoded);
1537-
QCOMPARE(int(err), int(expectedError));
1543+
QCOMPARE(err, expectedError);
15381544
if (!QByteArray(QTest::currentDataTag()).contains("utf8")) {
1539-
QCOMPARE(int(err2), int(expectedError));
1540-
QCOMPARE(int(err3), int(expectedError));
1545+
QCOMPARE(err2, expectedError);
1546+
QCOMPARE(err3, expectedError);
15411547
}
15421548
}
15431549

@@ -1903,7 +1909,7 @@ void tst_Parser::strictValidation()
19031909
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
19041910

19051911
err = cbor_value_validate(&first, flags);
1906-
QCOMPARE(int(err), int(expectedError));
1912+
QCOMPARE(err, expectedError);
19071913
}
19081914

19091915
void tst_Parser::resumeParsing_data()
@@ -1930,7 +1936,7 @@ void tst_Parser::resumeParsing()
19301936
}
19311937
if (err != CborErrorUnexpectedEOF)
19321938
qDebug() << "Length is" << len;
1933-
QCOMPARE(int(err), int(CborErrorUnexpectedEOF));
1939+
QCOMPARE(err, CborErrorUnexpectedEOF);
19341940
}
19351941
}
19361942

@@ -2025,13 +2031,13 @@ void tst_Parser::recursionLimit()
20252031

20262032
it = first;
20272033
err = cbor_value_advance(&it);
2028-
QCOMPARE(int(err), int(CborErrorNestingTooDeep));
2034+
QCOMPARE(err, CborErrorNestingTooDeep);
20292035

20302036
it = first;
20312037
if (cbor_value_is_map(&it)) {
20322038
CborValue dummy;
20332039
err = cbor_value_map_find_value(&it, "foo", &dummy);
2034-
QCOMPARE(int(err), int(CborErrorNestingTooDeep));
2040+
QCOMPARE(err, CborErrorNestingTooDeep);
20352041
}
20362042
}
20372043

0 commit comments

Comments
 (0)