Skip to content

Commit bbf4d95

Browse files
committed
introduce ASSERT_OR_PRINT test macro
1 parent 2bea375 commit bbf4d95

File tree

6 files changed

+45
-50
lines changed

6 files changed

+45
-50
lines changed

tests/TestSuite.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ extern "C" {
3838
#define ASSERT assert
3939

4040

41+
#ifdef ASSERT_OR_PRINT
42+
# undef ASSERT_OR_PRINT
43+
#endif
44+
#define ASSERT_OR_PRINT(_statement, _err) \
45+
do { \
46+
if (!_statement) { \
47+
fprintf(stderr, "FAIL:%s:%d %s()\n %s\n %s\n\n", \
48+
__FILE__, __LINE__, __FUNCTION__, \
49+
#_statement, _err.message); \
50+
fflush(stderr); \
51+
abort(); \
52+
} \
53+
} while (0)
54+
55+
4156
#ifdef ASSERT_CMPINT
4257
# undef ASSERT_CMPINT
4358
#endif

tests/test-libmongoc.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,23 +497,17 @@ call_ismaster (bson_t *reply)
497497
mongoc_uri_t *uri;
498498
mongoc_client_t *client;
499499
bson_error_t error;
500-
bool r;
501500

502501
uri_str = test_framework_get_uri_str_from_env ();
503502
uri = mongoc_uri_new (uri_str);
504503
assert (uri);
505504
mongoc_uri_set_option_as_int32 (uri, "serverSelectionTimeoutMS", 1000);
506505

507506
client = mongoc_client_new_from_uri (uri);
508-
r = mongoc_client_command_simple (client, "admin",
509-
tmp_bson ("{'isMaster': 1}"),
510-
NULL, reply, &error);
511-
512-
if (!r) {
513-
printf ("ismaster error: %s\n", error.message);
514-
}
515-
516-
assert (r);
507+
ASSERT_OR_PRINT (mongoc_client_command_simple (client, "admin",
508+
tmp_bson ("{'isMaster': 1}"),
509+
NULL, reply, &error),
510+
error);
517511

518512
mongoc_client_destroy (client);
519513
mongoc_uri_destroy (uri);

tests/test-mongoc-client.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ test_mongoc_client_authenticate (void *context)
9090
bson_init (&roles);
9191
BCON_APPEND (&roles,
9292
"0", "{", "role", "read", "db", "test", "}");
93-
r = mongoc_database_add_user(database, username, "testpass", &roles, NULL, &error);
94-
ASSERT_CMPINT(r, ==, 1);
93+
94+
ASSERT_OR_PRINT (mongoc_database_add_user(database, username, "testpass",
95+
&roles, NULL, &error),
96+
error);
97+
9598
mongoc_database_destroy(database);
9699

97100
/*
@@ -110,7 +113,7 @@ test_mongoc_client_authenticate (void *context)
110113
if (!r) {
111114
r = mongoc_cursor_error(cursor, &error);
112115
if (r) {
113-
MONGOC_ERROR("Authentication failure: \"%s\"", error.message);
116+
printf("Authentication failure: \"%s\"", error.message);
114117
}
115118
assert(!r);
116119
}
@@ -704,14 +707,11 @@ test_exhaust_cursor (void *context)
704707
}
705708

706709
BEGIN_IGNORE_DEPRECATIONS;
707-
r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE,
708-
(const bson_t **)bptr, 10, wr, &error);
710+
ASSERT_OR_PRINT (mongoc_collection_insert_bulk (
711+
collection, MONGOC_INSERT_NONE,
712+
(const bson_t **)bptr, 10, wr, &error),
713+
error);
709714
END_IGNORE_DEPRECATIONS;
710-
711-
if (!r) {
712-
printf("Insert bulk failure: %s\n", error.message);
713-
}
714-
assert(r);
715715
}
716716

717717
/* create a couple of cursors */

tests/test-mongoc-database.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ test_has_collection (void)
3434
bson_oid_init (&oid, NULL);
3535
bson_append_oid (&b, "_id", 3, &oid);
3636
bson_append_utf8 (&b, "hello", 5, "world", 5);
37-
r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &b, NULL,
38-
&error);
39-
if (!r) {
40-
MONGOC_WARNING ("%s\n", error.message);
41-
}
42-
assert (r);
37+
ASSERT_OR_PRINT (mongoc_collection_insert (collection, MONGOC_INSERT_NONE,
38+
&b, NULL, &error),
39+
error);
4340
bson_destroy (&b);
4441

4542
r = mongoc_database_has_collection (database, name, &error);
@@ -114,7 +111,6 @@ test_drop (void)
114111
mongoc_client_t *client;
115112
bson_error_t error = { 0 };
116113
char *dbname;
117-
bool r;
118114

119115
client = test_framework_client_new ();
120116
assert (client);
@@ -123,8 +119,7 @@ test_drop (void)
123119
database = mongoc_client_get_database (client, dbname);
124120
bson_free (dbname);
125121

126-
r = mongoc_database_drop (database, &error);
127-
assert (r);
122+
ASSERT_OR_PRINT (mongoc_database_drop (database, &error), error);
128123
assert (!error.domain);
129124
assert (!error.code);
130125

tests/test-mongoc-gridfs.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ test_create (void)
4545
client = test_framework_client_new ();
4646
assert (client);
4747

48-
gridfs = mongoc_client_get_gridfs (client, "test", "foo", &error);
49-
if (!gridfs) {
50-
printf ("mongoc_client_get_gridfs err: %s\n", error.message);
51-
}
52-
53-
assert (gridfs);
48+
ASSERT_OR_PRINT (
49+
(gridfs = mongoc_client_get_gridfs (client, "test", "foo", &error)),
50+
error);
5451

5552
mongoc_gridfs_drop (gridfs, &error);
5653

@@ -75,7 +72,6 @@ test_remove (void)
7572
mongoc_gridfs_file_opt_t opts = { 0 };
7673
mongoc_client_t *client;
7774
bson_error_t error;
78-
bool r;
7975
char name[32];
8076

8177
client = test_framework_client_new ();
@@ -94,9 +90,7 @@ test_remove (void)
9490
assert (file);
9591
assert (mongoc_gridfs_file_save (file));
9692

97-
r = mongoc_gridfs_file_remove (file, &error);
98-
if (!r) fprintf (stderr, "%s\n", error.message);
99-
assert (r);
93+
ASSERT_OR_PRINT (mongoc_gridfs_file_remove (file, &error), error);
10094

10195
mongoc_gridfs_file_destroy (file);
10296

@@ -251,8 +245,8 @@ test_create_from_stream (void)
251245
client = test_framework_client_new ();
252246
assert (client);
253247

254-
gridfs = get_test_gridfs (client, "from_stream", &error);
255-
assert (gridfs);
248+
ASSERT_OR_PRINT ((gridfs = get_test_gridfs (client, "from_stream", &error)),
249+
error);
256250

257251
mongoc_gridfs_drop (gridfs, &error);
258252

@@ -440,7 +434,6 @@ test_remove_by_filename (void)
440434
mongoc_gridfs_file_opt_t opt = { 0 };
441435
mongoc_client_t *client;
442436
bson_error_t error;
443-
bool ret;
444437

445438
client = test_framework_client_new ();
446439
assert (client);
@@ -461,9 +454,9 @@ test_remove_by_filename (void)
461454
assert (file);
462455
assert (mongoc_gridfs_file_save (file));
463456

464-
ret = mongoc_gridfs_remove_by_filename (gridfs, "foo_file_1.txt", &error);
465-
if (!ret) fprintf (stderr, "ERROR: %s\n", error.message);
466-
assert (ret);
457+
ASSERT_OR_PRINT (
458+
mongoc_gridfs_remove_by_filename (gridfs, "foo_file_1.txt", &error),
459+
error);
467460
mongoc_gridfs_file_destroy (file);
468461

469462
file = mongoc_gridfs_find_one_by_filename (gridfs, "foo_file_1.txt", &error);

tests/test-write-commands.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,14 @@ test_split_insert (void)
6464
_mongoc_write_command_execute (&command, client, 0, collection->db,
6565
collection->collection, NULL, 0, &result);
6666

67-
r = _mongoc_write_result_complete (&result, &reply, &error);
68-
69-
assert (r);
67+
ASSERT_OR_PRINT (_mongoc_write_result_complete (&result, &reply, &error),
68+
error);
7069
assert (result.nInserted == 3000);
7170

7271
_mongoc_write_command_destroy (&command);
7372
_mongoc_write_result_destroy (&result);
7473

75-
r = mongoc_collection_drop (collection, &error);
76-
assert (r);
74+
ASSERT_OR_PRINT (mongoc_collection_drop (collection, &error), error);
7775

7876
for (i = 0; i < 3000; i++) {
7977
bson_destroy (docs [i]);

0 commit comments

Comments
 (0)