Skip to contentSkip to conversation

qdev-monitor-test: simplify using g_assert_cmpstr()

PATCH In review Stefan Hajnoczi 3 participants 5 messages Dec 18, 2013 – Dec 19, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qdev-monitor-test: simplify using g_assert_cmpstr()

Use g_assert_cmpstr() instead of combining g_assert() and strcmp(3). This simplifies the code since we no longer have to play games to distinguish NULL from "" using "(null)".

gcc extension haters will also be happy that ?: was dropped.

---
 tests/qdev-monitor-test.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tests/qdev-monitor-test.c +5 -5
index ba7f9cc..eefaab8 100644
@@ -32,8 +32,9 @@ static void test_device_add(void)
                    "}}");
     g_assert(response);
     error = qdict_get_qdict(response, "error");
-    g_assert(!strcmp(qdict_get_try_str(error, "desc") ?: "",
-                     "Device needs media, but drive is empty"));
+    g_assert_cmpstr(qdict_get_try_str(error, "desc"),
+                    ==,
+                    "Device needs media, but drive is empty");
     QDECREF(response);
 
     /* Delete the drive */
@@ -42,7 +43,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_del drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "(null)", ""));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "");
     QDECREF(response);
 
     /* Try to re-add the drive.  This fails with duplicate IDs if a leaked
@@ -53,8 +54,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_add pci-addr=auto if=none,id=drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "",
-                     "OK\r\n"));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n");
     QDECREF(response);
 
     qtest_end();
Signature · 1 line
1.8.4.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Stefan Hajnoczi

Use g_assert_cmpstr() instead of combining g_assert() and strcmp(3). This simplifies the code since we no longer have to play games to distinguish NULL from "" using "(null)".

gcc extension haters will also be happy that ?: was dropped.

---
 tests/qdev-monitor-test.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tests/qdev-monitor-test.c +0 -0
index ba7f9cc..eefaab8 100644
@@ -32,8 +32,9 @@ static void test_device_add(void)
                    "}}");

tests/qdev-monitor-test.c static void test_device_add(void) · line 40

Quoted 10 lines
tests/qdev-monitor-test.c +3 -2
     g_assert(response);
     error = qdict_get_qdict(response, "error");
-    g_assert(!strcmp(qdict_get_try_str(error, "desc") ?: "",
-                     "Device needs media, but drive is empty"));
+    g_assert_cmpstr(qdict_get_try_str(error, "desc"),
+                    ==,
+                    "Device needs media, but drive is empty");
     QDECREF(response);
 
     /* Delete the drive */

Outside this patch's scope, but here goes anyway: why do we test the value of member desc? Isn't that awfully fragile?

It broke once already, in Andreas's commit 75884af "virtio-blk: Convert to QOM realize". Andreas, do you remember why you tossed the class == GenericError check instead of the desc check?

Quoted 10 lines
tests/qdev-monitor-test.c +1 -1
@@ -42,7 +43,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_del drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "(null)", ""));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "");
     QDECREF(response);
 
     /* Try to re-add the drive.  This fails with duplicate IDs if a leaked

tests/qdev-monitor-test.c static void test_device_add(void) · line 60

Quoted 9 lines
tests/qdev-monitor-test.c +1 -2
@@ -53,8 +54,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_add pci-addr=auto if=none,id=drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "",
-                     "OK\r\n"));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n");
     QDECREF(response);
 
     qtest_end();
#2 lore raw

Am 18.12.2013 18:11, schrieb Markus Armbruster:

Quoted 29 lines
Quoted 27 lines · Stefan Hajnoczi

Use g_assert_cmpstr() instead of combining g_assert() and strcmp(3). This simplifies the code since we no longer have to play games to distinguish NULL from "" using "(null)".

gcc extension haters will also be happy that ?: was dropped.

---
 tests/qdev-monitor-test.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tests/qdev-monitor-test.c +3 -2
index ba7f9cc..eefaab8 100644
@@ -32,8 +32,9 @@ static void test_device_add(void)
                    "}}");
     g_assert(response);
     error = qdict_get_qdict(response, "error");
-    g_assert(!strcmp(qdict_get_try_str(error, "desc") ?: "",
-                     "Device needs media, but drive is empty"));
+    g_assert_cmpstr(qdict_get_try_str(error, "desc"),
+                    ==,
+                    "Device needs media, but drive is empty");
     QDECREF(response);
 
Quoted 8 lines

tests/qdev-monitor-test.c static void test_device_add(void) · line 40

Quoted 1 line
tests/qdev-monitor-test.c +0 -0
     /* Delete the drive */

