/*
 * Copyright (C) 2023 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>

/******************************************************************************
 * Changed Tests
 *****************************************************************************/
static void
test_filter_changed_cb(BirbFilter *filter, gpointer data) {
	guint *counter = data;

	birb_assert_type(filter, BIRB_TYPE_FILTER);

	*counter = *counter + 1;
}

static void
test_filter_changed(void) {
	BirbFilter *filter = NULL;
	guint counter = 0;

	filter = g_object_new(BIRB_TYPE_FILTER, NULL);
	birb_assert_type(filter, BIRB_TYPE_FILTER);

	g_signal_connect(filter, "changed", G_CALLBACK(test_filter_changed_cb),
	                 &counter);

	birb_filter_changed(filter);
	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(filter);
}

/******************************************************************************
 * Matches tests
 *****************************************************************************/
static gboolean
test_filter_matches_impl(BirbFilter *filter, GObject *item) {
	gpointer ret = NULL;

	birb_assert_type(filter, BIRB_TYPE_FILTER);
	birb_assert_type(item, G_TYPE_OBJECT);

	ret = g_object_get_data(G_OBJECT(filter), "matches");

	return GPOINTER_TO_INT(ret);
}

static void
test_filter_matches(gconstpointer data) {
	BirbFilter *filter = NULL;
	BirbFilterClass *klass = NULL;
	GObject *item = NULL;
	gboolean actual = FALSE;
	gboolean expected = GPOINTER_TO_INT(data);

	filter = g_object_new(BIRB_TYPE_FILTER, NULL);
	birb_assert_type(filter, BIRB_TYPE_FILTER);

	klass = BIRB_FILTER_GET_CLASS(filter);
	klass->matches = test_filter_matches_impl;

	g_object_set_data(G_OBJECT(filter), "matches", (gpointer)data);

	item = g_object_new(G_TYPE_OBJECT, NULL);

	actual = birb_filter_matches(filter, item);
	if(expected) {
		g_assert_true(actual);
	} else {
		g_assert_false(actual);
	}

	g_assert_finalize_object(item);
	g_assert_finalize_object(filter);
}

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

	g_test_set_nonfatal_assertions();

	g_test_add_func("/filter/changed", test_filter_changed);
	g_test_add_data_func("/filter/matches/true", GINT_TO_POINTER(TRUE),
	                     test_filter_matches);
	g_test_add_data_func("/filter/matches/false", GINT_TO_POINTER(FALSE),
	                     test_filter_matches);

	return g_test_run();
}
