/***************************************************************************
*   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
*   geiseri@yahoo.com                                                     *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 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 General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "reporttests.h"
#include "block.h"
#include "mindmap.h"
#include "asciireporter.h"
#include "docbookreporter.h"

#include <QtTest/QtTest>
#include <QGraphicsScene>

QTEST_MAIN(ReportTests);

ReportTests::ReportTests(QObject *parent)
		: QObject(parent)
{

}


ReportTests::~ReportTests()
{

}

void ReportTests::testOutlineOrder()
{
	QGraphicsScene scene;
	Block root(0, &scene);
	root.setTitle("Root");
	root.setText("Body content for root 1");

	Block child1( &root, &scene);
	child1.setParentNode(&root);
	child1.setTitle("Child 1");
	child1.setText("Body content for child 1");

	Block child2( &root, &scene);
	child2.setParentNode(&root);
	child2.setTitle("Child 2");
	child2.setText("Body content for child 2");

	Block grandChild1( &child1, &scene);
	grandChild1.setParentNode(&child1);
	grandChild1.setTitle("Grandchild 1");
	grandChild1.setText("Body content for grandchild 1");

	Block grandChild2( &child1, &scene);
	grandChild2.setParentNode(&child1);
	grandChild2.setTitle("Grandchild 2");
	grandChild2.setText("Body content for grandchild 2");

	ASCIIReporter reporter;
	QString doc = reporter.generateReport(&root);

	qDebug() << doc;

}

void ReportTests::testLineWrapping( )
{
	QString doc;
	QString expected =
		"1 ROOT\n"
		"          Body content that will go longer than 80 cols so that it will wrap to\n"
		"     the next line and test line wrapping. This should go past two lines of\n"
		"     wrapping if I have enough text to go for.\n\n";
	QGraphicsScene scene;
	Block root(0, &scene);
	root.setTitle("Root");
	root.setText(
	              "\tBody content that will go longer than 80 cols so that "
	              "it will wrap to the next line and test line wrapping. "
	              "This should go past two lines of wrapping if I have "
	              "enough text to go for.");

	ASCIIReporter reporter;
	doc = reporter.generateReport(&root);

	QCOMPARE( doc, expected );

}

void ReportTests::testLineWrappingBetweenParagraphs( )
{
	QString doc;
	QString expected =
		"1 ROOT\n"
		"          Body content that will go longer than 80 cols so that it will wrap to\n"
		"     the next line and test line wrapping. This should go past two lines of\n"
		"     wrapping if I have enough text to go for.\n\n"
	    "          Body content that will go longer than 80 cols so that it will wrap to\n"
		"     the next line and test line wrapping. This should go past two lines of\n"
		"     wrapping if I have enough text to go for.\n\n";
	QGraphicsScene scene;
	Block root(0, &scene);
	root.setTitle("Root");
	root.setText(
	              "\tBody content that will go longer than 80 cols so that "
	              "it will wrap to the next line and test line wrapping. "
	              "This should go past two lines of wrapping if I have "
	              "enough text to go for.\n\n"
	              "\tBody content that will go longer than 80 cols so that "
	              "it will wrap to the next line and test line wrapping. "
	              "This should go past two lines of wrapping if I have "
	              "enough text to go for."
	            );

	ASCIIReporter reporter;
	doc = reporter.generateReport(&root);

	QCOMPARE( doc, expected );

}

/**
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<article>
<articleinfo>
<author><surname>%{name}</surname></author>
<pubdate>%{current date}</pubdate>
<title>%{document title}</title>
<abstract>
<para>%{document description}</para>
</abstract>
</articleinfo>
<section>
	<title>Like a Sect1</title>
	<para>This section is like a Sect1.</para>
	<section>
		<title>Like a Sect2</title>
		<para>This section is like a Sect2.</para>
		<section>
			<title>Like a Sect3</title>
			<para>This section is like a Sect3.</para>
		</section>
	</section>
</section>
</article>
*/

void generateSection( Block *node, QString *doc )
{
	QString sectionTemplate("%1<section>\n"
	                        "%1 <title>%2</title>\n"
	                        "%1 <para>%3</para>\n");
	QString endSectionTemplate( "%1</section>\n");

	QString padding;
	for( int index = 0; index < node->path().count(); ++index)
		padding += ' ';

	doc->append( sectionTemplate.arg(padding)
	           					.arg(node->title())
								.arg(node->text()));

	foreach( Block *child, node->children() )
		generateSection( child, doc);

	doc->append(endSectionTemplate.arg(padding));
}

void ReportTests::testDocbookSection( )
{
	QString doc;
	QString expected =
		"<section>\n"
		" <title>Root</title>\n"
		" <para>Body content</para>\n"
		"</section>\n";
	QGraphicsScene scene;
	Block root(0, &scene);
	root.setTitle("Root");
	root.setText("Body content");

	generateSection( &root, &doc);

	QCOMPARE( doc, expected );
}

void ReportTests::testDocbookNestedSections( )
{
	QString doc;
	QString expected =
		"<section>\n"
		" <title>Root</title>\n"
		" <para>Body content</para>\n"
		" <section>\n"
		"  <title>Child 1</title>\n"
		"  <para>Body content for child 1</para>\n"
		" </section>\n"
		"</section>\n";

	QGraphicsScene scene;
	Block root(0, &scene);
	root.setTitle("Root");
	root.setText("Body content");

	Block child1( &root, &scene);
	child1.setParentNode(&root);
	child1.setTitle("Child 1");
	child1.setText("Body content for child 1");

	generateSection( &root, &doc);

	QCOMPARE( doc, expected );
}

void ReportTests::testDocbookFullExport( )
{
	MindMap map;
	map.slotNewFile();

	Block child1( map.root(), map.scene() );
	child1.setParentNode(map.root());
	child1.setTitle("Child 1");
	child1.setText("Body content for child 1");

	DocbookReporter report;
	QString reportText = report.generateReport( &map );

	qDebug() << reportText;
}