Outside this patch's scope, but here goes anyway: why do we test the value of member desc? Isn't that awfully fragile?

It broke once already, in Andreas's commit 75884af "virtio-blk: Convert to QOM realize". Andreas, do you remember why you tossed the class == GenericError check instead of the desc check?

/me points to bonzini, who rebased it on my behalf. :)

I do agree that testing error descriptions is fragile in that people may not think of running make check on a trivial textual change, e.g. typo fix.

Andreas

Quoted 23 lines
Quoted 11 lines
tests/qdev-monitor-test.c +1 -1
@@ -42,7 +43,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_del drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "(null)", ""));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "");
     QDECREF(response);
 
     /* Try to re-add the drive.  This fails with duplicate IDs if a leaked

tests/qdev-monitor-test.c static void test_device_add(void) · line 60

Quoted 9 lines
tests/qdev-monitor-test.c +1 -2
@@ -53,8 +54,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_add pci-addr=auto if=none,id=drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "",
-                     "OK\r\n"));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n");
     QDECREF(response);
 
     qtest_end();
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#3 lore raw
In reply to Stefan Hajnoczi

Am 18.12.2013 17:34, schrieb Stefan Hajnoczi:

Quoted 1 line

Use g_assert_cmpstr() instead of combining g_assert() and strcmp(3).

I vaguely remember that some such handy function was introduced only after the minimum GLib version we require. Did you check on that? But IIRC we already have higher requirements for qtest that for the non-check code so it might not really matter...

Andreas

Quoted 47 lines

This simplifies the code since we no longer have to play games to distinguish NULL from "" using "(null)".

gcc extension haters will also be happy that ?: was dropped.

---
 tests/qdev-monitor-test.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tests/qdev-monitor-test.c +5 -5
index ba7f9cc..eefaab8 100644
@@ -32,8 +32,9 @@ static void test_device_add(void)
                    "}}");
     g_assert(response);
     error = qdict_get_qdict(response, "error");
-    g_assert(!strcmp(qdict_get_try_str(error, "desc") ?: "",
-                     "Device needs media, but drive is empty"));
+    g_assert_cmpstr(qdict_get_try_str(error, "desc"),
+                    ==,
+                    "Device needs media, but drive is empty");
     QDECREF(response);
 
     /* Delete the drive */
@@ -42,7 +43,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_del drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "(null)", ""));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "");
     QDECREF(response);
 
     /* Try to re-add the drive.  This fails with duplicate IDs if a leaked
@@ -53,8 +54,7 @@ static void test_device_add(void)
                    "   \"command-line\": \"drive_add pci-addr=auto if=none,id=drive0\""
                    "}}");
     g_assert(response);
-    g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "",
-                     "OK\r\n"));
+    g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n");
     QDECREF(response);
 
     qtest_end();
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw
Quoted 7 lines · Andreas Färber

Am 18.12.2013 17:34, schrieb Stefan Hajnoczi:

Quoted 1 line

Use g_assert_cmpstr() instead of combining g_assert() and strcmp(3).

I vaguely remember that some such handy function was introduced only after the minimum GLib version we require. Did you check on that? But IIRC we already have higher requirements for qtest that for the non-check code so it might not really matter...

Yes, before using it I did git grep g_assert_cmpstr. It's already used in many other test cases like tests/check-qjson.c.

Stefan