[gnome-builder/wip/chergert/git-oop: 27/34] add test, use variants to roundtrip



commit 1a7c31855cda7a4e02b322f88fa7dd8534029a5f
Author: Christian Hergert <chergert redhat com>
Date:   Thu Mar 21 18:13:33 2019 -0700

    add test, use variants to roundtrip

 src/plugins/git/line-cache.c      | 55 +++++++++++++++++++++++++++++----------
 src/plugins/git/line-cache.h      | 25 +++++++++++-------
 src/plugins/git/meson.build       |  9 +++++++
 src/plugins/git/test-line-cache.c | 54 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 119 insertions(+), 24 deletions(-)
---
diff --git a/src/plugins/git/line-cache.c b/src/plugins/git/line-cache.c
index 62df8ead1..bc9e8b9e2 100644
--- a/src/plugins/git/line-cache.c
+++ b/src/plugins/git/line-cache.c
@@ -2,20 +2,20 @@
  *
  * Copyright 2019 Christian Hergert <chergert redhat com>
  *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * SPDX-License-Identifier: GPL-3.0-or-later
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #define G_LOG_DOMAIN "line-cache"
@@ -26,11 +26,6 @@
 
 #include "line-cache.h"
 
-struct _LineCache
-{
-  GArray *lines;
-};
-
 LineCache *
 line_cache_new (void)
 {
@@ -202,3 +197,35 @@ line_cache_foreach_in_range (LineCache *self,
         callback ((gpointer)entry, user_data);
     }
 }
+
+GVariant *
+line_cache_to_variant (LineCache *self)
+{
+  g_return_val_if_fail (self != NULL, NULL);
+
+  return g_variant_new_fixed_array (G_VARIANT_TYPE ("u"),
+                                    (gconstpointer)self->lines->data,
+                                    self->lines->len,
+                                    sizeof (LineEntry));
+}
+
+LineCache *
+line_cache_new_from_variant (GVariant *variant)
+{
+  LineCache *self;
+
+  self = line_cache_new ();
+
+  if (variant != NULL)
+    {
+      gconstpointer base;
+      gsize n_elements = 0;
+
+      base = g_variant_get_fixed_array (variant, &n_elements, sizeof (LineEntry));
+
+      if (n_elements > 0 && n_elements < G_MAXINT)
+        g_array_append_vals (self->lines, base, n_elements);
+    }
+
+  return g_steal_pointer (&self);
+}
diff --git a/src/plugins/git/line-cache.h b/src/plugins/git/line-cache.h
index aad874817..bc58d77a9 100644
--- a/src/plugins/git/line-cache.h
+++ b/src/plugins/git/line-cache.h
@@ -2,20 +2,20 @@
  *
  * Copyright 2019 Christian Hergert <chergert redhat com>
  *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * SPDX-License-Identifier: GPL-3.0-or-later
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #pragma once
@@ -24,7 +24,10 @@
 
 G_BEGIN_DECLS
 
-typedef struct _LineCache LineCache;
+typedef struct
+{
+  GArray *lines;
+} LineCache;
 
 typedef enum
 {
@@ -41,6 +44,7 @@ typedef struct
 } LineEntry;
 
 LineCache *line_cache_new              (void);
+LineCache *line_cache_new_from_variant (GVariant  *variant);
 void       line_cache_free             (LineCache *self);
 void       line_cache_mark_range       (LineCache *self,
                                         gint       start_line,
@@ -53,5 +57,6 @@ void       line_cache_foreach_in_range (LineCache *self,
                                         gpointer   user_data);
 LineMark   line_cache_get_mark         (LineCache *self,
                                         gint       line);
+GVariant  *line_cache_to_variant       (LineCache *self);
 
 G_END_DECLS
diff --git a/src/plugins/git/meson.build b/src/plugins/git/meson.build
index 6fcf0358d..f40c5bc12 100644
--- a/src/plugins/git/meson.build
+++ b/src/plugins/git/meson.build
@@ -50,4 +50,13 @@ executable('gnome-builder-git', gnome_builder_git_sources,
      install_rpath: pkglibdir_abs,
 )
 
+test_line_cache = executable('test-line-cache', ['test-line-cache.c', 'line-cache.c'],
+      dependencies: [libgio_dep],
+           gui_app: false,
+           install: false,
+            c_args: exe_c_args,
+         link_args: exe_link_args,
+)
+test('test-line-cache', test_line_cache)
+
 endif
diff --git a/src/plugins/git/test-line-cache.c b/src/plugins/git/test-line-cache.c
new file mode 100644
index 000000000..99fc6a760
--- /dev/null
+++ b/src/plugins/git/test-line-cache.c
@@ -0,0 +1,54 @@
+/* line-cache.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "line-cache.h"
+
+static void
+test_basic (void)
+{
+  LineCache *lc1 = line_cache_new ();
+  LineCache *lc2;
+  GVariant *v;
+
+  line_cache_mark_range (lc1, 0, 10, LINE_MARK_ADDED);
+  line_cache_mark_range (lc1, 100, 200, LINE_MARK_REMOVED);
+
+  v = line_cache_to_variant (lc1);
+  g_assert_nonnull (v);
+
+  lc2 = line_cache_new_from_variant (v);
+  g_assert_nonnull (lc2);
+
+  g_assert_cmpint (lc1->lines->len, ==, 110);
+  g_assert_cmpint (lc2->lines->len, ==, 110);
+
+  g_assert_true (0 == memcmp (lc1->lines->data,
+                              lc2->lines->data,
+                              sizeof (LineEntry) * lc1->lines->len));
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/Plugins/Git/LineCache/basic", test_basic);
+  return g_test_run ();
+}


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