/*
 * Copyright (C) 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 Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <birb.h>

/******************************************************************************
 * Append tests
 *****************************************************************************/
static void
test_birb_env_append_new(void) {
	const char *value = NULL;

	birb_env_append("TEST_BIRB_APPEND_NEW", "new-value",
	                G_SEARCHPATH_SEPARATOR_S);

	value = g_getenv("TEST_BIRB_APPEND_NEW");
	g_assert_cmpstr(value, ==, "new-value");
}

static void
test_birb_env_append_existing(void) {
	const char *value = NULL;

	g_setenv("TEST_BIRB_APPEND_EXISTING", "abc123", TRUE);

	birb_env_append("TEST_BIRB_APPEND_EXISTING", "xyz789",
	                G_SEARCHPATH_SEPARATOR_S);

	value = g_getenv("TEST_BIRB_APPEND_EXISTING");
	g_assert_cmpstr(value, ==, "abc123" G_SEARCHPATH_SEPARATOR_S "xyz789");
}

/******************************************************************************
 * Prepend tests
 *****************************************************************************/
static void
test_birb_env_prepend_new(void) {
	const char *value = NULL;

	birb_env_append("TEST_BIRB_PREPEND_NEW", "new-value",
	                G_SEARCHPATH_SEPARATOR_S);

	value = g_getenv("TEST_BIRB_PREPEND_NEW");
	g_assert_cmpstr(value, ==, "new-value");
}

static void
test_birb_env_prepend_existing(void) {
	const char *value = NULL;

	g_setenv("TEST_BIRB_PREPEND_EXISTING", "abc123", TRUE);

	birb_env_prepend("TEST_BIRB_PREPEND_EXISTING", "xyz789",
	                G_SEARCHPATH_SEPARATOR_S);

	value = g_getenv("TEST_BIRB_PREPEND_EXISTING");
	g_assert_cmpstr(value, ==, "xyz789" G_SEARCHPATH_SEPARATOR_S "abc123");
}

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

	g_test_set_nonfatal_assertions();

	g_test_add_func("/env/append/new", test_birb_env_append_new);
	g_test_add_func("/env/append/existing", test_birb_env_append_existing);

	g_test_add_func("/env/prepend/new", test_birb_env_prepend_new);
	g_test_add_func("/env/prepend/existing", test_birb_env_prepend_existing);

	return g_test_run();
}
