/*
 * Copyright (C) 2023-2025 Birb Developers
 *
 * Birb is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the AUTHORS file distributed
 * with this source distribution
 *
 * 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 02111-1301 USA
 */

#include <glib.h>

#include <birb.h>

/******************************************************************************
 * Callbacks
 *****************************************************************************/
static void
test_birb_string_object_property_counter(GObject *obj,
                                         G_GNUC_UNUSED GParamSpec *pspec,
                                         gpointer data)
{
	guint *counter = data;

	birb_assert_type(obj, BIRB_TYPE_STRING_OBJECT);

	*counter = *counter + 1;
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_birb_string_object_new(void) {
	BirbStringObject *obj = NULL;

	obj = birb_string_object_new("birdy");
	birb_assert_type(obj, BIRB_TYPE_STRING_OBJECT);

	g_assert_finalize_object(obj);
}

static void
test_birb_string_object_properties(void) {
	BirbStringObject *obj = NULL;
	char *str = NULL;
	guint counter = 0;

	obj = g_object_new(
		BIRB_TYPE_STRING_OBJECT,
		"string", "birdy",
		NULL);

	counter = 0;
	g_signal_connect(obj, "notify",
	                 G_CALLBACK(test_birb_string_object_property_counter),
	                 &counter);

	g_object_get(
		G_OBJECT(obj),
		"string", &str,
		NULL);

	/* Make sure we didn't notify of anything. */
	g_assert_cmpuint(counter, ==, 0);

	g_assert_cmpstr(str, ==, "birdy");
	g_clear_pointer(&str, g_free);

	/* Now set our writable properties and verify that they only notify on
	 * change.
	 */
	counter = 0;

	birb_string_object_set_string(obj, "birdy");
	birb_string_object_set_string(obj, "birdy the bird");

	g_assert_cmpuint(counter, ==, 1);

	/* And we're done. */
	g_assert_finalize_object(obj);
}

static void
test_birb_string_object_new_accepts_null(void) {
	BirbStringObject *obj = NULL;

	/* g_return_val_if_fail outputs a warning that would cause the test to fail
	 * so we don't need an explicit assert. This also calls set_string so we
	 * don't need an explicit test for that either.
	 */
	obj = birb_string_object_new(NULL);

	birb_assert_type(obj, BIRB_TYPE_STRING_OBJECT);

	g_assert_finalize_object(obj);
}

static void
test_birb_string_object_equal(void) {
	BirbStringObject *str1 = NULL;
	BirbStringObject *str2 = NULL;
	BirbStringObject *str3 = NULL;

	str1 = birb_string_object_new(NULL);
	str2 = birb_string_object_new("test");
	str3 = birb_string_object_new("test");

	/* Test the NULL cases. */
	g_assert_true(birb_string_object_equal(NULL, NULL));
	g_assert_false(birb_string_object_equal(str1, NULL));
	g_assert_false(birb_string_object_equal(NULL, str1));

	/* Test the FALSE cases. */
	g_assert_false(birb_string_object_equal(str1, str2));
	g_assert_false(birb_string_object_equal(str2, str1));
	g_assert_false(birb_string_object_equal(str1, str3));
	g_assert_false(birb_string_object_equal(str3, str1));

	/* And make sure a TRUE case works. */
	g_assert_true(birb_string_object_equal(str2, str3));

	g_assert_finalize_object(str1);
	g_assert_finalize_object(str2);
	g_assert_finalize_object(str3);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char **argv) {
	g_test_init(&argc, &argv, NULL);

	g_test_set_nonfatal_assertions();

	g_test_add_func("/birb/string-object/new", test_birb_string_object_new);
	g_test_add_func("/birb/string-object/properties",
	                test_birb_string_object_properties);

	g_test_add_func("/birb/string-object/new/accepts-null",
	                test_birb_string_object_new_accepts_null);

	g_test_add_func("/birb/string-object/equal",
	                test_birb_string_object_equal);

	return g_test_run();
}
