# -*- coding: utf-8 -*-

import io
import shutil
import tempfile
import unittest
from os import path

from nikola.plugins.compile.markdown import CompileMarkdown
from .base import BaseTestCase, FakeSite


class CompileMarkdownTests(BaseTestCase):
    def setUp(self):
        self.tmp_dir = tempfile.mkdtemp()
        self.input_path = path.join(self.tmp_dir, 'input.markdown')
        self.output_path = path.join(self.tmp_dir, 'output.html')

        self.compiler = CompileMarkdown()
        self.compiler.set_site(FakeSite())

    def compile(self, input_string):
        with io.open(self.input_path, "w+", encoding="utf8") as input_file:
            input_file.write(input_string)

        self.compiler.compile(self.input_path, self.output_path, lang='en')

        output_str = None
        with io.open(self.output_path, "r", encoding="utf8") as output_path:
            output_str = output_path.read()

        return output_str

    def tearDown(self):
        shutil.rmtree(self.tmp_dir)

    def test_compile_empty(self):
        input_str = ''
        actual_output = self.compile(input_str)
        self.assertEqual(actual_output, '')

    def test_compile_code_hilite(self):
        input_str = '''\
    #!python
    from this
'''
        expected_output = '''\
<table class="codehilitetable"><tr><td class="linenos">\
<div class="linenodiv"><pre>1</pre></div>\
</td><td class="code"><pre class="code literal-block"><span></span>\
<span class="kn">from</span> <span class="nn">this</span>
</pre>
</td></tr></table>
'''

        actual_output = self.compile(input_str)
        self.assertEqual(actual_output.strip(), expected_output.strip())

    def test_compile_strikethrough(self):
        input_str = '~~striked out text~~'
        expected_output = '<p><del>striked out text</del></p>'

        actual_output = self.compile(input_str)
        self.assertEqual(actual_output.strip(), expected_output.strip())

    def test_mdx_podcast(self):
        input_str = "[podcast]https://archive.org/download/Rebeldes_Stereotipos/rs20120609_1.mp3[/podcast]"
        expected_output = '<p><audio controls=""><source src="https://archive.org/download/Rebeldes_Stereotipos/rs20120609_1.mp3" type="audio/mpeg"></source></audio></p>'
        actual_output = self.compile(input_str)
        self.assertEqual(actual_output.strip(), expected_output.strip())


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

# -*- coding: utf-8 -*-

import io
from os import path

from nikola.plugins.compile.markdown import CompileMarkdown

import pytest

from .base import FakeSite


@pytest.fixture(scope="module")
def fakesite():
    return FakeSite()


@pytest.fixture(scope="module")
def compiler(fakesite):
    compiler = CompileMarkdown()
    compiler.set_site(fakesite)
    return compiler


@pytest.fixture
def input_path(tmpdir):
    return path.join(tmpdir, 'input.markdown')


@pytest.fixture
def output_path(tmpdir):
    return path.join(tmpdir, 'output.html')


def markdown_compile(compiler, input_path, output_path, text):
    with io.open(input_path, "w+", encoding="utf8") as input_file:
        input_file.write(text)

    compiler.compile(input_path, output_path, lang='en')

    with io.open(output_path, "r", encoding="utf8") as output_path:
        return output_path.read()


@pytest.mark.parametrize("input_str, expected_output", [
    ('', ''),
    ("[podcast]https://archive.org/download/Rebeldes_Stereotipos/rs20120609_1.mp3[/podcast]", '<p><audio controls=""><source src="https://archive.org/download/Rebeldes_Stereotipos/rs20120609_1.mp3" type="audio/mpeg"></source></audio></p>'),
    ('~~striked out text~~', '<p><del>striked out text</del></p>'),
    ('''\
    #!python
    from this
''', '''\
<table class="codehilitetable"><tr><td class="linenos">\
<div class="linenodiv"><pre>1</pre></div>\
</td><td class="code"><pre class="code literal-block"><span></span>\
<span class="kn">from</span> <span class="nn">this</span>
</pre>
</td></tr></table>
'''),
], ids=["empty", "mdx podcast", "strikethrough", "hilite"])
def test_compiling_markdown(compiler, input_path, output_path, input_str, expected_output):
    output = markdown_compile(compiler, input_path, output_path, input_str)
    assert output.strip() == expected_output.strip()

Use test parametrization to check compiling to markdown.