[PATCH weston v6 3/4] Converted the config parser test to the new framework.

Jon A. Cruz jonc at osg.samsung.com
Thu Jul 2 23:36:46 PDT 2015


Signed-off-by: Jon A. Cruz <jonc at osg.samsung.com>
---
 Makefile.am                |  10 +-
 tests/config-parser-test.c | 371 +++++++++++++++++++++++++++++++++++++--------
 2 files changed, 320 insertions(+), 61 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 89d9e4c..42f0ed4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1106,7 +1106,15 @@ libtest_runner_la_SOURCES =			\
 libtest_runner_la_CFLAGS = $(AM_CFLAGS) $(COMPOSITOR_CFLAGS)
 
 config_parser_test_SOURCES = tests/config-parser-test.c
-config_parser_test_LDADD = libshared.la libtest-runner.la $(COMPOSITOR_LIBS)
+config_parser_test_LDADD =	\
+	libshared.la		\
+	$(COMPOSITOR_LIBS)	\
+	libzunitc.la		\
+	libzunitcmain.la
+config_parser_test_CFLAGS =			\
+	$(AM_CFLAGS)				\
+	-Wunused \
+	-I$(top_srcdir)/tools/zunitc/inc
 
 vertex_clip_test_SOURCES =			\
 	tests/vertex-clip-test.c		\
diff --git a/tests/config-parser-test.c b/tests/config-parser-test.c
index f659004..1d4e96a 100644
--- a/tests/config-parser-test.c
+++ b/tests/config-parser-test.c
@@ -27,6 +27,7 @@
 
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
@@ -34,28 +35,77 @@
 
 #include "config-parser.h"
 
+#include "shared/helpers.h"
+#include "zunitc/zunitc.h"
+
+struct fixture_data {
+	const char *text;
+	struct weston_config *config;
+};
+
 static struct weston_config *
-run_test(const char *text)
+load_config(const char *text)
 {
-	struct weston_config *config;
+	struct weston_config *config = NULL;
+	int len = 0;
+	int fd = -1;
 	char file[] = "/tmp/weston-config-parser-test-XXXXXX";
-	int fd, len;
+
+	ZUC_ASSERTG_NOT_NULL(text, out);
 
 	fd = mkstemp(file);
+	ZUC_ASSERTG_NE(-1, fd, out);
+
 	len = write(fd, text, strlen(text));
-	assert(len == (int) strlen(text));
+	ZUC_ASSERTG_EQ((int)strlen(text), len, out_close);
 
 	config = weston_config_parse(file);
+
+out_close:
 	close(fd);
 	unlink(file);
+out:
+	return config;
+}
 
+static void *
+setup_test_config(void *data)
+{
+	struct weston_config *config = load_config(data);
+	ZUC_ASSERTG_NOT_NULL(config, out);
+
+out:
 	return config;
 }
 
-static const char t0[] =
-	"# nothing in this file...\n";
+static void *
+setup_test_config_failing(void *data)
+{
+	struct weston_config *config = load_config(data);
+	ZUC_ASSERTG_NULL(config, err_free);
 
-static const char t1[] =
+	return config;
+err_free:
+	weston_config_destroy(config);
+	return NULL;
+}
+
+static void
+cleanup_test_config(void *data)
+{
+	struct weston_config *config = data;
+	ZUC_ASSERT_NOT_NULL(config);
+	weston_config_destroy(config);
+}
+
+static struct zuc_fixture config_test_t0 = {
+	.data = "# nothing in this file...\n",
+	.set_up = setup_test_config,
+	.tear_down = cleanup_test_config
+};
+
+static struct zuc_fixture config_test_t1 = {
+	.data =
 	"# comment line here...\n"
 	"\n"
 	"[foo]\n"
@@ -79,130 +129,331 @@ static const char t1[] =
 	"[bucket]\n"
 	"material=plastic \n"
 	"color=red\n"
-	"contents=sand\n";
+	"contents=sand\n",
+	.set_up = setup_test_config,
+	.tear_down = cleanup_test_config
+};
 
 static const char *section_names[] = {
 	"foo", "bar", "stuff", "bucket", "bucket"
 };
 
-static const char t2[] =
+/*
+ * Since these next few won't parse, we don't add the tear_down to
+ * attempt cleanup.
+ */
+
+static struct zuc_fixture config_test_t2 = {
+	.data =
 	"# invalid section...\n"
-	"[this bracket isn't closed\n";
+	"[this bracket isn't closed\n",
+	.set_up = setup_test_config_failing,
+};
 
-static const char t3[] =
+static struct zuc_fixture config_test_t3 = {
+	.data =
 	"# line without = ...\n"
 	"[bambam]\n"
-	"this line isn't any kind of valid\n";
+	"this line isn't any kind of valid\n",
+	.set_up = setup_test_config_failing,
+};
 
-static const char t4[] =
+static struct zuc_fixture config_test_t4 = {
+	.data =
 	"# starting with = ...\n"
 	"[bambam]\n"
-	"=not valid at all\n";
+	"=not valid at all\n",
+	.set_up = setup_test_config_failing,
+};
 
-int main(int argc, char *argv[])
+ZUC_TEST_F(config_test_t0, comment_only)
 {
-	struct weston_config *config;
-	struct weston_config_section *section;
-	const char *name;
-	char *s;
-	int r, b, i;
-	int32_t n;
-	uint32_t u;
+	struct weston_config *config = data;
+	ZUC_ASSERT_NOT_NULL(config);
+}
 
-	config = run_test(t0);
-	assert(config);
-	weston_config_destroy(config);
+/** @todo individual t1 tests should have more descriptive names. */
+
+ZUC_TEST_F(config_test_t1, test001)
+{
+	struct weston_config_section *section;
+	struct weston_config *config = data;
+	ZUC_ASSERT_NOT_NULL(config);
+	section = weston_config_get_section(config,
+					    "mollusc", NULL, NULL);
+	ZUC_ASSERT_NULL(section);
+}
 
-	config = run_test(t1);
-	assert(config);
-	section = weston_config_get_section(config, "mollusc", NULL, NULL);
-	assert(section == NULL);
+ZUC_TEST_F(config_test_t1, test002)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "foo", NULL, NULL);
 	r = weston_config_section_get_string(section, "a", &s, NULL);
-	assert(r == 0 && strcmp(s, "b") == 0);
+
+	ZUC_ASSERTG_EQ(0, r, out_free);
+	ZUC_ASSERTG_STREQ("b", s, out_free);
+
+out_free:
 	free(s);
+}
+
+ZUC_TEST_F(config_test_t1, test003)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "foo", NULL, NULL);
 	r = weston_config_section_get_string(section, "b", &s, NULL);
-	assert(r == -1 && errno == ENOENT && s == NULL);
+
+	ZUC_ASSERT_EQ(-1, r);
+	ZUC_ASSERT_EQ(ENOENT, errno);
+	ZUC_ASSERT_NULL(s);
+}
+
+ZUC_TEST_F(config_test_t1, test004)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "foo", NULL, NULL);
 	r = weston_config_section_get_string(section, "name", &s, NULL);
-	assert(r == 0 && strcmp(s, "Roy Batty") == 0);
+
+	ZUC_ASSERTG_EQ(0, r, out_free);
+	ZUC_ASSERTG_STREQ("Roy Batty", s, out_free);
+
+out_free:
 	free(s);
+}
+
+ZUC_TEST_F(config_test_t1, test005)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "bar", NULL, NULL);
 	r = weston_config_section_get_string(section, "a", &s, "boo");
-	assert(r == -1 && errno == ENOENT && strcmp(s, "boo") == 0);
+
+	ZUC_ASSERTG_EQ(-1, r, out_free);
+	ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
+	ZUC_ASSERTG_STREQ("boo", s, out_free);
+
+out_free:
 	free(s);
+}
+
+ZUC_TEST_F(config_test_t1, test006)
+{
+	int r;
+	int32_t n;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "bar", NULL, NULL);
 	r = weston_config_section_get_int(section, "number", &n, 600);
-	assert(r == 0 && n == 5252);
+
+	ZUC_ASSERT_EQ(0, r);
+	ZUC_ASSERT_EQ(5252, n);
+}
+
+ZUC_TEST_F(config_test_t1, test007)
+{
+	int r;
+	int32_t n;
+	struct weston_config_section *section;
+	struct weston_config *config = data;;
 
 	section = weston_config_get_section(config, "bar", NULL, NULL);
 	r = weston_config_section_get_int(section, "+++", &n, 700);
-	assert(r == -1 && errno == ENOENT && n == 700);
+
+	ZUC_ASSERT_EQ(-1, r);
+	ZUC_ASSERT_EQ(ENOENT, errno);
+	ZUC_ASSERT_EQ(700, n);
+}
+
+ZUC_TEST_F(config_test_t1, test008)
+{
+	int r;
+	uint32_t u;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "bar", NULL, NULL);
 	r = weston_config_section_get_uint(section, "number", &u, 600);
-	assert(r == 0 && u == 5252);
+	ZUC_ASSERT_EQ(0, r);
+	ZUC_ASSERT_EQ(5252, u);
+}
+
+ZUC_TEST_F(config_test_t1, test009)
+{
+	int r;
+	uint32_t u;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "bar", NULL, NULL);
 	r = weston_config_section_get_uint(section, "+++", &u, 600);
-	assert(r == -1 && errno == ENOENT && u == 600);
+	ZUC_ASSERT_EQ(-1, r);
+	ZUC_ASSERT_EQ(ENOENT, errno);
+	ZUC_ASSERT_EQ(600, u);
+}
+
+ZUC_TEST_F(config_test_t1, test010)
+{
+	int r, b;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "bar", NULL, NULL);
 	r = weston_config_section_get_bool(section, "flag", &b, 600);
