<commit_before><?php

use FluxBB\Markdown\Parser;
use FluxBB\Markdown\Renderer\XhtmlRenderer;
use Symfony\Component\Finder\Finder;

/**
 * Tests FluxBB\Extensions\Core\*
 *
 * @group Markdown
 * @group MarkdownCore
 *
 * @author <NAME> <<EMAIL>>
 */
class CoreExtensionsTest extends PHPUnit_Framework_TestCase
{

    /**
     * Test core patterns
     *
     * See `test/Resources/markdown-testsuite`
     *
     * @param string $name     Name of the test case
     * @param string $markdown The Markdown content
     * @param string $expected Expected output
     *
     * @dataProvider markdownTestSuiteProvider
     */
    public function testWithMarkdownTestSuite($name, $markdown, $expected)
    {
        $ciconia = new Parser(new XhtmlRenderer());
        $html    = $ciconia->render($markdown);

        $this->assertEquals($expected, $html, sprintf('%s failed', $name));
    }

    /**
     * Test an email link
     */
    public function testAutoLinkEmail()
    {
        $ciconia  = new Parser();
        $markdown = 'Email <<EMAIL>>';
        $output   = $ciconia->render($markdown);
        $expected = '<p>Email <a href="mailto:<EMAIL>"><EMAIL></a></p>';

        $this->assertEquals($expected, html_entity_decode($output));
    }

    /**
     * On strict mode
     *
     * @param string $name     Name of the test case
     * @param string $markdown The Markdown content
     * @param string $expected Expected output
     *
     * @dataProvider strictModeProvider
     * @expectedException \FluxBB\Markdown\Exception\SyntaxError
     */
    public function testStrictMode($name, $markdown, $expected)
    {
        $ciconia = new Parser();
        $html    = $ciconia->render($markdown, ['strict' => true]);

        $this->assertEquals($expected, $html, sprintf('%s failed', $name));
    }

    /**
     * @return array
     */
    public function markdownTestSuiteProvider()
    {
        $finder = Finder::create()
            ->in(__DIR__.'/../Resources/core')
            ->files()
            ->name('*.md')
            ->notName('link-automatic-email.md');

        return $this->processPatterns($finder);
    }

    /**
     * @return array
     */
    public function strictModeProvider()
    {
        $finder = Finder::create()
            ->in(__DIR__.'/../Resources/options/strict/core')
            ->files()
            ->name('*.md');

        return $this->processPatterns($finder);
    }

    /**
     * @param Finder|\Symfony\Component\Finder\SplFileInfo[] $finder
     *
     * @return array
     */
    protected function processPatterns(Finder $finder)<commit_msg>Test using the CommonMark spec examples instead of the Markdown test suite.<commit_after><?php

use FluxBB\Markdown\Parser;
use FluxBB\Markdown\Renderer\XhtmlRenderer;
use Symfony\Component\Finder\Finder;

/**
 * Tests FluxBB\Extensions\Core\*
 *
 * @group Markdown
 * @group MarkdownCore
 *
 * @author <NAME> <<EMAIL>>
 */
class CoreExtensionsTest extends PHPUnit_Framework_TestCase
{

    /**
     * Test CommonMark spec examples
     *
     * See `test/Resources/spec.txt`
     *
     * @param string $name
     * @param string $markdown
     * @param string $expected
     * @param string $section
     *
     * @dataProvider commonMarkSpecProvider
     */
    public function testWithCommonMarkSpec($name, $markdown, $expected, $section)
    {
        $parser = new Parser(new XhtmlRenderer());
        $html = $parser->render($markdown);

        $this->assertEquals($expected, $html, "$section: $name failed");
    }

    /**
     * Test an email link
     */
    public function testAutoLinkEmail()
    {
        $ciconia  = new Parser();
        $markdown = 'Email <<EMAIL>>';
        $output   = $ciconia->render($markdown);
        $expected = '<p>Email <a href="mailto:<EMAIL>"><EMAIL></a></p>';

        $this->assertEquals($expected, html_entity_decode($output));
    }

    /**
     * On strict mode
     *
     * @param string $name     Name of the test case
     * @param string $markdown The Markdown content
     * @param string $expected Expected output
     *
     * @dataProvider strictModeProvider
     * @expectedException \FluxBB\Markdown\Exception\SyntaxError
     */
    public function testStrictMode($name, $markdown, $expected)
    {
        $ciconia = new Parser();
        $html    = $ciconia->render($markdown, ['strict' => true]);

        $this->assertEquals($expected, $html, sprintf('%s failed', $name));
    }

    /**
     * @return array
     */
    public function commonMarkSpecProvider()
    {
        $lines = file(__DIR__.'/../Resources/CommonMark/spec.txt', FILE_IGNORE_NEW_LINES);

        $state = 0; // 0 regular text, 1 markdown example, 2 html output
        $startLine = 0;
        $lineNumber = 0;
        $exampleNumber = 0;
        $headerText = '';
        $markdownLines = [];
        $htmlLines = [];

        $tests = [];
        foreach ($lines as $line) {
            $lineNumber++;

            if ($state == 0 && preg_match('/#+ /', $line)) {
                $headerText = trim(preg_replace('/#+ /', '', $line));
            }

            if (trim($line) == '.') {
                $state = ($state + 1) % 3;
                if ($state == 0) {
                    $exampleNumber++;
                    $endLine = $lineNumber;

                    $tests[] = [
                        "#$exampleNumber (lines $startLine - $endLine)",
                        str_replace('→', "\t", implode("\n", $markdownLines)),
                        implode("\n", $htmlLines),
                        $headerText,
                    ];

                    $startLine = 0;
                    $markdownLines = [];
                    $htmlLines = [];
                }
            } elseif ($state == 1) {
                if ($startLine == 0) {
                    $startLine = $lineNumber - 1;
                }
                $markdownLines[] = $line;
            } elseif ($state == 2) {
                $htmlLines[] = $line;
            }
        }

        return $tests;
    }

    /**
     * @return array
     */
    public function strictModeProvider()
    {
        $finder = Finder::create()
            ->in(__DIR__.'/../Resources/options/strict/core')
            ->files()
            ->name('*.md');

        return $this->processPatterns($finder);
    }

    /**
     * @param Finder|\Symfony\Component\Finder\SplFileInfo[] $finder
     *
     * @return array
     */
    protected function processPatterns(Finder $finder)
{'fraction_non_alphanumeric': 0.1455189405605174, 'fraction_numerical': 0.0024638127502309825, 'mean_word_length': 1.9671082686158063, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3854849', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1838, 'n_words': 400}