/*
 * test_attr.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 <stdlib.h>

#include "status.h"

#include "attr.c"


START_TEST(test_attr1_no_status)
{
	struct SH_Attr * attr;
	const char * name = "name";

	attr = SH_Attr_new (name, NULL, NULL);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert_ptr_ne (name, attr->name);
	ck_assert_str_eq (name, attr->name);
	ck_assert_ptr_eq (NULL, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr1_with_status)
{
	struct SH_Status status;
	struct SH_Attr * attr;
	const char * name = "name";

	_status_preinit (status);
	attr = SH_Attr_new (name, NULL, &status);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert (succeed (&status));
	ck_assert_ptr_ne (name, attr->name);
	ck_assert_str_eq (name, attr->name);
	ck_assert_ptr_eq (NULL, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr2_no_status)
{
	struct SH_Attr * attr;
	const char * name = "name";
	const char * value = "value";

	attr = SH_Attr_new (name, value, NULL);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert_ptr_ne (name, attr->name);
	ck_assert_str_eq (name, attr->name);
	ck_assert_ptr_ne (value, attr->value);
	ck_assert_str_eq (value, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr2_with_status)
{
	struct SH_Status status;
	struct SH_Attr * attr;
	const char * name = "name";
	const char * value = "value";

	_status_preinit (status);
	attr = SH_Attr_new (name, value, &status);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert (succeed (&status));
	ck_assert_ptr_ne (name, attr->name);
	ck_assert_str_eq (name, attr->name);
	ck_assert_ptr_ne (value, attr->value);
	ck_assert_str_eq (value, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr_raw1_no_status)
{
	struct SH_Attr * attr;
	char * name = strdup ("name");

	attr = SH_Attr_raw_new (name, NULL, NULL);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert_ptr_eq (name, attr->name);
	ck_assert_ptr_eq (NULL, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr_raw1_with_status)
{
	struct SH_Status status;
	struct SH_Attr * attr;
	char * name = strdup ("name");

	_status_preinit (status);
	attr = SH_Attr_raw_new (name, NULL, &status);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert (succeed (&status));
	ck_assert_ptr_eq (name, attr->name);
	ck_assert_ptr_eq (NULL, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr_raw2_no_status)
{
	struct SH_Attr * attr;
	char * name = strdup ("name");
	char * value = strdup ("value");

	attr = SH_Attr_raw_new (name, value, NULL);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert_ptr_eq (name, attr->name);
	ck_assert_ptr_eq (value, attr->value);

	SH_Attr_free (attr);
}
END_TEST

START_TEST(test_attr_raw2_with_status)
{
	struct SH_Status status;
	struct SH_Attr * attr;
	char * name = strdup ("name");
	char * value = strdup ("value");

	_status_preinit (status);
	attr = SH_Attr_raw_new (name, value, &status);
	ck_assert_ptr_ne (NULL, attr);
	ck_assert (succeed (&status));
	ck_assert_ptr_eq (name, attr->name);
	ck_assert_ptr_eq (value, attr->value);

	SH_Attr_free (attr);
}
END_TEST

Suite * test_suite (void)
{
	Suite *s;
	TCase *tc_core;

	s = suite_create ("Testsuite SeFHT Attr");

	/* Core test case */
	tc_core = tcase_create ("Core");

	tcase_add_test (tc_core, test_attr1_no_status);
	tcase_add_test (tc_core, test_attr1_with_status);
	tcase_add_test (tc_core, test_attr2_no_status);
	tcase_add_test (tc_core, test_attr2_with_status);

	tcase_add_test (tc_core, test_attr_raw1_no_status);
	tcase_add_test (tc_core, test_attr_raw1_with_status);
	tcase_add_test (tc_core, test_attr_raw2_no_status);
	tcase_add_test (tc_core, test_attr_raw2_with_status);
	suite_add_tcase (s, tc_core);

	return s;
}

int main (void)
{
	int number_failed;
	Suite *s;
	SRunner *sr;

	s = test_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;
}