Signed-off-by: Jon A. Cruz <j...@osg.samsung.com> --- Makefile.am | 9 +- tests/config-parser-test.c | 368 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 310 insertions(+), 67 deletions(-)
diff --git a/Makefile.am b/Makefile.am index f0ed4d2..8234d2a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1144,7 +1144,14 @@ libtest_runner_la_SOURCES = \ libtest_runner_la_CFLAGS = $(GCC_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 = \ + -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..926d291 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,70 @@ #include "config-parser.h" -static struct weston_config * -run_test(const char *text) -{ +#include "zunitc/zunitc.h" + +#define ARRAY_LENGTH(a) ((int) (sizeof (a) / sizeof (a)[0])) + +struct fixture_data { + const char *text; struct weston_config *config; +}; + +static struct weston_config *load_config(const char *text, bool good_content) +{ + struct weston_config *config = NULL; char file[] = "/tmp/weston-config-parser-test-XXXXXX"; - int fd, len; - fd = mkstemp(file); - len = write(fd, text, strlen(text)); - assert(len == (int) strlen(text)); + ZUC_EXPECT_TRUE(text != NULL); + if (text) { + int len = 0; + int fd = mkstemp(file); + + len = write(fd, text, strlen(text)); + ZUC_EXPECT_EQ((int)strlen(text), len); + + config = weston_config_parse(file); + close(fd); + unlink(file); + + if (good_content) + ZUC_EXPECT_TRUE(config != NULL); + else + ZUC_EXPECT_TRUE(config == NULL); + } + return config; +} - config = weston_config_parse(file); - close(fd); - unlink(file); +static void *setup_test_config(void *data) +{ + struct weston_config *config = load_config(data, true); + + if (zuc_has_failure()) + ZUC_MARK_FATAL("Fixture setup failed."); return config; } -static const char t0[] = - "# nothing in this file...\n"; +static void *setup_test_config_failing(void *data) +{ + return load_config(data, false); +} + +static void cleanup_test_config(void *data) +{ + struct weston_config *config = data; + ZUC_ASSERT_TRUE(config != NULL); + 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 const char t1[] = +static struct zuc_fixture config_test_t1 = { + .data = "# comment line here...\n" "\n" "[foo]\n" @@ -79,130 +122,323 @@ 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_TRUE(config != NULL); +} - config = run_test(t0); - assert(config); - weston_config_destroy(config); +/** @todo individual t1 tests should have more descriptive names. */ - config = run_test(t1); - assert(config); - section = weston_config_get_section(config, "mollusc", NULL, NULL); - assert(section == NULL); +ZUC_TEST_F(config_test_t1, test001) +{ + struct weston_config_section *section; + struct weston_config *config = data; + ZUC_ASSERT_TRUE(config != NULL); + section = weston_config_get_section(config, + "mollusc", NULL, NULL); + ZUC_ASSERT_TRUE(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_EXPECT_EQ(0, r); + ZUC_EXPECT_EQ(0, strcmp(s, "b")); 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_EXPECT_EQ(-1, r); + ZUC_EXPECT_EQ(ENOENT, errno); + ZUC_EXPECT_TRUE(s == NULL); +} + +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_EXPECT_EQ(0, r); + ZUC_EXPECT_EQ(0, strcmp(s, "Roy Batty")); + 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_EXPECT_EQ(-1, r); + ZUC_EXPECT_EQ(ENOENT, errno); + ZUC_EXPECT_EQ(0, strcmp(s, "boo")); + 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_EXPECT_EQ(0, r); + ZUC_EXPECT_EQ(0, strcmp(s, "live crabs")); + free(s); +} - section = weston_config_get_section(config, "bucket", "color", "red"); +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"); r = weston_config_section_get_string(section, "contents", &s, NULL); - assert(r == 0 && strcmp(s, "sand") == 0); + + ZUC_EXPECT_EQ(0, r); + ZUC_EXPECT_EQ(0, strcmp(s, "sand")); + 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_TRUE(section == NULL); r = weston_config_section_get_string(section, "contents", &s, "eels"); - assert(r == -1 && errno == ENOENT && strcmp(s, "eels") == 0); + + ZUC_EXPECT_EQ(-1, r); + ZUC_EXPECT_EQ(ENOENT, errno); + ZUC_EXPECT_EQ(0, strcmp(s, "eels")); + 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, §ion, &name)) - assert(strcmp(section_names[i++], name) == 0); - assert(i == 5); + ZUC_EXPECT_EQ(0, strcmp(section_names[i++], name)); - weston_config_destroy(config); + ZUC_EXPECT_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_TRUE(config == NULL); +} - config = run_test(t3); - assert(config == NULL); +ZUC_TEST_F(config_test_t3, doesnt_parse) +{ + struct weston_config *config = data; + ZUC_ASSERT_TRUE(config == NULL); +} - config = run_test(t4); - assert(config == NULL); +ZUC_TEST_F(config_test_t4, doesnt_parse) +{ + struct weston_config *config = data; + ZUC_ASSERT_TRUE(config == NULL); +} +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_TRUE(section == NULL); } -- 2.1.0 _______________________________________________ wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel