/*
 * 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>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_birb_readline_new(void) {
	BirbReadline *readline = NULL;

	readline = birb_readline_new("birb> ");

	birb_assert_type(readline, BIRB_TYPE_READLINE);

	g_assert_finalize_object(readline);
}

static void
test_birb_readline_properties(void) {
	BirbReadline *readline = NULL;
	char *prompt = NULL;

	/* We use the new function as the input and output stream properties are
	 * write-only construct-only properties that check for valid values.
	 */
	readline = g_object_new(BIRB_TYPE_READLINE,
	                        "prompt", "> ",
	                        NULL);

	birb_assert_type(readline, BIRB_TYPE_READLINE);

	g_object_get(readline,
	             "prompt", &prompt,
	             NULL);

	g_assert_cmpstr(prompt, ==, "> ");
	g_clear_pointer(&prompt, g_free);

	g_assert_finalize_object(readline);
}

/******************************************************************************
 * Input tests
 *****************************************************************************/
static void
test_birb_readline_read_cb(GObject *source,
                           GAsyncResult *result,
                           gpointer data)
{
	BirbReadline *readline = BIRB_READLINE(source);
	GCancellable *cancellable = NULL;
	GError *error = NULL;
	char *line = NULL;
	guint *counter = data;

	cancellable = g_task_get_cancellable(G_TASK(result));

	line = birb_readline_read_stream_finish(readline, result, &error);
	g_assert_no_error(error);

	g_assert_cmpstr(line, ==, "ls");
	g_clear_pointer(&line, g_free);

	*counter = *counter + 1;

	g_cancellable_cancel(cancellable);
}

static void
test_birb_readline_input_simple(void) {
	BirbReadline *readline = NULL;
	GCancellable *cancellable = NULL;
	GInputStream *input = NULL;
	guint counter = 0;

	input = g_memory_input_stream_new_from_data("ls\n", -1, NULL);

	readline = birb_readline_new("> ");
	cancellable = g_cancellable_new();

	birb_readline_read_stream_async(readline,
	                                input,
	                                cancellable,
	                                test_birb_readline_read_cb,
	                                &counter);

	/* We have to loop multiple times because we're only reading one byte at a
	 * time.
	 */
	while(!g_cancellable_is_cancelled(cancellable)) {
		g_main_context_iteration(NULL, FALSE);
	}

	g_assert_cmpuint(counter, ==, 1);

	g_assert_finalize_object(readline);
	g_assert_finalize_object(input);
	g_assert_finalize_object(cancellable);
}

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

	g_test_add_func("/birb/readline/new", test_birb_readline_new);
	g_test_add_func("/birb/readline/properties",
	                test_birb_readline_properties);

	g_test_add_func("/birb/readline/input/simple",
	                test_birb_readline_input_simple);

	return g_test_run();
}
