"""
test.py
-------

Unit testing for MarkdownPP.

"""

import unittest
import os
import re
from MarkdownPP import MarkdownPP
from MarkdownPP import modules as Modules
from io import StringIO


class MarkdownPPTests(unittest.TestCase):

    def test_include(self):
        input = StringIO('foobar\n!INCLUDE "test_include.md"\n')
        result = 'foobar\nThis is a test.\n'

        output = StringIO()
        MarkdownPP(input=input, modules=['include'], output=output)

        output.seek(0)
        self.assertEqual(output.read(), result)

    def test_include_url(self):
        path = os.path.join(os.getcwd(), "test_include.md")
        input = StringIO('foobar\n!INCLUDEURL "file://{}"\n'.format(path))
        result = 'foobar\nThis is a test.\n'

        output = StringIO()
        MarkdownPP(input=input, modules=['includeurl'], output=output)

        output.seek(0)
        self.assertEqual(output.read(), result)

    def test_youtube(self):
        input = StringIO('foobar\n!VIDEO "http://www.youtube.com/embed/7aEYoP5-duY"\n')
        result = 'foobar\n[![Link to Youtube video](images/youtube/7aEYoP5-duY.png)](http://www.youtube.com/watch?v=7aEYoP5-duY)\n'

        output = StringIO()
        MarkdownPP(input=input, modules=['youtubeembed'], output=output)

        output.seek(0)
        self.assertEqual(output.read(), result)

    def test_toc(self):
        input = StringIO('\n# Document Title\n\n'
                         '!TOC\n\n'
                         '## Header 1\n'
                         '### Header 1.a\n'
                         '## Header 2\n')

        result = """
        # Document Title

        1\.  [Header 1](#header1)
        1.1\.  [Header 1.a](#header1.a)
        2\.  [Header 2](#header2)

        <a name="header1"></a>

        ## 1\. Header 1
        <a name="header1.a"></a>

        ### 1.1\. Header 1.a
        <a name="header2"></a>

        ## 2\. Header 2"""

        output = StringIO()
        MarkdownPP(input=input, modules=['tableofcontents'], output=output)

        output.seek(0)
        self.assertEqual([l.strip() for l in output.readlines()],
                         [l.strip() for l in result.split('\n')])

    def test_reference(self):
        input = StringIO('\n!REF\n\n[github]: http://github.com "GitHub"')
        result = '\n*\t[GitHub][github]\n\n[github]: http://github.com "GitHub"'

        output = StringIO()
        MarkdownPP(input=input, modules=['reference'], output=output)

        output.seek(0)
        self.assertEqual(output.read(), result)

    def test_latexrender(self):
        input = StringIO('$\displaystyle 1 + 1 = 2 $')
        result_re = r'!\[\\displaystyle 1 \+ 1 = 2 \]\(http:\/\/quicklatex\.com\/.*\.png "\\displaystyle 1 \+ 1 = 2 "\)'

        output = StringIO()
        MarkdownPP(input=input, modules=['latexrender'], output=output)

        output.seek(0)
        output_str = output.read().strip()
        match = re.match(result_re, output_str)
        self.assertIsNotNone(match)
        self.assertEqual(match.span(), (0, len(output_str)))

    def test_file(self):
        with open('../readme.mdpp', 'r') as mdpp, open('../readme.md', 'r') as md:
            input = mdpp
            result = md.read()

            output = StringIO()
            modules = list(Modules.keys())
            MarkdownPP(input=input, modules=modules, output=output)

        output.seek(0)
        self.assertEqual(output.read(), result)


if __name__ == '__main__':
    unittest.main()

@@ -992,16 +992,44 @@
 n!VIDEO 
+'%0A                         '
 %22http://
@@ -1076,32 +1076,33 @@
        result = 
+(
 'foobar%5Cn%5B!%5BLink
@@ -1094,16 +1094,37 @@
 foobar%5Cn
+'%0A                  '
 %5B!%5BLink 
@@ -1173,16 +1173,37 @@
 uY.png)%5D
+'%0A                  '
 (http://
@@ -1241,16 +1241,17 @@
 -duY)%5Cn'
+)
 %0A%0A      
@@ -2374,16 +2374,17 @@
 esult = 
+(
 '%5Cn*%5Ct%5BG
@@ -2403,32 +2403,53 @@
 b%5D%5Cn%5Cn%5Bgithub%5D: 
+'%0A                  '
 http://github.co
@@ -2459,16 +2459,17 @@
 GitHub%22'
+)
 %0A%0A      
@@ -2740,16 +2740,17 @@
 lt_re = 
+(
 r'!%5C%5B%5C%5Cd
@@ -2774,16 +2774,41 @@
 1 = 2 %5C%5D
+'%0A                     r'
 %5C(http:%5C
@@ -2836,16 +2836,41 @@
 *%5C.png %22
+'%0A                     r'
 %5C%5Cdispla
@@ -2891,16 +2891,17 @@
 = 2 %22%5C)'
+)
 %0A%0A      
@@ -3263,18 +3263,16 @@
 eadme.md
-pp
 ', 'r') 
@@ -3280,11 +3280,54 @@
 s md
-pp,
+:%0A            result = md.read()%0A%0A        with
  ope
@@ -3333,32 +3333,34 @@
 en('../readme.md
+pp
 ', 'r') as md:%0A 
@@ -3348,32 +3348,34 @@
 dpp', 'r') as md
+pp
 :%0A            in
@@ -3388,39 +3388,8 @@
 mdpp
-%0A            result = md.read()
 %0A%0A  

fix long lines.