/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 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 Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include <QObject>
using ::QObject;
// using Q_OBJECT
#include <QString>
using ::QString;
#include <QTest>
// using QTEST_MAIN
// using QVERIFY_EXCEPTION_THROWN
#include <qt-mocks/Matchers.h>
using qt_mocks::equals;
using qt_mocks::isFalse;
using qt_mocks::isTrue;
using qt_mocks::memberFunction;
// using QVERIFY_THAT

#include <accounting/AccountCode.h>
using drn::accounting::AccountCode;
using drn::accounting::AccountNumber;
#include <accounting/AccountingErrors.h>
using drn::accounting::AccountError;
#include <foundation/Error.h>
using drn::foundation::Error;


namespace drn
{
namespace accounting
{
namespace tests
{

struct AccountCodeUT : QObject
{
	Q_OBJECT

private slots:
	//{ constructor Tests

	void constructor_default_ShouldBeDefaults()
	{
		const AccountCode ac{};
		QVERIFY_THAT(ac.number(), equals(AccountNumber{}));
		QVERIFY_TRUE(ac.name().isEmpty());
		QVERIFY_FALSE(ac.parent().hasValue());
	}

	void constructor_nameInit_ShouldBeNameAndRestDefaults()
	{
		const AccountNumber expectedNumber{};
		const QString expected{"New Account Dude"};
		const AccountNumber expectedParent{1010u};
		const AccountCode ac{expected, expectedParent};
		QVERIFY_THAT(ac.number(), equals(expectedNumber));
		QVERIFY_THAT(ac.name(), equals(expected));
		QVERIFY_TRUE(ac.parent().hasValue());
		QVERIFY_THAT(*ac.parent(), equals(expectedParent));
	}

	void constructor_numberInit_ShouldBeNumberAndRestDefaults()
	{
		const AccountNumber expectedNumber{1001u};
		const AccountCode ac{expectedNumber};
		QVERIFY_THAT(ac.number(), equals(expectedNumber));
		QVERIFY_TRUE(ac.name().isEmpty());
		QVERIFY_FALSE(ac.parent().hasValue());
	}

	void constructor_fullInit_ShouldBeValues()
	{
		const AccountNumber expectedNumber{1001u};
		const QString expectedName{"Cash"};
		const AccountNumber expectedParent{1010u};
		const AccountCode ac{expectedNumber, expectedName, expectedParent};
		QVERIFY_THAT(ac.number(), equals(expectedNumber));
		QVERIFY_THAT(ac.name(), equals(expectedName));
		QVERIFY_TRUE(ac.parent().hasValue());
		QVERIFY_THAT(*ac.parent(), equals(expectedParent));
	}

	void constructor_invalidNameEmpty_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(AccountCode(AccountNumber{1001}, "   ", {}), AccountError);
	}

	void constructor_invalidNameHasSeparator_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(
			AccountCode(AccountNumber{1001}, "abcd" + AccountCode::separator_ + "efgh", {}),
			AccountError
		);
	}

	void constructor_numberInitNumberSameAsParent_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(
			AccountCode(AccountNumber{1010u}, AccountNumber{1010u}),
			AccountError
		);
	}

	void constructor_fullInitNumberSameAsParent_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(
			AccountCode(AccountNumber{1010u}, "blarg", AccountNumber{1010u}),
			AccountError
		);
	}

	void constructor_nameInitInvalidNameEmpty_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(AccountCode("   "), AccountError);
	}

	void constructor_nameInitInvalidNameHasSeparator_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(
			AccountCode("abcd" + AccountCode::separator_ + "efgh"),
			AccountError
		);
	}

	void constructor_nameInitNumberSameAsParent_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(AccountCode("blarg", AccountNumber{}), AccountError);
	}

	//}

	//{ setNumber Tests

	void setNumber_validParams_ShouldSet()
	{
		AccountCode ac{};
		const AccountNumber expectedNumber{1015u};
		QVERIFY_NO_THROW(ac.setNumber(expectedNumber));
		QVERIFY_THAT(ac.number(), equals(expectedNumber));
		QVERIFY_FALSE(ac.parent().hasValue());
	}

	void setNumber_numberSameAsParent_ShouldThrow()
	{
		const AccountNumber expectedNumber{1015u};
		const AccountNumber expectedParent{1010u};
		AccountCode ac{expectedNumber, expectedParent};
		QVERIFY_EXCEPTION_THROWN(ac.setNumber(expectedParent), AccountError);
	}

	//}

	//{ setName Tests

	void setName_validParam_ShouldBeValue()
	{
		const QString expectedName{"Cash"};
		AccountCode ac{};
		ac.setName(expectedName);
		QVERIFY_THAT(ac, memberFunction(&AccountCode::name, equals(expectedName)));
	}

	void setName_invalidParam_ShouldBeValue()
	{
		const QString expectedName{"   "};
		AccountCode ac{};
		QVERIFY_EXCEPTION_THROWN(ac.setName(expectedName), AccountError);
	}

	//}

	//{ setParent Tests

	void setParent_validParams_ShouldSet()
	{
		const AccountNumber expectedNumber{1015u};
		const AccountNumber expectedParent{1010u};
		AccountCode ac{expectedNumber};
		QVERIFY_NO_THROW(ac.setParent(expectedParent));
		QVERIFY_THAT(ac.number(), equals(expectedNumber));
		QVERIFY_TRUE(ac.parent().hasValue());
		QVERIFY_THAT(*ac.parent(), equals(expectedParent));
	}

	void setParent_parentSameAsNumber_ShouldThrow()
	{
		const AccountNumber expectedNumber{1015u};
		AccountCode ac{expectedNumber};
		QVERIFY_EXCEPTION_THROWN(ac.setParent(expectedNumber), AccountError);
		QVERIFY_THAT(ac.number(), equals(expectedNumber));
		QVERIFY_FALSE(ac.parent().hasValue());
	}

	void setParent_noNumber_ShouldThrow()
	{
		const AccountNumber expectedParent{1010u};
		AccountCode ac{};
		QVERIFY_EXCEPTION_THROWN(ac.setParent(expectedParent), AccountError);
		QVERIFY_TRUE(AccountNumber::invalid_ == ac.number());
		QVERIFY_FALSE(ac.parent().hasValue());
	}

	void setParent_empty_ShouldThrow()
	{
		const AccountNumber expectedParent{};
		AccountCode ac{};
		QVERIFY_EXCEPTION_THROWN(ac.setParent(expectedParent), AccountError);
		QVERIFY_TRUE(AccountNumber::invalid_ == ac.number());
		QVERIFY_FALSE(ac.parent().hasValue());
	}

	//}
};

}}}

QTEST_MAIN(drn::accounting::tests::AccountCodeUT)
#include "AccountCode-unit-tests.moc"
