/* * test_node_fragment.c * * Copyright 2022 Jonathan Schöbel <jonathan@Ubermos-2019> * * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * */ #include <check.h> #include <stdbool.h> #include <stdlib.h> #include "macro.h" #include "status.h" #include "node_fragment.h" START_TEST(test_node_fragment) { struct SH_Status status; struct SH_Fragment * fragment; struct SH_Data * data; const char * tag = "tag"; data = SH_Data_new (NULL); /* valid tag - no error */ fragment = SH_NodeFragment_new (tag, data, NULL); ck_assert_int_ne ((long int) fragment, (long int) NULL); ck_assert_str_eq (((struct SH_NodeFragment *) fragment)->tag, tag); SH_Fragment_free (fragment); /* valid tag - error */ status.status = UNDEFINED; fragment = SH_NodeFragment_new (tag, data, &status); ck_assert_int_ne ((long int) fragment, (long int) NULL); ck_assert_int_eq (status.status, SUCCESS); ck_assert_str_eq (((struct SH_NodeFragment *) fragment)->tag, tag); SH_Fragment_free (fragment); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_copy) { struct SH_Status status; struct SH_Fragment * fragment; struct SH_Fragment * copy; struct SH_Data * data; data = SH_Data_new (NULL); fragment = SH_NodeFragment_new ("html", data, NULL); /* without error */ copy = SH_NodeFragment_copy (((struct SH_NodeFragment *) fragment), NULL); ck_assert_ptr_ne (copy, NULL); ck_assert_ptr_ne (fragment, copy); ck_assert_str_eq (((struct SH_NodeFragment *) fragment)->tag, ((struct SH_NodeFragment *) copy)->tag); ck_assert_int_eq (((struct SH_NodeFragment *) copy)->child_n, 0); ck_assert_ptr_ne (((struct SH_NodeFragment *) fragment)->childs, ((struct SH_NodeFragment *) copy)->childs); SH_Fragment_free (copy); /* with error */ status.status = UNDEFINED; copy = SH_NodeFragment_copy (((struct SH_NodeFragment *) fragment), &status); ck_assert_int_eq (status.status, SUCCESS); ck_assert_ptr_ne (copy, NULL); ck_assert_ptr_ne (fragment, copy); ck_assert_str_eq (((struct SH_NodeFragment *) fragment)->tag, ((struct SH_NodeFragment *) copy)->tag); ck_assert_int_eq (((struct SH_NodeFragment *) copy)->child_n, 0); ck_assert_ptr_ne (((struct SH_NodeFragment *) fragment)->childs, ((struct SH_NodeFragment *) copy)->childs); SH_Fragment_free (fragment); SH_Fragment_free (copy); SH_Data_free (data); } END_TEST static void check_childs (struct SH_NodeFragment * fragment, struct SH_NodeFragment * copy) { size_t index; for (index = 0; index < copy->child_n; index++) { ck_assert_ptr_ne (((struct SH_NodeFragment *) fragment->childs[index]), ((struct SH_NodeFragment *) copy->childs[index])); ck_assert_str_eq (((struct SH_NodeFragment *) fragment->childs[index])->tag, ((struct SH_NodeFragment *) copy->childs[index])->tag); ck_assert_int_eq (((struct SH_NodeFragment *) fragment->childs[index])->child_n, ((struct SH_NodeFragment *) copy->childs[index])->child_n); check_childs (((struct SH_NodeFragment *) fragment->childs[index]), ((struct SH_NodeFragment *) copy->childs[index])); } return; } START_TEST(test_node_fragment_deepcopy) { struct SH_Status status; struct SH_Fragment * fragment; struct SH_Fragment * child1; struct SH_Fragment * child2; struct SH_Fragment * copy; struct SH_Data * data; data = SH_Data_new (NULL); fragment = SH_NodeFragment_new ("html", data, NULL); child1 = SH_NodeFragment_new ("head", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) fragment), child1, NULL); child2 = SH_NodeFragment_new ("title", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) child1), child2, NULL); child1 = SH_NodeFragment_new ("body", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) fragment), child1, NULL); child2 = SH_NodeFragment_new ("header", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) child1), child2, NULL); child2 = SH_NodeFragment_new ("main", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) child1), child2, NULL); child2 = SH_NodeFragment_new ("footer", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) child1), child2, NULL); /* without error */ copy = SH_Fragment_copy (fragment, NULL); ck_assert_ptr_ne (copy, NULL); ck_assert_ptr_ne (fragment, copy); ck_assert_str_eq (((struct SH_NodeFragment *) fragment)->tag, ((struct SH_NodeFragment *) copy)->tag); ck_assert_int_eq (((struct SH_NodeFragment *) fragment)->child_n, ((struct SH_NodeFragment *) copy)->child_n); ck_assert_ptr_ne (((struct SH_NodeFragment *) fragment)->childs, ((struct SH_NodeFragment *) copy)->childs); check_childs (((struct SH_NodeFragment *) fragment), ((struct SH_NodeFragment *) copy)); SH_Fragment_free (copy); /* with error */ status.status = UNDEFINED; copy = SH_Fragment_copy (fragment, &status); ck_assert_int_eq (status.status, SUCCESS); ck_assert_ptr_ne (copy, NULL); ck_assert_ptr_ne (fragment, copy); ck_assert_str_eq (((struct SH_NodeFragment *) fragment)->tag, ((struct SH_NodeFragment *) copy)->tag); ck_assert_int_eq (((struct SH_NodeFragment *) fragment)->child_n, ((struct SH_NodeFragment *) copy)->child_n); ck_assert_ptr_ne (((struct SH_NodeFragment *) fragment)->childs, ((struct SH_NodeFragment *) copy)->childs); check_childs (((struct SH_NodeFragment *) fragment), ((struct SH_NodeFragment *) copy)); SH_Fragment_free (fragment); SH_Fragment_free (copy); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_tag) { struct SH_Status status; struct SH_Fragment * fragment; struct SH_Data * data; char * tag; const char * tag1 = "html"; const char * tag2 = "body"; data = SH_Data_new (NULL); /* no error */ fragment = SH_NodeFragment_new (tag1, data, NULL); tag = SH_NodeFragment_get_tag (((struct SH_NodeFragment *) fragment), NULL); ck_assert_str_eq (tag, tag1); free (tag); SH_Fragment_free (fragment); /* error */ fragment = SH_NodeFragment_new (tag2, data, NULL); status.status = UNDEFINED; tag = SH_NodeFragment_get_tag (((struct SH_NodeFragment *) fragment), &status); ck_assert_str_eq (tag, tag2); ck_assert_int_eq (status.status, SUCCESS); free (tag); SH_Fragment_free (fragment); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_child) { struct SH_Status status; struct SH_Fragment * parent; struct SH_Fragment * child1; struct SH_Fragment * child2; struct SH_Data * data; bool boolean; data = SH_Data_new (NULL); parent = SH_NodeFragment_new ("html", data, NULL); child1 = SH_NodeFragment_new ("head", data, NULL); child2 = SH_NodeFragment_new ("body", data, NULL); /* no error */ ck_assert_int_eq (((struct SH_NodeFragment *) parent)->child_n, 0); boolean = SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent), child1, NULL); ck_assert_int_eq (boolean, TRUE); ck_assert_int_eq (((struct SH_NodeFragment *) parent)->child_n, 1); ck_assert_ptr_eq (((struct SH_NodeFragment *) parent)->childs[0], child1); /* with error */ ck_assert_int_eq (((struct SH_NodeFragment *) parent)->child_n, 1); status.status = UNDEFINED; boolean = SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent), child2, &status); ck_assert_int_eq (boolean, TRUE); ck_assert_int_eq (status.status, SUCCESS); ck_assert_int_eq (((struct SH_NodeFragment *) parent)->child_n, 2); ck_assert_ptr_eq (((struct SH_NodeFragment *) parent)->childs[1], child2); SH_Fragment_free (parent); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_get_child) { struct SH_Status status; struct SH_Fragment * parent; struct SH_Fragment * child1; struct SH_Fragment * child2; struct SH_Data * data; data = SH_Data_new (NULL); parent = SH_NodeFragment_new ("html", data, NULL); child1 = SH_NodeFragment_new ("body", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent), child1, NULL); /* without error */ child2 = SH_NodeFragment_get_child (((struct SH_NodeFragment *) parent), 0, NULL); ck_assert_ptr_eq (child1, child2); child2 = SH_NodeFragment_get_child (((struct SH_NodeFragment *) parent), 1, NULL); ck_assert_ptr_eq (NULL, child2); /* with error */ status.status = UNDEFINED; child2 = SH_NodeFragment_get_child (((struct SH_NodeFragment *) parent), 0, &status); ck_assert_ptr_eq (child1, child2); ck_assert_int_eq (status.status, SUCCESS); status.status = UNDEFINED; child2 = SH_NodeFragment_get_child (((struct SH_NodeFragment *) parent), 1, &status); ck_assert_ptr_eq (NULL, child2); ck_assert_int_eq (status.status, E_VALUE); SH_Fragment_free (parent); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_is_child) { struct SH_Fragment * parent; struct SH_Fragment * child1; struct SH_Fragment * child2; struct SH_Data * data; bool boolean; data = SH_Data_new (NULL); parent = SH_NodeFragment_new ("html", data, NULL); child1 = SH_NodeFragment_new ("head", data, NULL); child2 = SH_NodeFragment_new ("title", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent), child1, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) child1), child2, NULL); boolean = SH_NodeFragment_is_child (((struct SH_NodeFragment *) parent), child1); ck_assert_int_eq (boolean, TRUE); boolean = SH_NodeFragment_is_child (((struct SH_NodeFragment *) parent), child2); ck_assert_int_eq (boolean, FALSE); boolean = SH_NodeFragment_is_child (((struct SH_NodeFragment *) child1), child2); ck_assert_int_eq (boolean, TRUE); SH_Fragment_free (parent); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_is_descendant) { struct SH_Fragment * parent; struct SH_Fragment * child1; struct SH_Fragment * child2; struct SH_Fragment * child3; struct SH_Data * data; bool boolean; data = SH_Data_new (NULL); parent = SH_NodeFragment_new ("html", data, NULL); child1 = SH_NodeFragment_new ("head", data, NULL); child2 = SH_NodeFragment_new ("body", data, NULL); child3 = SH_NodeFragment_new ("title", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent), child1, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent), child2, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) child1), child3, NULL); boolean = SH_NodeFragment_is_descendant (((struct SH_NodeFragment *) parent), child1); ck_assert_int_eq (boolean, TRUE); boolean = SH_NodeFragment_is_descendant (((struct SH_NodeFragment *) parent), child2); ck_assert_int_eq (boolean, TRUE); boolean = SH_NodeFragment_is_descendant (((struct SH_NodeFragment *) parent), child3); ck_assert_int_eq (boolean, TRUE); boolean = SH_NodeFragment_is_descendant (((struct SH_NodeFragment *) child1), child2); ck_assert_int_eq (boolean, FALSE); boolean = SH_NodeFragment_is_descendant (((struct SH_NodeFragment *) child1), child3); ck_assert_int_eq (boolean, TRUE); boolean = SH_NodeFragment_is_descendant (((struct SH_NodeFragment *) child2), child3); ck_assert_int_eq (boolean, FALSE); SH_Fragment_free (parent); SH_Data_free (data); } END_TEST START_TEST(test_node_fragment_html) { struct SH_Status status; struct SH_Fragment * fragment1; struct SH_Fragment * fragment2; struct SH_Data * data; struct SH_Text * text; char * string; size_t length; data = SH_Data_new (NULL); /* no error */ fragment1 = SH_NodeFragment_new ("html", data, NULL); fragment2 = SH_NodeFragment_new ("body", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) fragment1), fragment2, NULL); text = SH_Fragment_to_html (fragment1, INLINE, 0, 1, INDENT_TEXT, NULL); string = SH_Text_get_string (text, 0, SIZE_MAX, &length, NULL); ck_assert_str_eq (string, "<html><body></body></html>"); free (string); text = SH_Fragment_to_html (fragment1, WRAP, 0, 1, INDENT_TEXT, NULL); string = SH_Text_get_string (text, 0, SIZE_MAX, &length, NULL); ck_assert_str_eq (string, "<html>\n\t<body>\n\t</body>\n</html>\n"); free (string); SH_Fragment_free (fragment1); /* error */ fragment1 = SH_NodeFragment_new ("html", data, NULL); fragment2 = SH_NodeFragment_new ("body", data, NULL); SH_NodeFragment_append_child (((struct SH_NodeFragment *) fragment1), fragment2, NULL); status.status = UNDEFINED; text = SH_Fragment_to_html (fragment1, INLINE, 0, 1, INDENT_TEXT, &status); string = SH_Text_get_string (text, 0, SIZE_MAX, &length, NULL); ck_assert_str_eq (string, "<html><body></body></html>"); free (string); ck_assert_int_eq (status.status, SUCCESS); status.status = UNDEFINED; text = SH_Fragment_to_html (fragment1, WRAP, 0, 1, INDENT_TEXT, &status); string = SH_Text_get_string (text, 0, SIZE_MAX, &length, NULL); ck_assert_str_eq (string, "<html>\n\t<body>\n\t</body>\n</html>\n"); free (string); ck_assert_int_eq (status.status, SUCCESS); SH_Fragment_free (fragment1); SH_Data_free (data); } END_TEST Suite * fragment_suite (void) { Suite *s; TCase *tc_core; s = suite_create ("Testsuite SeFHT Fragment"); /* Core test case */ tc_core = tcase_create ("Core"); tcase_add_test (tc_core, test_node_fragment); tcase_add_test (tc_core, test_node_fragment_copy); tcase_add_test (tc_core, test_node_fragment_deepcopy); tcase_add_test (tc_core, test_node_fragment_tag); tcase_add_test (tc_core, test_node_fragment_child); tcase_add_test (tc_core, test_node_fragment_get_child); tcase_add_test (tc_core, test_node_fragment_is_child); tcase_add_test (tc_core, test_node_fragment_is_descendant); tcase_add_test (tc_core, test_node_fragment_html); suite_add_tcase (s, tc_core); return s; } int main (int argc, char **argv) { int number_failed; Suite *s; SRunner *sr; s = fragment_suite (); sr = srunner_create (s); srunner_run_all (sr, CK_NORMAL); number_failed = srunner_ntests_failed (sr); srunner_free (sr); return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }