aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@lanedo.com>2012-02-11 19:08:39 +0100
committerAleksander Morgado <aleksander@lanedo.com>2012-03-15 14:15:04 +0100
commitb7938448ea125827a7e54f11d6b548d26653cc37 (patch)
tree158944e06060fa475633a03ba2e06e52977a192c
parent7df5874a88c6fc09ad62f655f4da9c0626c224eb (diff)
libmm-common,tests: new tests for the key/value parser
-rw-r--r--.gitignore1
-rw-r--r--configure.ac1
-rw-r--r--libmm-common/Makefile.am1
-rw-r--r--libmm-common/tests/Makefile.am20
-rw-r--r--libmm-common/tests/test-common-helpers.c277
5 files changed, 300 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index d237b86e..2fcede17 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,6 +54,7 @@ libmm-common/mm-gdbus-*.[ch]
libmm-common/mm-enums-types.[ch]
libmm-common/mm-errors-types.[ch]
libmm-common/*.xml
+libmm-common/tests/test-common-helpers
po/Makefile.in.in
po/POTFILES
diff --git a/configure.ac b/configure.ac
index d600a89a..b2fd7ab2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -196,6 +196,7 @@ data/Makefile
include/Makefile
build-aux/Makefile
libmm-common/Makefile
+libmm-common/tests/Makefile
libqcdm/Makefile
libqcdm/src/Makefile
libqcdm/tests/Makefile
diff --git a/libmm-common/Makefile.am b/libmm-common/Makefile.am
index 3cbd237c..e9a87cd7 100644
--- a/libmm-common/Makefile.am
+++ b/libmm-common/Makefile.am
@@ -1,3 +1,4 @@
+SUBDIRS = . tests
lib_LTLIBRARIES = libmm-common.la
diff --git a/libmm-common/tests/Makefile.am b/libmm-common/tests/Makefile.am
new file mode 100644
index 00000000..296c4147
--- /dev/null
+++ b/libmm-common/tests/Makefile.am
@@ -0,0 +1,20 @@
+
+noinst_PROGRAMS = \
+ test-common-helpers
+
+test_common_helpers_SOURCES = \
+ test-common-helpers.c
+
+test_common_helpers_CPPFLAGS = \
+ $(MM_CFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/include \
+ -I$(top_builddir)/include \
+ -I$(top_srcdir)/libmm-common \
+ -I$(top_builddir)/libmm-common
+
+test_common_helpers_LDADD = \
+ $(top_builddir)/libmm-common/libmm-common.la \
+ $(MM_LIBS)
+
+TEST_PROGS = test-common-helpers
diff --git a/libmm-common/tests/test-common-helpers.c b/libmm-common/tests/test-common-helpers.c
new file mode 100644
index 00000000..8d158ca8
--- /dev/null
+++ b/libmm-common/tests/test-common-helpers.c
@@ -0,0 +1,277 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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:
+ *
+ * Copyright (C) 2012 Google, Inc.
+ */
+
+#include <glib.h>
+
+#include <libmm-common.h>
+
+/********************* KEY VALUE PARSER TESTS *********************/
+
+typedef struct {
+ const gchar *key;
+ const gchar *value;
+} KeyValueEntry;
+
+/* ---- Expected cases ---- */
+
+typedef struct {
+ const KeyValueEntry *entries;
+ guint n_entries;
+ guint i;
+} CommonKeyValueTestContext;
+
+static gboolean
+common_key_value_test_foreach (const gchar *key,
+ const gchar *value,
+ CommonKeyValueTestContext *ctx)
+{
+ g_assert_cmpuint (ctx->i, <, ctx->n_entries);
+
+ g_assert_cmpstr (key, ==, ctx->entries[ctx->i].key);
+ g_assert_cmpstr (value, ==, ctx->entries[ctx->i].value);
+ ctx->i++;
+
+ return TRUE;
+}
+
+static void
+common_key_value_test (const gchar *str,
+ const KeyValueEntry *entries,
+ guint n_entries)
+{
+ GError *error = NULL;
+ CommonKeyValueTestContext ctx;
+
+ ctx.entries = entries;
+ ctx.n_entries = n_entries;
+ ctx.i = 0;
+
+ mm_common_parse_key_value_string (str,
+ &error,
+ (MMParseKeyValueForeachFn)common_key_value_test_foreach,
+ &ctx);
+ g_assert_no_error (error);
+ g_assert_cmpuint (ctx.i, ==, ctx.n_entries);
+}
+
+static void
+key_value_test_standard (void)
+{
+ const gchar *str =
+ "key1=value1,"
+ "key2=value2,"
+ "key3=value3";
+ const KeyValueEntry entries[] = {
+ { "key1", "value1" },
+ { "key2", "value2" },
+ { "key3", "value3" }
+ };
+
+ common_key_value_test (str, entries, G_N_ELEMENTS (entries));
+}
+
+static void
+key_value_test_spaces (void)
+{
+ const gchar *str =
+ " key1 = value1 , "
+ "\t\tkey2\t=\tvalue2\t,\t"
+ "\n\nkey3\n=\nvalue3\n";
+ const KeyValueEntry entries[] = {
+ { "key1", "value1" },
+ { "key2", "value2" },
+ { "key3", "value3" }
+ };
+
+ common_key_value_test (str, entries, G_N_ELEMENTS (entries));
+}
+
+static void
+key_value_test_double_quotes (void)
+{
+ const gchar *str =
+ "key1=\"this is a string\","
+ "key2=\"and so is this\"";
+ const KeyValueEntry entries[] = {
+ { "key1", "this is a string" },
+ { "key2", "and so is this" }
+ };
+
+ common_key_value_test (str, entries, G_N_ELEMENTS (entries));
+}
+
+static void
+key_value_test_single_quotes (void)
+{
+ const gchar *str =
+ "key1='this is a string',"
+ "key2='and so is this'";
+ const KeyValueEntry entries[] = {
+ { "key1", "this is a string" },
+ { "key2", "and so is this" }
+ };
+
+ common_key_value_test (str, entries, G_N_ELEMENTS (entries));
+}
+
+static void
+key_value_test_empty_value (void)
+{
+ const gchar *str =
+ "key1=,"
+ "key2=\"\"";
+ const KeyValueEntry entries[] = {
+ { "key1", "" },
+ { "key2", "" }
+ };
+
+ common_key_value_test (str, entries, G_N_ELEMENTS (entries));
+}
+
+static void
+key_value_test_empty_string (void)
+{
+ const gchar *str = "";
+ const KeyValueEntry entries[] = { };
+
+ common_key_value_test (str, entries, G_N_ELEMENTS (entries));
+}
+
+/* ---- Unexpected cases ---- */
+
+static gboolean
+common_key_value_error_test_foreach (const gchar *key,
+ const gchar *value,
+ gpointer none)
+{
+ /* no op */
+ return TRUE;
+}
+
+static void
+common_key_value_error_test (const gchar *str)
+{
+ GError *error = NULL;
+
+ mm_common_parse_key_value_string (str,
+ &error,
+ (MMParseKeyValueForeachFn)common_key_value_error_test_foreach,
+ NULL);
+
+ /* We don't really care about the specific error type */
+ g_assert (error != NULL);
+ g_error_free (error);
+}
+
+static void
+key_value_error_test_no_first_key (void)
+{
+ common_key_value_error_test ("=value1");
+}
+
+static void
+key_value_error_test_no_key (void)
+{
+ common_key_value_error_test ("key1=value1,"
+ "=value2");
+}
+
+static void
+key_value_error_test_missing_double_quotes_0 (void)
+{
+ common_key_value_error_test ("key1=\"value1");
+}
+
+static void
+key_value_error_test_missing_double_quotes_1 (void)
+{
+ common_key_value_error_test ("key1=\"value1,"
+ "key2=\"value2\"");
+}
+
+static void
+key_value_error_test_missing_double_quotes_2 (void)
+{
+ common_key_value_error_test ("key1=\"value1\","
+ "key2=\"value2");
+}
+
+static void
+key_value_error_test_missing_single_quotes_0 (void)
+{
+ common_key_value_error_test ("key1='value1");
+}
+
+static void
+key_value_error_test_missing_single_quotes_1 (void)
+{
+ common_key_value_error_test ("key1='value1,"
+ "key2='value2'");
+}
+
+static void
+key_value_error_test_missing_single_quotes_2 (void)
+{
+ common_key_value_error_test ("key1='value1',"
+ "key2='value2");
+}
+
+static void
+key_value_error_test_missing_comma_0 (void)
+{
+ common_key_value_error_test ("key1=value1 "
+ "key2=value2");
+}
+
+static void
+key_value_error_test_missing_comma_1 (void)
+{
+ common_key_value_error_test ("key1=\"value1\" "
+ "key2=\"value2\"");
+}
+
+static void
+key_value_error_test_missing_comma_2 (void)
+{
+ common_key_value_error_test ("key1='value1' "
+ "key2='value2'");
+}
+
+int main (int argc, char **argv)
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/MM/Common/KeyValue/standard", key_value_test_standard);
+ g_test_add_func ("/MM/Common/KeyValue/spaces", key_value_test_spaces);
+ g_test_add_func ("/MM/Common/KeyValue/double-quotes", key_value_test_double_quotes);
+ g_test_add_func ("/MM/Common/KeyValue/single-quotes", key_value_test_single_quotes);
+ g_test_add_func ("/MM/Common/KeyValue/empty-value", key_value_test_empty_value);
+ g_test_add_func ("/MM/Common/KeyValue/empty-string", key_value_test_empty_string);
+
+ g_test_add_func ("/MM/Common/KeyValue/Error/no-first-key", key_value_error_test_no_first_key);
+ g_test_add_func ("/MM/Common/KeyValue/Error/no-key", key_value_error_test_no_key);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-double-quotes-0", key_value_error_test_missing_double_quotes_0);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-double-quotes-1", key_value_error_test_missing_double_quotes_1);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-double-quotes-2", key_value_error_test_missing_double_quotes_2);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-single-quotes-0", key_value_error_test_missing_single_quotes_0);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-single-quotes-1", key_value_error_test_missing_single_quotes_1);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-single-quotes-2", key_value_error_test_missing_single_quotes_2);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-comma-0", key_value_error_test_missing_comma_0);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-comma-1", key_value_error_test_missing_comma_1);
+ g_test_add_func ("/MM/Common/KeyValue/Error/missing-comma-2", key_value_error_test_missing_comma_2);
+
+ return g_test_run ();
+}