#include <glib.h>

#include "goffice/utils/regutf8.h"

typedef struct {
	GORegexp re;
} Fixture;

static void
fixture_setup (Fixture *fix,
	       gconstpointer test_data)
{
	g_assert_cmpint (REG_NOERROR, ==, go_regcomp (&fix->re, test_data, REG_ICASE));
}

static void
fixture_teardown (Fixture *fix,
		  gconstpointer test_data)
{
	go_regfree (&fix->re);
}

static gchar *
get_matched_string (const gchar *text, GORegmatch match) 
{
	return g_strndup (text + match.rm_so, match.rm_eo - match.rm_so);
}

static void
test_head_character (Fixture *fix, gconstpointer test_data)
{
	GORegmatch match[10];
	const gchar *text;
	gchar *matched_string;

	text = "abc";
	g_assert_cmpint (REG_NOERROR, ==, go_regexec (&fix->re, text, G_N_ELEMENTS (match), match, 0));
	matched_string = get_matched_string (text, match[0]);
	g_assert_cmpstr ("a", ==, get_matched_string (text, match[0]));
	g_free (matched_string);
	g_assert_cmpint (-1, ==, match[1].rm_so);
	g_assert_cmpint (-1, ==, match[1].rm_eo);
}

static void
test_tail_character (Fixture *fix, gconstpointer test_data)
{
	GORegmatch match[10];
	const gchar *text;
	gchar *matched_string;

	text = "abc";
	g_assert_cmpint (REG_NOERROR, ==, go_regexec (&fix->re, text, G_N_ELEMENTS (match), match, 0));
	matched_string = get_matched_string (text, match[0]);
	g_assert_cmpstr ("c", ==, matched_string);
	g_free (matched_string);
	g_assert_cmpint (-1, ==, match[1].rm_so);
	g_assert_cmpint (-1, ==, match[1].rm_eo);
}

int
main (int argc, char *argv[])
{
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/regex/head_character", Fixture, "^a", fixture_setup, test_head_character, fixture_teardown);
	g_test_add ("/regex/tail_character", Fixture, "c$", fixture_setup, test_tail_character, fixture_teardown);

	return g_test_run ();
}

