/*
 * 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_email_is_valid_valid(void) {
	/* Many of the valid email addresses listed below are from
	 * https://fightingforalostcause.net/misc/2006/compare-email-regex.php.
	 */
	const char *addresses[] = {
		"purple-devel@lists.sf.net",
		"l3tt3rsAndNumb3rs@domain.com",
		"has-dash@domain.com",
		"hasApostrophe.o'leary@domain.org",
		"uncommonTLD@domain.museum",
		"uncommonTLD@domain.travel",
		"uncommonTLD@domain.mobi",
		"countryCodeTLD@domain.uk",
		"countryCodeTLD@domain.rw",
		"lettersInDomain@911.com",
		"underscore_inLocal@domain.net",
		"IPInsteadOfDomain@127.0.0.1",
		"IPAndPort@127.0.0.1:25",
		"subdomain@sub.domain.com",
		"local@dash-inDomain.com",
		"dot.inLocal@foo.com",
		"a@singleLetterLocal.org",
		"singleLetterDomain@x.org",
		"&*=?^+{}'~@validCharsInLocal.net",
		"foo@bar.newTLD",
		"HenryTheGreatWhiteCricket@live.ca",
		"HenryThe__WhiteCricket@hotmail.com",
	};

	for(size_t i = 0; i < G_N_ELEMENTS(addresses); i++) {
		g_assert_true(birb_email_is_valid(addresses[i]));
	}
}

static void
test_birb_email_is_valid_invalid(void) {
	/* Many of the invalid email addresses listed below are from
	 * https://fightingforalostcause.net/misc/2006/compare-email-regex.php.
	 */
	const char *addresses[] = {
		"purple-devel@@lists.sf.net",
		"purple@devel@lists.sf.net",
		"purple-devel",
		"purple-devel@",
		"@lists.sf.net",
		"totally bogus",
		"@missingLocal.org",
		"missingatSign.net",
		"two@@signs.com",
		"colonButNoPort@127.0.0.1:",
	};

	for(size_t i = 0; i < G_N_ELEMENTS(addresses); i++) {
		g_assert_false(birb_email_is_valid(addresses[i]));
	}
}

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

	g_test_add_func("/birb/email/is-valid/valid",
	                test_birb_email_is_valid_valid);
	g_test_add_func("/birb/email/is-valid/invalid",
	                test_birb_email_is_valid_invalid);

	return g_test_run();
}
