Newer
Older
*
* 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 "status.h"
struct SH_Status status;
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);
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);
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);
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);
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);
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]));
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);
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);
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);
struct SH_Status status;
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);
fragment = SH_NodeFragment_new (tag2, data, NULL);
tag = SH_NodeFragment_get_tag (((struct SH_NodeFragment *) fragment),
ck_assert_int_eq (status.status, SUCCESS);
SH_Fragment_free (fragment);
SH_Data_free (data);
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);
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 (((struct SH_NodeFragment *) parent)->child_n,
1);
ck_assert_ptr_eq (((struct SH_NodeFragment *) parent)->childs[0],
child1);
ck_assert_int_eq (((struct SH_NodeFragment *) parent)->child_n, 1);
boolean = SH_NodeFragment_append_child (((struct SH_NodeFragment *) parent),
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);
START_TEST(test_node_fragment_get_child)
struct SH_Status status;
struct SH_Fragment * parent;
struct SH_Fragment * child1;
const 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);
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 */
child2 = SH_NodeFragment_get_child (((struct SH_NodeFragment *) parent),
ck_assert_int_eq (status.status, SUCCESS);
child2 = SH_NodeFragment_get_child (((struct SH_NodeFragment *) parent),
ck_assert_int_eq (status.status, E_VALUE);
SH_Fragment_free (parent);
SH_Data_free (data);
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);
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);
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");
SH_Fragment_free (fragment1);
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,
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);
text = SH_Fragment_to_html (fragment1, WRAP, 0, 1, INDENT_TEXT,
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");
ck_assert_int_eq (status.status, SUCCESS);
SH_Fragment_free (fragment1);
SH_Data_free (data);
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 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;
}