/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "monadtest.h"
#include <QtTest>
#include <monad.h>
#include <typelist.h>

QTEST_MAIN (LC::Util::MonadTest)

namespace LC
{
namespace Util
{
	void MonadTest::testBoostOptionalReturn ()
	{
		const auto& pure = Return<boost::optional> (2);
		QCOMPARE (pure, boost::optional<int> { 2 });
	}

	void MonadTest::testBoostOptionalBind ()
	{
		const auto& pure = Return<boost::optional> (2);
		const auto& result = Bind (pure, [] (int value) { return boost::optional<int> { ++value }; });
		QCOMPARE (result, boost::optional<int> { 3 });
	}

	void MonadTest::testBoostOptionalBindEmpty ()
	{
		const auto& result = Bind (boost::optional<int> {}, [] (int value) { return boost::optional<int> { ++value }; });
		QCOMPARE (result, boost::optional<int> {});
	}

	void MonadTest::testBoostOptionalBindOperator ()
	{
		const auto& pure = Return<boost::optional> (2);
		const auto& result = pure >> [] (int value) { return boost::optional<int> { ++value }; };
		QCOMPARE (result, boost::optional<int> { 3 });
	}

	void MonadTest::testBoostOptionalBindEmptyOperator ()
	{
		const auto& result = boost::optional<int> {} >> [] (int value) { return boost::optional<int> { ++value }; };
		QCOMPARE (result, boost::optional<int> {});
	}

	void MonadTest::testBoostOptionalDo ()
	{
		const auto& result = Do (boost::optional<int> { 2 },
				[] (int a) -> boost::optional<int> { return a * 2; },
				[] (int a) -> boost::optional<int> { return a + 1; },
				[] (int a) -> boost::optional<int> { return a * 3; });
		QCOMPARE (result, boost::optional<int> { 15 });
	}

	void MonadTest::testBoostOptionalDoEmpty ()
	{
		bool called = false;
		const auto& result = Do (boost::optional<int> { 2 },
				[] (int a) -> boost::optional<int> { return a * 2; },
				[] (int) -> boost::optional<int> { return {}; },
				[&called] (int a) -> boost::optional<int> { called = true; return a * 3; });

		QCOMPARE (result, boost::optional<int> {});
		QCOMPARE (called, false);
	}

	void MonadTest::testCompatibilitySingle ()
	{
		constexpr auto result = detail::IsCompatibleMonad<
					Typelist<QString>,
					Typelist<QString>
				> ();
		QCOMPARE (result, true);
	}

	void MonadTest::testCompatibilitySingleDif ()
	{
		constexpr auto result = detail::IsCompatibleMonad<
					Typelist<QString>,
					Typelist<QByteArray>
				> ();
		QCOMPARE (result, true);
	}

	void MonadTest::testCompatibilityMulti ()
	{
		constexpr auto result = detail::IsCompatibleMonad<
					Typelist<int, float, QString>,
					Typelist<int, float, QString>
				> ();
		QCOMPARE (result, true);
	}

	void MonadTest::testCompatibilityMultiDifEnd ()
	{
		constexpr auto result = detail::IsCompatibleMonad<
					Typelist<int, float, QString>,
					Typelist<int, float, QByteArray>
				> ();
		QCOMPARE (result, true);
	}

	void MonadTest::testInCompatibilityMulti ()
	{
		constexpr auto result = detail::IsCompatibleMonad<
					Typelist<int, float, QString>,
					Typelist<int, double, QString>
				> ();
		QCOMPARE (result, false);
	}

	void MonadTest::testInCompatibilityMultiStart ()
	{
		constexpr auto result = detail::IsCompatibleMonad<
					Typelist<int, float, QString>,
					Typelist<QByteArray, float, QString>
				> ();
		QCOMPARE (result, false);
	}
}
}
