/*
	Test-ZIniWriter.cpp
	Author: James Russell <jcrussell@762studios.com>

	Purpose: Unit Test the ZIniWriter class.
*/

#include "ZUnitTest.hpp"

#include <ZUtil/ZIniWriter.hpp>

static const char* test_WriteSuccess();
static const char* test_WriteFailure();

//List of unit tests
ZUnitTest ZIniWriterUnitTests[] =
{
	{ "ZIniWriter: WriteSuccess",		test_WriteSuccess },
	{ "ZIniWriter: WriteFailure",		test_WriteFailure }
};

//Now declare the ZUnitTestBlock associated with this.
DECLARE_ZTESTBLOCK(ZIniWriter);

/*************************************************************************/

namespace ZIniWriterTestData
{
	const char* test1Output =	 "[Header1]"			LINE_TERMINATOR
								 "Variable1=Value1"		LINE_TERMINATOR
								 "Variable2=Value2"		LINE_TERMINATOR
								 ""						LINE_TERMINATOR
								 "[Header2]"			LINE_TERMINATOR
								 "Variable1=Value3"		LINE_TERMINATOR
														LINE_TERMINATOR;
	
	const char* test2Output =	 "[Header1]"			LINE_TERMINATOR
								 "Variable1=Value11"	LINE_TERMINATOR
								 "Variable1=Value12"	LINE_TERMINATOR
								 "Variable1=Value13"	LINE_TERMINATOR
								 "Variable2=Value21"	LINE_TERMINATOR
								 "Variable2=Value22"	LINE_TERMINATOR
								 ""						LINE_TERMINATOR
								 "[Header2]"			LINE_TERMINATOR
								 "Variable1=Value31"	LINE_TERMINATOR
								 "Variable1=Value32"	LINE_TERMINATOR
														LINE_TERMINATOR;
}

using namespace ZIniWriterTestData;

/*************************************************************************/

static const char* test_WriteSuccess()
{
	ZKVTree tree1;

	tree1.Put("Header1.Variable1", "Value1");
	tree1.Put("Header1.Variable2", "Value2");

	tree1.Put("Header2.Variable1", "Value3");

	ZIniWriter writer;
	ZString output;
	
	TASSERT(writer.Write(tree1, output), "ZIniWriter failed to write valid data set 1!");

	TASSERT(output == test1Output, "ZIniWriter has written invalid output for data set 1!");

	ZKVTree tree2;

	tree2.Add("Header1", "Variable1", "Value11");
	tree2.Add("Header1", "Variable1", "Value12");
	tree2.Add("Header1", "Variable1", "Value13");
	tree2.Add("Header1", "Variable2", "Value21");
	tree2.Add("Header1", "Variable2", "Value22");

	tree2.Add("Header2", "Variable1", "Value31");
	tree2.Add("Header2", "Variable1", "Value32");
	
	output.Clear();

	TASSERT(writer.Write(tree2, output), "ZIniWriter failed to write valid data set 2!");

	TASSERT(output == test2Output, "ZIniWriter has written invalid output for data set 2!");

	return ZTEST_SUCCESS;
}

/*************************************************************************/

static const char* test_WriteFailure()
{
	//Currently, only one way to fail
	//  Empty Registry (no sections)

	ZKVTree tree;
	ZIniWriter writer;
	ZString output;

	TASSERT(!writer.Write(tree, output), "ZIniWriter wrote data with empty kvtree!");

	return ZTEST_SUCCESS;
}