[glibmm] tests/glibmm_ustring_compare: Fix so it works for C++20



commit 8c2bc60ceb48e3a64bf83920b999801895052f61
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sun Apr 11 16:20:21 2021 +0200

    tests/glibmm_ustring_compare: Fix so it works for C++20
    
    Make 3 tests, where the 2 new tests check that the disallowed comparisons
    between std::string and Glib::ustring don't compile.
    The new tests are added only in Meson builds (ninja test).
    See MR !50

 tests/glibmm_ustring_compare/main.cc | 43 +++++++++++++-----------------------
 tests/meson.build                    | 24 ++++++++++++++++++++
 2 files changed, 39 insertions(+), 28 deletions(-)
---
diff --git a/tests/glibmm_ustring_compare/main.cc b/tests/glibmm_ustring_compare/main.cc
index 349a2741..ccbbd280 100644
--- a/tests/glibmm_ustring_compare/main.cc
+++ b/tests/glibmm_ustring_compare/main.cc
@@ -1,26 +1,13 @@
 #include <glibmm.h>
-
 #include <iostream>
 
-// Helper class to check for non-existing overload
-template<typename T>
-struct Convertible
-{
-  Convertible(const T&){};
-};
-
-static bool expect_missing_overload = false;
-
-void
-operator==(Convertible<std::string> const&, Glib::ustring const&)
-{
-  g_assert_true(expect_missing_overload);
-  expect_missing_overload = false;
-}
-
 int
 main(int, char**)
 {
+  const char *cstr1 = "Hello";
+  Glib::ustring ustr1 = cstr1;
+
+#ifndef GLIBMM_TEST_THAT_COMPILATION_FAILS
   // allocating
   static_assert(std::is_convertible<const char*, Glib::ustring>::value, "");
   // non-allocating
@@ -30,12 +17,10 @@ main(int, char**)
   static_assert(!std::is_convertible<Glib::UStringView, Glib::ustring>::value, "");
   static_assert(!std::is_convertible<Glib::UStringView, const char *>::value, "");
 
-  const char *cstr1 = "Hello";
   const char *cstr2 = "World";
   const char *cstr12 = "HelloWorld";
   const char *cstr12_25 = "lloWo"; // cstr12[2:2 + 5]
 
-  Glib::ustring ustr1 = cstr1;
   Glib::ustring ustr2 = cstr2;
   Glib::ustring ustr12 = cstr12;
   Glib::ustring ustr12_25 = cstr12_25;
@@ -98,17 +83,19 @@ main(int, char**)
   static_assert(!std::is_convertible<std::string, Glib::UStringView>::value, "");
   static_assert(!std::is_convertible<Glib::UStringView, std::string>::value, "");
 
-  std::string sstr1 = cstr1;
+#else // GLIBMM_TEST_THAT_COMPILATION_FAILS
+
+  // By design some combinations of std::string and Glib::ustring are not allowed.
+  // Copied from ustring.h: Using the wrong string class shall not be as easy as
+  // using the right string class.
 
-  // Would not compile without the helper overload
-  expect_missing_overload = true;
-  sstr1 == ustr1;
-  g_assert_false(expect_missing_overload);
+  std::string sstr1 = cstr1;
 
-  // Doesn't compile because of missing Glib::ustring::compare overload (expected), but
-  // unfortunately not testable like the other way round.
-#if 0
-  ustr1 == sstr1;
+#if GLIBMM_TEST_THAT_COMPILATION_FAILS == 1
+  sstr1 == ustr1; // Shall not compile
+#else
+  ustr1 == sstr1; // Shall not compile
+#endif
 #endif
 
   return EXIT_SUCCESS;
diff --git a/tests/meson.build b/tests/meson.build
index dabeb775..653ed878 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -41,6 +41,7 @@ test_programs = [
 ]
 
 thread_dep = dependency('threads')
+meson_backend = find_program(meson.backend(), required: true)
 
 foreach ex : test_programs
   dir = ''
@@ -66,4 +67,27 @@ foreach ex : test_programs
   )
 
   test(ex_name, exe_file)
+
+  if ex[0][0] == 'glibmm_ustring_compare'
+
+    # Tests that shall fail to compile.
+    foreach suffix : ['1', '2']
+      ex_name_s = ex_name + suffix
+      exe_file = executable(ex_name_s, ex_sources,
+        cpp_args: ['-DGLIBMM_DISABLE_DEPRECATED', '-DGIOMM_DISABLE_DEPRECATED',
+                   '-DGLIBMM_TEST_THAT_COMPILATION_FAILS=' + suffix],
+        dependencies: mm_dep,
+        implicit_include_directories: false,
+        gui_app: false,
+        build_by_default: false,
+        install: false,
+      )
+
+      target_name = 'tests' / ex_name_s
+      test(ex_name_s, meson_backend,
+        args: target_name,
+        should_fail: true,
+      )
+    endforeach
+  endif
 endforeach


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]