
t2l = new tedi2html();

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

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

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

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

		assert.equal(t2l.convert("paragraph \n#This isn't a title", "", ""), ("paragraph \n<h1>This isn't a title</h1>\n"));

		assert.equal(t2l.convert("#This isn't a title", "", ""), ("<h1>This isn't a title</h1>\n"));
		assert.equal(t2l.convert(" #This isn't a title", "", ""), ("<h1>This isn't a title</h1>\n"));
		assert.equal(t2l.convert("\t##This isn't a title", "", ""), ("<h2>This isn't a title</h2>\n"));

		assert.equal(t2l.convert("##This isn't a title", "", ""), ("<h2>This isn't a title</h2>\n"));
		assert.equal(t2l.convert("###This isn't a title", "", ""), ("<h3>This isn't a title</h3>\n"));
		assert.equal(t2l.convert("####This isn't a title", "", ""), ("<h4>This isn't a title</h4>\n"));
		assert.equal(t2l.convert("#####This isn't a title", "", ""), "<h4>This isn't a title</h4>\n" );
		assert.equal(t2l.convert("#####This isn't a title", "", ""), "<h4>This isn't a title</h4>\n" );
	});
	QUnit.test('list', function(assert) {
		console.log("LIST");

		assert.equal(t2l.convert("__", "", ""), ("<ul>\n"));
		assert.equal(t2l.convert("--Text", "", ""), ("--Text"));
		assert.equal(t2l.convert(",,", "", ""), (",,"));
		assert.equal(t2l.convert("__\n--Text\n,,", "", ""), ("<ul>\n<li>Text</ul>\n"));
	});
	QUnit.test('quotes', function(assert) {
		console.log("QUOTE");

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

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

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

		try{
			t2l.convert("[(Test with closing square bracket)]", "", "");
		}catch(err){
			assert.equal(err, "Missing ')' in link tag.<a href=\"Test with closing square bracket)]");
		}
		
		assert.equal(t2l.convert("[(Test with closing square bracket) ]", "", ""), "<a href=\"Test with closing square bracket\"></a>");

		assert.equal(t2l.convert("[() ]", "", ""), "<a href=\"\"></a>");

		assert.equal(t2l.convert("[( ) ]", "", ""), "<a href=\" \"></a>");

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

		assert.equal(t2l.convert("[(test\\) test with escaped char) ]", "", ""), "<a href=\"test) test with escaped char\"></a>");
		assert.equal(t2l.convert("[(test\\) test with escaped char) \\]sasdfasd]", "", ""), "<a href=\"test) test with escaped char\">]sasdfasd</a>");
		assert.equal(t2l.convert("[(test\\( test with escaped char) \\]sasdfasd]", "", ""), "<a href=\"test( test with escaped char\">]sasdfasd</a>");

		assert.equal(t2l.convert("\\([Test with closing square bracket) text", "", ""), "([Test with closing square bracket) text");
		assert.equal(t2l.convert("\\([ ([Test with closing square bracket] text)", "", ""), "([ <img alt=\"Test with closing square bracket\" src=\"text\">");
	});
	QUnit.test('images', function(assert) {
		console.log("IMAGES");

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

		assert.equal(t2l.convert("([Test with closing square bracket] )", "", ""), "<img alt=\"Test with closing square bracket\" src=\"\">");
		assert.equal(t2l.convert("([] )", "", ""), "<img alt=\"\" src=\"\">");
		assert.equal(t2l.convert("([ ] )", "", ""), "<img alt=\" \" src=\"\">");

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

		assert.equal(t2l.convert("([test\\] test with escaped char] )", "", ""), "<img alt=\"test] test with escaped char\" src=\"\">");
		assert.equal(t2l.convert("([test\\] test with escaped char] \\)sasdfasd)", "", ""), "<img alt=\"test] test with escaped char\" src=\")sasdfasd\">");

		assert.equal(t2l.convert("\\([Test with closing square bracket) text", "", ""), "([Test with closing square bracket) text");
		assert.equal(t2l.convert("\\([ ([Test with closing square bracket] text)", "", ""), "([ <img alt=\"Test with closing square bracket\" src=\"text\">");
	});

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

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

		assert.equal(t2l.convert("{(Test with closing square bracket) }", "", ""), "<div class=\"Test with closing square bracket\"></div>");
		assert.equal(t2l.convert("{() }", "", ""), "<div class=\"\"></div>");
		assert.equal(t2l.convert("{( ) }", "", ""), "<div class=\" \"></div>");

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

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

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

		assert.equal(t2l.convert("[Test with a open square bracket ( also parenthesis and { brackets", "", ""), ("[Test with a open square bracket ( also parenthesis and { brackets"));
		assert.equal(t2l.convert("[(url) Test with a link ([alternative text] image url)] and {(class) brackets}", "", ""), ("<a href=\"url\">Test with a link <img alt=\"alternative text\" src=\"image url\"></a> and <div class=\"class\">brackets</div>"));
		assert.equal(t2l.convert("{(class) [(url) Test with a link ([alternative text] image url)] and brackets}", "", ""), ("<div class=\"class\"><a href=\"url\">Test with a link <img alt=\"alternative text\" src=\"image url\"></a> and brackets</div>"));
	});

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

		assert.equal(t2l.convert("| first cell | second cell |", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\n");
		assert.equal(t2l.convert("| first cell ", "", ""), "| first cell \n");

		try{
			t2l.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(t2l.convert("| first cell | second cell |\n| third cell | fourth cell |", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr><tr><td> third cell </td><td> fourth cell </td></tr></table>\n");
		assert.equal(t2l.convert("| first cell |\n| second cell | third cell |", "", ""), "<table><tr><td> first cell </td></tr><tr><td> second cell </td><td> third cell </td></tr></table>\n");
		assert.equal(t2l.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n");
		assert.equal(t2l.convert("Text adhoc for testing\n| a | b |\n| c | d |\n", "", ""), "Text adhoc for testing<table><tr><td> a </td><td> b </td></tr><tr><td> c </td><td> d </td></tr></table>\n");
		assert.equal(t2l.convert("Text adhoc for testing\n| a | b |\n| c | d |\nText adhoc for testing\n", "", ""), "Text adhoc for testing<table><tr><td> a </td><td> b </td></tr><tr><td> c </td><td> d </td></tr></table>\nText adhoc for testing");


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

		console.log("END TABLE");

		assert.equal(t2l.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n#Title1", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n<h1>Title1</h1>\n");
		assert.equal(t2l.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n__\n--Item\n,,", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n<ul>\n<li>Item</ul>\n");
		assert.equal(t2l.convert("__\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n--Item\n,,", "", ""), "<ul>\n<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n<li>Item</ul>\n");
		assert.equal(t2l.convert("__\n--Item\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n,,", "", ""), "<ul>\n<li>Item<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n</ul>\n");
		assert.equal(t2l.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<Test\n", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n<code>Test</code>");
		assert.equal(t2l.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<+Test\n", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n<pre>Test</pre>\n");
		assert.equal(t2l.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<+Test with pipe |\n", "", ""), "<table><tr><td> first cell </td><td> second cell </td></tr></table>\nText line<table><tr><td> third cell </td><td> fourth cell </td></tr></table>\n<pre>Test with pipe |</pre>\n");
	});

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

		assert.equal(t2l.convert("Lorem Ipsum \n ", "", ""), ("Lorem Ipsum \n \n"));
		assert.equal(t2l.convert("First line\nLorem Ipsum \n ", "", ""), ("First lineLorem Ipsum \n \n"));
		assert.equal(t2l.convert("First line \nLorem Ipsum \n ", "", ""), ("First line \nLorem Ipsum \n \n"));
		assert.equal(t2l.convert("Lorem{(class) Ipsum} \n", "", ""), ("Lorem<div class=\"class\">Ipsum</div>\n"));

		assert.equal(t2l.convert("Lorem\nIpsum\nDolor\n", "", ""), ("LoremIpsumDolor"));
		assert.equal(t2l.convert("{(class) text}\n", "", ""), ("<div class=\"class\">text</div>"));
		assert.equal(t2l.convert("Lorem\n{(class) text}\nDolor\n", "", ""), ("Lorem<div class=\"class\">text</div>Dolor"));
	});

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

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

		assert.equal(t2l.convert("Text adhoc for testing\n< metatag without space at end\nText adhoc for testing", "", ""), "Text adhoc for testing<code> metatag without space at end</code>Text adhoc for testing");
		assert.equal(t2l.convert("Text adhoc for testing \n< metatag without space at end\nText adhoc for testing", "", ""), "Text adhoc for testing \n<code> metatag without space at end</code>Text adhoc for testing");
		assert.equal(t2l.convert("Text adhoc for testing\n< metatag without space at end\nText adhoc for testing ", "", ""), "Text adhoc for testing<code> metatag without space at end</code>Text adhoc for testing \n");

		assert.equal(t2l.convert("Text adhoc for testing\n<+ metatag with space at end\nText adhoc for testing", "", ""), "Text adhoc for testing<pre> metatag with space at end</pre>\nText adhoc for testing");

		assert.equal(t2l.convert("Text adhoc for testing \n<+ metatag with space at end\nText adhoc for testing", "", ""), "Text adhoc for testing \n<pre> metatag with space at end</pre>\nText adhoc for testing");
		assert.equal(t2l.convert("Text adhoc for testing\n<+ metatag with space at end\nText adhoc for testing ", "", ""), "Text adhoc for testing<pre> metatag with space at end</pre>\nText adhoc for testing \n");

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

		assert.equal(t2l.convert("alkasdfasdf \n#This isn't a title\nksadjfañlskdfj", "", "")
			, ("alkasdfasdf \n<h1>This isn't a title</h1>\nksadjfañlskdfj"));

		assert.equal(t2l.convert("alkasdfasdf\n#This isn't a title\nksadjfañlskdfj", "", "")
			, "alkasdfasdf<h1>This isn't a title</h1>\nksadjfañlskdfj");

		assert.equal(t2l.convert("Text adhoc for testing\n{(class) container text } Text adhoc for testing ", "", ""), "Text adhoc for testing<div class=\"class\">container text </div> Text adhoc for testing\n");
		assert.equal(t2l.convert("Text adhoc for testing \n{(class) container text }\nText adhoc for testing ", "", ""), "Text adhoc for testing \n<div class=\"class\">container text </div>Text adhoc for testing \n");
		assert.equal(t2l.convert("Text adhoc for testing \n{(class) container text}Text adhoc for testing ", "", ""), "Text adhoc for testing \n<div class=\"class\">container text</div>Text adhoc for testing\n");

		assert.equal(t2l.convert("Paragraph without space at end\n< hello ", "", ""), "Paragraph without space at end<code> hello </code>");
		assert.equal(t2l.convert("Paragraph without space at end\n< hello without space at end\n lsdkjfgsñdlkfjñlk", "", ""), "Paragraph without space at end<code> hello without space at end</code> lsdkjfgsñdlkfjñlk");
		assert.equal(t2l.convert("Paragraph without space at end\n< hello ", "", ""), "Paragraph without space at end<code> hello </code>");

	});

  });

  

