]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/complaints.c
[gdb/testsuite] Reimplement gdb.gdb/complaints.exp as unittest
[thirdparty/binutils-gdb.git] / gdb / complaints.c
index fecbcd74ce5dc2300efcdf903a224df3b1b02d60..525a3a7eacf4bd1cb020863664f573cdbf4b9d0c 100644 (file)
@@ -21,6 +21,7 @@
 #include "complaints.h"
 #include "command.h"
 #include "gdbcmd.h"
+#include "gdbsupport/selftest.h"
 #include <unordered_map>
 
 /* Map format strings to counters.  */
@@ -74,6 +75,53 @@ complaints_show_value (struct ui_file *file, int from_tty,
                    value);
 }
 
+#if GDB_SELF_TEST
+namespace selftests {
+
+/* Entry point for complaints unit tests.  */
+
+static void
+test_complaints ()
+{
+  std::unordered_map<const char *, int> tmp;
+  scoped_restore reset_counters = make_scoped_restore (&counters, tmp);
+  scoped_restore reset_stop_whining = make_scoped_restore (&stop_whining, 2);
+
+#define CHECK_COMPLAINT(STR, CNT)                                      \
+  do                                                                   \
+    {                                                                  \
+      std::string output;                                              \
+      output = execute_fn_to_string ([]() { complaint (STR); }, false);        \
+      std::string expected                                             \
+       = _("During symbol reading: ") + std::string (STR "\n");        \
+      SELF_CHECK (output == expected);                                 \
+      SELF_CHECK (counters[STR] == CNT);                               \
+    } while (0)
+
+#define CHECK_COMPLAINT_SILENT(STR, CNT)                               \
+  do                                                                   \
+    {                                                                  \
+      std::string output;                                              \
+      output = execute_fn_to_string ([]() { complaint (STR); }, false);        \
+      SELF_CHECK (output.empty ());                                    \
+      SELF_CHECK (counters[STR] == CNT);                               \
+    } while (0)
+
+  CHECK_COMPLAINT ("maintenance complaint 0", 1);
+  CHECK_COMPLAINT ("maintenance complaint 0", 2);
+  CHECK_COMPLAINT_SILENT ("maintenance complaint 0", 3);
+  CHECK_COMPLAINT ("maintenance complaint 1", 1);
+  clear_complaints ();
+  CHECK_COMPLAINT ("maintenance complaint 0", 1);
+
+#undef CHECK_COMPLAINT
+#undef CHECK_COMPLAINT_SILENT
+}
+
+
+} // namespace selftests
+#endif /* GDB_SELF_TEST */
+
 void _initialize_complaints ();
 void
 _initialize_complaints ()
@@ -84,4 +132,8 @@ Set max number of complaints about incorrect symbols."), _("\
 Show max number of complaints about incorrect symbols."), NULL,
                            NULL, complaints_show_value,
                            &setlist, &showlist);
+
+#if GDB_SELF_TEST
+  selftests::register_test ("complaints", selftests::test_complaints);
+#endif /* GDB_SELF_TEST */
 }