/**********************************************************************
 * 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 "eithertest.h"
#include <QtTest>
#include <either.h>
#include <curry.h>
#include <void.h>

QTEST_MAIN (LC::Util::EitherTest)

namespace LC
{
namespace Util
{
	using SomeEither_t = Either<int, QString>;

	void EitherTest::testBasicLeft ()
	{
		const auto& left = SomeEither_t::Left (1);
		QCOMPARE (left.IsLeft (), true);
		QCOMPARE (left.IsRight (), false);
		QCOMPARE (left.GetLeft (), 1);

		bool hadCaught = false;
		try
		{
			left.GetRight ();
		}
		catch (const std::exception&)
		{
			hadCaught = true;
		}
		QCOMPARE (hadCaught, true);
	}

	void EitherTest::testBasicRight ()
	{
		const auto& right = SomeEither_t::Right ("foo");
		QCOMPARE (right.IsLeft (), false);
		QCOMPARE (right.IsRight (), true);
		QCOMPARE (right.GetRight (), QString { "foo" });

		bool hadCaught = false;
		try
		{
			right.GetLeft ();
		}
		catch (const std::exception&)
		{
			hadCaught = true;
		}
		QCOMPARE (hadCaught, true);
	}

	void EitherTest::testFMapLeft ()
	{
		const auto& left = SomeEither_t::Left (1);
		const auto& fmapped = Fmap (left, [] (const QString& str) { return str + "_mapped"; });
		QCOMPARE (fmapped, left);
	}

	void EitherTest::testFMapRight ()
	{
		const auto& right = SomeEither_t::Right ("foo");
		const auto& fmapped = Fmap (right, [] (const QString& str) { return str + "_mapped"; });
		QCOMPARE (fmapped.GetRight (), QString { "foo_mapped" });
	}

	void EitherTest::testFMapRightChangeType ()
	{
		const auto& right = SomeEither_t::Right ("foo");
		const auto& fmapped = Fmap (right, [] (const QString& str) { return static_cast<long> (str.size ()); });
		QCOMPARE (fmapped.GetRight (), static_cast<long> (right.GetRight ().size ()));
	}

	void EitherTest::testPure ()
	{
		const auto& pure = Pure<Either, int> (QString { "foo" });
		QCOMPARE (pure, SomeEither_t::Right ("foo"));
	}

	void EitherTest::testGSL ()
	{
		const auto& pure = Pure<Either, int> ([] (const QString& s) { return s + "_pure"; });
		const auto& app = pure * Pure<Either, int> (QString { "foo" });
		QCOMPARE (app, SomeEither_t::Right ("foo_pure"));
	}

	void EitherTest::testGSLLeft ()
	{
		const auto& pure = Pure<Either, int> ([] (const QString& s) { return s + "_pure"; });
		const auto& value = SomeEither_t::Left (2);
		const auto& app = pure * value;
		QCOMPARE (app, value);
	}

	void EitherTest::testGSLCurry ()
	{
		const auto& summer = Pure<Either, int> (Curry ([] (const QString& a, const QString& b) { return a + b; }));
		const auto& s1 = Pure<Either, int> (QString { "foo" });
		const auto& s2 = Pure<Either, int> (QString { "bar" });
		const auto& app = summer * s1 * s2;
		QCOMPARE (app, SomeEither_t::Right ("foobar"));
	}

	void EitherTest::testGSLCurryLeft ()
	{
		const auto& summer = Pure<Either, int> (Curry ([] (const QString& a, const QString& b) { return a + b; }));
		const auto& s1 = SomeEither_t::Left (2);
		const auto& s2 = Pure<Either, int> (QString { "bar" });
		const auto& app = summer * s1 * s2;
		QCOMPARE (app, s1);
	}

	void EitherTest::testBind ()
	{
		const auto& res = Return<Either, int> (QString { "foo" }) >>
				[] (const QString& right) { return SomeEither_t::Right (right + "_bound"); };
		QCOMPARE (res, SomeEither_t::Right ("foo_bound"));
	}

	void EitherTest::testBindLeft ()
	{
		const auto& value = SomeEither_t::Left (2);
		const auto& res = value >>
				[] (const QString& right) { return SomeEither_t::Right (right + "_bound"); };
		QCOMPARE (res, value);
	}

	struct NoDefaultCtor
	{
		NoDefaultCtor () = delete;
		NoDefaultCtor (const QString&)
		{
		}

		bool operator== (const NoDefaultCtor&) const
		{
			return true;
		}
	};

	void EitherTest::testBindLeftNotConstructed ()
	{
		const auto& value = Either<NoDefaultCtor, Void>::Right ({});
		const auto& expected = Either<NoDefaultCtor, int>::Right (5);
		const auto res = value >>
				[&expected] (const auto&) { return expected; };
		QCOMPARE (res, expected);
	}
}
}