-	assert(r == 0 && b == 0);
+	ZUC_ASSERT_EQ(0, r);
+	ZUC_ASSERT_EQ(0, b);
+}
+
+ZUC_TEST_F(config_test_t1, test011)
+{
+	int r, b;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
+
+	section = weston_config_get_section(config, "stuff", NULL, NULL);
+	r = weston_config_section_get_bool(section, "flag", &b, -1);
+	ZUC_ASSERT_EQ(0, r);
+	ZUC_ASSERT_EQ(1, b);
+}
+
+ZUC_TEST_F(config_test_t1, test012)
+{
+	int r, b;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "stuff", NULL, NULL);
 	r = weston_config_section_get_bool(section, "flag", &b, -1);
-	assert(r == 0 && b == 1);
+	ZUC_ASSERT_EQ(0, r);
+	ZUC_ASSERT_EQ(1, b);
+}
+
+ZUC_TEST_F(config_test_t1, test013)
+{
+	int r, b;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = weston_config_get_section(config, "stuff", NULL, NULL);
 	r = weston_config_section_get_bool(section, "bonk", &b, -1);
-	assert(r == -1 && errno == ENOENT && b == -1);
+	ZUC_ASSERT_EQ(-1, r);
+	ZUC_ASSERT_EQ(ENOENT, errno);
+	ZUC_ASSERT_EQ(-1, b);
+}
 
-	section = weston_config_get_section(config, "bucket", "color", "blue");
+ZUC_TEST_F(config_test_t1, test014)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
+
+	section = weston_config_get_section(config,
+					    "bucket", "color", "blue");
 	r = weston_config_section_get_string(section, "contents", &s, NULL);
-	assert(r == 0 && strcmp(s, "live crabs") == 0);
+
+	ZUC_ASSERTG_EQ(0, r, out_free);
+	ZUC_ASSERTG_STREQ("live crabs", s, out_free);
+
+out_free:
 	free(s);
+}
+
+ZUC_TEST_F(config_test_t1, test015)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
-	section = weston_config_get_section(config, "bucket", "color", "red");
+	section = weston_config_get_section(config,
+					    "bucket", "color", "red");
 	r = weston_config_section_get_string(section, "contents", &s, NULL);
-	assert(r == 0 && strcmp(s, "sand") == 0);
+
+	ZUC_ASSERTG_EQ(0, r, out_free);
+	ZUC_ASSERTG_STREQ("sand", s, out_free);
+
+out_free:
 	free(s);
+}
 
-	section = weston_config_get_section(config, "bucket", "color", "pink");
-	assert(section == NULL);
+ZUC_TEST_F(config_test_t1, test016)
+{
+	char *s;
+	int r;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
+
+	section = weston_config_get_section(config,
+					    "bucket", "color", "pink");
+	ZUC_ASSERT_NULL(section);
 	r = weston_config_section_get_string(section, "contents", &s, "eels");
-	assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0);
+
+	ZUC_ASSERTG_EQ(-1, r, out_free);
+	ZUC_ASSERTG_EQ(ENOENT, errno, out_free);
+	ZUC_ASSERTG_STREQ("eels", s, out_free);
+
+out_free:
 	free(s);
+}
+
+ZUC_TEST_F(config_test_t1, test017)
+{
+	const char *name;
+	int i;
+	struct weston_config_section *section;
+	struct weston_config *config = data;
 
 	section = NULL;
 	i = 0;
 	while (weston_config_next_section(config, &section, &name))
-		assert(strcmp(section_names[i++], name) == 0);
-	assert(i == 5);
+		ZUC_ASSERT_STREQ(section_names[i++], name);
 
-	weston_config_destroy(config);
+	ZUC_ASSERT_EQ(5, i);
+}
 
-	config = run_test(t2);
-	assert(config == NULL);
+ZUC_TEST_F(config_test_t2, doesnt_parse)
+{
+	struct weston_config *config = data;
+	ZUC_ASSERT_NULL(config);
+}
 
-	config = run_test(t3);
-	assert(config == NULL);
+ZUC_TEST_F(config_test_t3, doesnt_parse)
+{
+	struct weston_config *config = data;
+	ZUC_ASSERT_NULL(config);
+}
 
-	config = run_test(t4);
-	assert(config == NULL);
+ZUC_TEST_F(config_test_t4, doesnt_parse)
+{
+	struct weston_config *config = data;
+	ZUC_ASSERT_NULL(config);
+}
 
+ZUC_TEST(config_test, destroy_null)
+{
 	weston_config_destroy(NULL);
-	assert(weston_config_next_section(NULL, NULL, NULL) == 0);
+	ZUC_ASSERT_EQ(0, weston_config_next_section(NULL, NULL, NULL));
+}
 
+ZUC_TEST(config_test, section_from_null)
+{
+	struct weston_config_section *section;
 	section = weston_config_get_section(NULL, "bucket", NULL, NULL);
-	assert(section == NULL);
-
-	return 0;
+	ZUC_ASSERT_NULL(section);
 }
-- 
2.1.0



More information about the wayland-devel mailing list