[gjs: 3/18] jsapi-util: Add copy function to GjsAutoStrv (and test it)




commit d251072b5fca199ac019f69d7de662d30f338587
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Sep 29 21:12:28 2020 +0200

    jsapi-util: Add copy function to GjsAutoStrv (and test it)

 gjs/jsapi-util.h              |  2 +-
 test/gjs-test-jsapi-utils.cpp | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
---
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 0f342283..eee80fcf 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -151,7 +151,7 @@ struct GjsAutoCharFuncs {
 using GjsAutoChar =
     GjsAutoPointer<char, char, GjsAutoCharFuncs::free, GjsAutoCharFuncs::dup>;
 
-using GjsAutoStrv = GjsAutoPointer<char*, char*, g_strfreev>;
+using GjsAutoStrv = GjsAutoPointer<char*, char*, g_strfreev, g_strdupv>;
 
 template <typename T>
 using GjsAutoUnref = GjsAutoPointer<T, void, g_object_unref, g_object_ref>;
diff --git a/test/gjs-test-jsapi-utils.cpp b/test/gjs-test-jsapi-utils.cpp
index 3ba49a15..5489ffa7 100644
--- a/test/gjs-test-jsapi-utils.cpp
+++ b/test/gjs-test-jsapi-utils.cpp
@@ -433,6 +433,27 @@ static void test_gjs_autostrv_init() {
         g_assert_cmpstr(autoptr[i], ==, strv[i]);
 }
 
+static void test_gjs_autostrv_init_take_ownership() {
+    const char* strv[] = {"FOO", "Bar", "BAZ", nullptr};
+    GjsAutoStrv autoptr(const_cast<char* const*>(strv), GjsAutoTakeOwnership());
+
+    for (int i = g_strv_length(const_cast<char**>(strv)); i >= 0; i--)
+        g_assert_cmpstr(autoptr[i], ==, strv[i]);
+    g_assert(autoptr != strv);
+}
+
+static void test_gjs_autostrv_copy() {
+    const char* strv[] = {"FOO", "Bar", "BAZ", nullptr};
+    GjsAutoStrv autoptr = g_strdupv(const_cast<char**>(strv));
+
+    char** copy = autoptr.copy();
+    for (int i = g_strv_length(const_cast<char**>(strv)); i >= 0; i--)
+        g_assert_cmpstr(copy[i], ==, strv[i]);
+    g_assert(autoptr != copy);
+
+    g_strfreev(copy);
+}
+
 static void test_gjs_autotypeclass_init() {
     GjsAutoTypeClass<GObjectClass> autoclass(gjs_test_object_get_type());
 
@@ -512,6 +533,10 @@ void gjs_test_add_tests_for_jsapi_utils(void) {
 
     g_test_add_func("/gjs/jsapi-utils/gjs-autostrv/init",
                     test_gjs_autostrv_init);
+    g_test_add_func("/gjs/jsapi-utils/gjs-autostrv/init/take_ownership",
+                    test_gjs_autostrv_init_take_ownership);
+    g_test_add_func("/gjs/jsapi-utils/gjs-autostrv/copy",
+                    test_gjs_autostrv_copy);
 
     g_test_add_func("/gjs/jsapi-utils/gjs-autotypeclass/init",
                     test_gjs_autotypeclass_init);


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