From 0b624c7fba92d74858746328888548d28b55f84a Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sun, 19 Jul 2015 15:23:36 +0100 Subject: Add basic tests for creating a layout object. --- test/Makefile | 3 ++ test/assert-tests.c | 38 +++++++++++++++++++++++ test/basic-layout-tests.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++ test/tests.c | 52 +++++++++++++++++++++++++++++++ test/tests.h | 19 ++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 test/Makefile create mode 100644 test/assert-tests.c create mode 100644 test/basic-layout-tests.c create mode 100644 test/tests.c create mode 100644 test/tests.h diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..5703e79 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,3 @@ +DIR_TEST_ITEMS := testrunner:tests.c;assert-tests.c;basic-layout-tests.c + +include $(NSBUILD)/Makefile.subdir diff --git a/test/assert-tests.c b/test/assert-tests.c new file mode 100644 index 0000000..4fca036 --- /dev/null +++ b/test/assert-tests.c @@ -0,0 +1,38 @@ +/* + * This file is part of LibNSLayout's tests + * Licensed under the ISC License, http://opensource.org/licenses/ISC + * Copyright 2015 Michael Drake + */ + +#include +#include +#include + +#include "tests.h" + +#ifndef UNUSED +#define UNUSED(x) (void)(x) +#endif + +/* TODO: Test for each individual param being NULL. */ +START_TEST (test_nslayout_layout_create_aborts1) +{ + nslayout_layout *layout; + (void) nslayout_layout_create(NULL, NULL, NULL, NULL, NULL, &layout); +} +END_TEST + + +void nslayout_assert_suite(SRunner *sr) +{ + Suite *s = suite_create("libnslayout: API Assert tests"); + TCase *tc_assert = tcase_create("Creation/Destruction"); + + tcase_add_test_raise_signal( + tc_assert, + test_nslayout_layout_create_aborts1, + SIGABRT); + suite_add_tcase(s, tc_assert); + + srunner_add_suite(sr, s); +} diff --git a/test/basic-layout-tests.c b/test/basic-layout-tests.c new file mode 100644 index 0000000..84611a9 --- /dev/null +++ b/test/basic-layout-tests.c @@ -0,0 +1,78 @@ +/* + * This file is part of LibNSLayout's tests + * Licensed under the ISC License, http://opensource.org/licenses/ISC + * Copyright 2015 Michael Drake + */ + +#include +#include +#include + +#include "tests.h" + +#ifndef UNUSED +#define UNUSED(x) (void)(x) +#endif + +int pw; + +static nslayout_error nslayout_test_callback( + nslayout_request *req, + nslayout_layout *layout, + void *pw) +{ + UNUSED(req); + UNUSED(layout); + UNUSED(pw); + return NSLAYOUT_OK; +} + +START_TEST (test_nslayout_layout_create_ok) +{ + nslayout_layout *layout = NULL; + nslayout_error error; + dom_exception dom_error; + css_error css_err; + dom_document *doc; + css_select_ctx *css_ctx; + css_media_type media = CSS_MEDIA_SCREEN; + + /* Create a DOM document */ + dom_error = dom_implementation_create_document(DOM_IMPLEMENTATION_HTML, + NULL, NULL, NULL, NULL, NULL, &doc); + ck_assert(dom_error == DOM_NO_ERR); + + /* Create a selection context (with no sheets added) */ + css_err = css_select_ctx_create(&css_ctx); + ck_assert(css_err == CSS_OK); + + error = nslayout_layout_create(doc, + css_ctx, + &media, + nslayout_test_callback, + &pw, + &layout); + fail_unless(error == NSLAYOUT_OK, + "Unable to create layout"); + fail_unless(layout != NULL, + "Returned OK but str was still NULL"); + + css_err = css_select_ctx_destroy(css_ctx); + ck_assert(css_err == CSS_OK); + + dom_node_unref(doc); +} +END_TEST + + +void nslayout_basic_layout_suite(SRunner *sr) +{ + Suite *s = suite_create("libnslayout: basic layout tests"); + TCase *tc_layout_basic = tcase_create("Creation/Destruction"); + + tcase_add_test(tc_layout_basic, + test_nslayout_layout_create_ok); + suite_add_tcase(s, tc_layout_basic); + + srunner_add_suite(sr, s); +} diff --git a/test/tests.c b/test/tests.c new file mode 100644 index 0000000..a58e038 --- /dev/null +++ b/test/tests.c @@ -0,0 +1,52 @@ +/* + * This file is part of LibNSLayout's tests + * Licensed under the ISC License, http://opensource.org/licenses/ISC + * Copyright 2015 Michael Drake + */ + +#include +#include + +#include + +#include "tests.h" + +#ifndef UNUSED +#define UNUSED(x) ((x) = (x)) +#endif + +#ifndef NDEBUG +/* This means that assertion failures are silent in tests */ +void __assert_fail(const char *__assertion, const char *__file, + unsigned int __line, const char *__function) { + (void)__assertion; + (void)__file; + (void)__line; + (void)__function; + abort(); +} +#endif + +int main(int argc, char **argv) +{ + int number_failed = 0; + SRunner *sr; + + UNUSED(argc); + UNUSED(argv); + + sr = srunner_create(suite_create("Test suite for libnslayout")); + +#ifndef NDEBUG + nslayout_assert_suite(sr); +#endif + nslayout_basic_layout_suite(sr); + + srunner_set_fork_status(sr, CK_FORK); + srunner_run_all(sr, CK_ENV); + number_failed = srunner_ntests_failed(sr); + + srunner_free(sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/test/tests.h b/test/tests.h new file mode 100644 index 0000000..4f7795f --- /dev/null +++ b/test/tests.h @@ -0,0 +1,19 @@ +/* + * This file is part of LibNSLayout's tests + * Licensed under the ISC License, http://opensource.org/licenses/ISC + * Copyright 2015 Michael Drake + */ + +#ifndef nslayout_tests_h_ +#define nslayout_tests_h_ + +#include + +#include + +#include + +extern void nslayout_assert_suite(SRunner *); +extern void nslayout_basic_layout_suite(SRunner *); + +#endif -- cgit v1.2.3