
t2t = new tedi2tex();

QUnit.module('tedi2tex', function() {
	QUnit.test('convert initial line text', function(assert) {

		//Empty string
		console.log("EMPTY STRINGS");

		assert.equal((t2t.convert("", "", "")), (""));
		assert.equal(t2t.convert(" ", "", ""), (" \n\n"));
		assert.equal(t2t.convert("\n\n\n", "", ""), ("\n\n\n"));
	});

	QUnit.test('titles', function(assert) {
		//Titles
		console.log("TITLES");
		try{
			t2t.convert("#####", "", "");
		}catch(err) {
			assert.equal(err, "Unexpected heading size#####");
		}

		assert.equal(t2t.convert("paragraph\n#This isn't a title", "", ""), ("paragraph\n\\section{This isn't a title}\n"));

		assert.equal(t2t.convert("#This isn't a title", "", ""), ("\\section{This isn't a title}\n"));
		assert.equal(t2t.convert(" #This isn't a title", "", ""), ("\\section{This isn't a title}\n"));
		assert.equal(t2t.convert("\t##This isn't a title", "", ""), ("\\subsection{This isn't a title}\n"));

		assert.equal(t2t.convert("##This isn't a title", "", ""), ("\\subsection{This isn't a title}\n"));
		assert.equal(t2t.convert("###This isn't a title", "", ""), ("\\subsubsection{This isn't a title}\n"));
		assert.equal(t2t.convert("####This isn't a title", "", ""), ("\\subsubsection{This isn't a title}\n"));
		assert.equal(t2t.convert("#####This isn't a title", "", ""), "\\subsubsection{This isn't a title}\n" );
		assert.equal(t2t.convert("#####This isn't a title", "", ""), "\\subsubsection{This isn't a title}\n" );
	});
	QUnit.test('list', function(assert) {
		console.log("LIST");

		assert.equal(t2t.convert("__", "", ""), ("\\begin{itemize}\n"));
		assert.equal(t2t.convert("--Text", "", ""), ("--Text\n"));
		assert.equal(t2t.convert(",,", "", ""), (",,\n"));
		assert.equal(t2t.convert("__\n--Text\n,,", "", ""), ("\\begin{itemize}\n\\item Text\n\\end{itemize}\n"));
	});
	QUnit.test('quotes', function(assert) {
		console.log("QUOTE");

		assert.equal(t2t.convert("\"Quoted text\"", "", ""), ("Quoted text\n"));

		try{
			t2t.convert("\"Bad quoting", "", "")
		}catch(err) {
			assert.equal(err, "Missing end quotes.\"Bad quoting");
		}
	});
	QUnit.test('links', function(assert) {
		console.log("LINKS");

		assert.equal(t2t.convert("[Test with a open square bracket", "", ""), ("[Test with a open square bracket\n"));
		try{
			t2t.convert("[(Test with a open square bracket and parenthesis", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in link tag.\\href{Test with a open square bracket and parenthesis");
		}
		
		try{
			t2t.convert("[(Test without closing square bracket)", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in link tag.\\href{Test without closing square bracket)");
		}

		try{
			t2t.convert("[(Test with closing square bracket)]", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in link tag.\\href{Test with closing square bracket)]");
		}
		
		assert.equal(t2t.convert("[(Test with closing square bracket) ]", "", ""), "\\href{Test with closing square bracket}{}\n");

		assert.equal(t2t.convert("[() ]", "", ""), "\\href{}{}\n");

		assert.equal(t2t.convert("[( ) ]", "", ""), "\\href{ }{}\n");

		try{
			t2t.convert("[(test\\) test with escaped char\\) ]", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in link tag.\\href{test\\) test with escaped char\\) ]");
		}

		assert.equal(t2t.convert("[(test\\) test with escaped char) ]", "", ""), "\\href{test) test with escaped char}{}\n");
		assert.equal(t2t.convert("[(test\\) test with escaped char) \\]sasdfasd]", "", ""), "\\href{test) test with escaped char}{]sasdfasd}\n");
		assert.equal(t2t.convert("[(test\\( test with escaped char) \\]sasdfasd]", "", ""), "\\href{test( test with escaped char}{]sasdfasd}\n");

		assert.equal(t2t.convert("\\([Test with closing square bracket) text", "", ""), "([Test with closing square bracket) text\n");
		assert.equal(t2t.convert("\\([ ([Test with closing square bracket] text)", "", ""), 
`([ \\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{text}
\\caption{Test with closing square bracket}
\\end{figure}\n`);
	});
	QUnit.test('images', function(assert) {
		console.log("IMAGES");

		assert.equal(t2t.convert("(Test with an open parenthesis", "", ""), ("(Test with an open parenthesis\n"));
		
		try{
			t2t.convert("([Test with a open square bracket and parenthesis", "", "");
		}catch(err){
			assert.equal(err, "Missing ']' in images tag.Test with a open square bracket and parenthesis");
		}
		try{
			t2t.convert("([Test without closing square bracket]", "", "");
		}catch(err){
			assert.equal(err, "Missing ']' in images tag.Test without closing square bracket]");
		}
		try{
			t2t.convert("([Test with closing square bracket])", "", "");
		}catch(err){
			assert.equal(err, "Missing ']' in images tag.Test with closing square bracket])");
		}

		assert.equal(t2t.convert("([Test with closing square bracket] )", "", ""), `\\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{}
\\caption{Test with closing square bracket}
\\end{figure}\n`);
		assert.equal(t2t.convert("([] )", "", ""), `\\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{}
\\caption{}
\\end{figure}\n`);
		assert.equal(t2t.convert("([ ] )", "", ""), `\\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{}
\\caption{ }
\\end{figure}\n`);

		try{
			t2t.convert("([test\\] test with escaped char\\] )", "", "");
		}catch(err){
			assert.equal(err, "Missing ']' in images tag.test\\] test with escaped char\\] )");
		}

		assert.equal(t2t.convert("([test\\] test with escaped char] )", "", ""), `\\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{}
\\caption{test] test with escaped char}
\\end{figure}\n`);
		assert.equal(t2t.convert("([test\\] test with escaped char] \\)sasdfasd)", "", ""), `\\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{)sasdfasd}
\\caption{test] test with escaped char}
\\end{figure}\n`);

		assert.equal(t2t.convert("\\([Test with closing square bracket) text", "", ""), "([Test with closing square bracket) text\n");
		assert.equal(t2t.convert("\\([ ([Test with closing square bracket] text)", "", ""), `([ \\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{text}
\\caption{Test with closing square bracket}
\\end{figure}\n`);
	});

	QUnit.test('blocks', function(assert) {
		console.log("BLOCKS");

		assert.equal(t2t.convert("{Test with an open parenthesis", "", ""), ("{Test with an open parenthesis\n"));
		try{
			t2t.convert("{(Test with a open square bracket and parenthesis", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in block tag.Test with a open square bracket and parenthesis");
		}
		try{
			t2t.convert("{(Test without closing square bracket)", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in block tag.Test without closing square bracket)");
		}
		try{
			t2t.convert("{(Test with closing square bracket)}", "", "")
		}catch(err){
			assert.equal(err, "Missing ')' in block tag.Test with closing square bracket)}");
		}

		assert.equal(t2t.convert("{(Test with closing square bracket) }", "", ""), "\n");
		assert.equal(t2t.convert("{() }", "", ""), "\n");
		assert.equal(t2t.convert("{( ) }", "", ""), "\n");

		try{
			t2t.convert("{(test\\) test with escaped char\\) }", "", "")
		}catch(err){
			assert.equal(err, "Missing ')' in block tag.test\\) test with escaped char\\) }");
		}

		assert.equal(t2t.convert("{(test\\) test with escaped char) }", "", ""), "\n");
		
		assert.equal(t2t.convert("{(test\\) test with escaped char) \\}sasdfasd}", "", ""), "}sasdfasd\n");
		assert.equal(t2t.convert("\\{(Test with closing square bracket) text", "", ""), "{(Test with closing square bracket) text\n");
		assert.equal(t2t.convert("\\{( {(Test with closing square bracket) text}", "", ""), "{( text\n");
		
		assert.equal(t2t.convert(" {() #This isn't a title}", "", ""), (" #This isn't a title\n"));
		assert.equal(t2t.convert(" {(class) #This isn't a title}", "", ""), (" #This isn't a title\n"));
		assert.equal(t2t.convert(" {(class) \n #This is a title \n }", "", ""), (" \n\n\\section{This is a title }\n \n\n"));
		try{
			t2t.convert(" {(class)#This isn't a title", "", "")
		}catch(err){
			assert.equal(err, "Missing ')' in block tag. class)#This isn't a title");
		}
	});

	QUnit.test('multiples unquoted tags', function(assert) {
		console.log("MULTIPLES UNQUOTED TAGS");

		assert.equal(t2t.convert("[Test with a open square bracket ( also parenthesis and { brackets", "", ""), ("[Test with a open square bracket ( also parenthesis and { brackets\n"));
		assert.equal(t2t.convert("[(url) Test with a link ([alternative text] image url)] and {(class) brackets}", "", ""), (`\\href{url}{Test with a link \\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{image url}
\\caption{alternative text}
\\end{figure}} and brackets\n`));
		assert.equal(t2t.convert("{(class) [(url) Test with a link ([alternative text] image url)] and brackets}", "", ""), (`\\href{url}{Test with a link \\begin{figure}[h!]
\\centering
\\includegraphics[width=1\\textwidth]{image url}
\\caption{alternative text}
\\end{figure}} and brackets\n`));
	});

	QUnit.test('table', function(assert) {
		console.log("TABLE");

		assert.equal(t2t.convert("| first cell | second cell |", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}\n`);
		assert.equal(t2t.convert("| first cell ", "", ""), "| first cell \n\n");

		try{
			t2t.convert("| first cell | second cell ", "", "");
		}catch(err){
			assert.equal(err, "Table not correctly written\nMaybe there's a whitespace at end of line first cell | second cell ");
		}
		assert.equal(t2t.convert("| first cell | second cell |\n| third cell | fourth cell |", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}\n`);
		assert.equal(t2t.convert("| first cell |\n| second cell | third cell |", "", ""), `\\begin{longtable}{|p{0.9\\linewidth}|}
\\hline
 first cell \\tabularnewline
\\hline
 second cell  &  third cell \\tabularnewline
\\hline
\\end{longtable}\n`);
		assert.equal(t2t.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}\n`);
		assert.equal(t2t.convert("Text adhoc for testing\n| a | b |\n| c | d |\n", "", ""), `Text adhoc for testing
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 a  &  b \\tabularnewline
\\hline
 c  &  d \\tabularnewline
\\hline
\\end{longtable}\n`);
		assert.equal(t2t.convert("Text adhoc for testing\n| a | b |\n| c | d |\nText adhoc for testing\n", "", ""), `Text adhoc for testing
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 a  &  b \\tabularnewline
\\hline
 c  &  d \\tabularnewline
\\hline
\\end{longtable}
Text adhoc for testing\n`);


	});
	
	QUnit.test('end_table', function(assert) {

		console.log("END TABLE");

		assert.equal(t2t.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n#Title1", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\section{Title1}\n`);
		assert.equal(t2t.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n__\n--Item\n,,", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\begin{itemize}
\\item Item
\\end{itemize}\n`);
		assert.equal(t2t.convert("__\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n--Item\n,,", "", ""), `\\begin{itemize}
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\item Item
\\end{itemize}\n`);
		assert.equal(t2t.convert("__\n--Item\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n,,", "", ""), `\\begin{itemize}
\\item Item
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\end{itemize}\n`);
		assert.equal(t2t.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<Test\n", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\verb!Test!\n`);
		assert.equal(t2t.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<+Test\n", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\begin{verbatim}
Test
\\end{verbatim}\n\n`);
		assert.equal(t2t.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<+Test with pipe |\n", "", ""), `\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 first cell  &  second cell \\tabularnewline
\\hline
\\end{longtable}
Text line
\\begin{longtable}{|p{0.45\\linewidth}|p{0.45\\linewidth}|}
\\hline
 third cell  &  fourth cell \\tabularnewline
\\hline
\\end{longtable}
\\begin{verbatim}
Test with pipe |
\\end{verbatim}\n\n`);
	});

	QUnit.test('new line', function(assert) {
		console.log("NEW LINE");

		assert.equal(t2t.convert("Lorem Ipsum \n ", "", ""), ("Lorem Ipsum \n\n \n\n"));
		assert.equal(t2t.convert("First line\nLorem Ipsum \n ", "", ""), ("First line\nLorem Ipsum \n\n \n\n"));
		assert.equal(t2t.convert("First line \nLorem Ipsum \n ", "", ""), ("First line \n\nLorem Ipsum \n\n \n\n"));
		assert.equal(t2t.convert("Lorem{(class) Ipsum} \n", "", ""), ("LoremIpsum \n\n"));

		assert.equal(t2t.convert("Lorem\nIpsum\nDolor\n", "", ""), ("Lorem\nIpsum\nDolor\n"));
		assert.equal(t2t.convert("{(class) text}\n", "", ""), ("text\n"));
		assert.equal(t2t.convert("Lorem\n{(class) text}\nDolor\n", "", ""), ("Lorem\ntext\nDolor\n"));
	});

	QUnit.test('metatags', function(assert) {
		console.log("METATAGS");

		assert.equal(t2t.convert("<! Comment commenting things", "", ""), "");

		assert.equal(t2t.convert("Text adhoc for testing\n< metatag without space at end\nText adhoc for testing", "", ""), "Text adhoc for testing\n\\verb! metatag without space at end!\nText adhoc for testing\n");
		assert.equal(t2t.convert("Text adhoc for testing \n< metatag without space at end\nText adhoc for testing", "", ""), "Text adhoc for testing \n\n\\verb! metatag without space at end!\nText adhoc for testing\n");
		assert.equal(t2t.convert("Text adhoc for testing\n< metatag without space at end\nText adhoc for testing ", "", ""), "Text adhoc for testing\n\\verb! metatag without space at end!\nText adhoc for testing \n\n");

		assert.equal(t2t.convert("Text adhoc for testing\n<+ metatag with space at end\nText adhoc for testing", "", ""), `Text adhoc for testing
\\begin{verbatim}
 metatag with space at end
\\end{verbatim}

Text adhoc for testing\n`);

		assert.equal(t2t.convert("Text adhoc for testing \n<+ metatag with space at end\nText adhoc for testing", "", ""), `Text adhoc for testing 

\\begin{verbatim}
 metatag with space at end
\\end{verbatim}

Text adhoc for testing\n`);
		assert.equal(t2t.convert("Text adhoc for testing\n<+ metatag with space at end\nText adhoc for testing ", "", ""), `Text adhoc for testing
\\begin{verbatim}
 metatag with space at end
\\end{verbatim}

Text adhoc for testing \n\n`);

		assert.equal(t2t.convert("Text adhoc for testing\n<> embed with space at end\nText adhoc for testing ", "", ""), "Text adhoc for testing\n embed with space at end\nText adhoc for testing \n\n");
	});
	QUnit.test('misc.', function(assert) {
		console.log("MISC.");

		assert.equal(t2t.convert("alkasdfasdf \n#This isn't a title\nksadjfañlskdfj", "", "")
			, ("alkasdfasdf \n\n\\section{This isn't a title}\nksadjfañlskdfj\n"));

		assert.equal(t2t.convert("alkasdfasdf\n#This isn't a title\nksadjfañlskdfj", "", "")
			, "alkasdfasdf\n\\section{This isn't a title}\nksadjfañlskdfj\n");

		assert.equal(t2t.convert("Text adhoc for testing\n{(class) container text } Text adhoc for testing ", "", ""), "Text adhoc for testing\ncontainer text  Text adhoc for testing \n\n");
		assert.equal(t2t.convert("Text adhoc for testing \n{(class) container text }\nText adhoc for testing ", "", ""), "Text adhoc for testing \n\ncontainer text \n\nText adhoc for testing \n\n");
		assert.equal(t2t.convert("Text adhoc for testing \n{(class) container text}Text adhoc for testing ", "", ""), "Text adhoc for testing \n\ncontainer textText adhoc for testing \n\n");

		assert.equal(t2t.convert("Paragraph without space at end\n< hello ", "", ""), "Paragraph without space at end\n\\verb! hello !\n");
		assert.equal(t2t.convert("Paragraph without space at end\n< hello without space at end\n lsdkjfgsñdlkfjñlk", "", ""), "Paragraph without space at end\n\\verb! hello without space at end!\n lsdkjfgsñdlkfjñlk\n");
		assert.equal(t2t.convert("Paragraph without space at end\n< hello ", "", ""), "Paragraph without space at end\n\\verb! hello !\n");

	});

  });

  

