<commit_before><?php

function setup_autoload() {
    // quick setup for autoloading
    $path = str_replace('/', DIRECTORY_SEPARATOR, dirname(__FILE__) . '/../');
    $path = realpath($path);
    set_include_path(get_include_path() . PATH_SEPARATOR . $path);

    spl_autoload_register(function ($class) {
        $file = dirname(__FILE__) . '/../src/' . str_replace("\\", DIRECTORY_SEPARATOR, $class) . '.php';
        if(file_exists($file)) {
            require_once($file);
        }
    });
}

function find_tests() {
    // find the tests
    $path = str_replace('/', DIRECTORY_SEPARATOR, dirname(__FILE__) . '/');
    $path = realpath($path);
    return glob($path . DIRECTORY_SEPARATOR . '*.jade');
}

function build_list($test_list) {
    $group_list = array();
    foreach ($test_list as $test) {
        $name = basename($test, '.jade');
        $parts = preg_split('/[.-]/', $name);

        if (!isset($group_list[$parts[0]])) {
            $group_list[$parts[0]] = array();
        }
        $group_list[$parts[0]][] = array('link' => $test, 'name' => $name);
    }

    return $group_list;
}

function get_php_code($file) {
    $jade = new \Jade\Jade();
    return $jade->render($file);
}

function init_tests() {
    mb_internal_encoding('utf-8');
    error_reporting(E_ALL);
    setup_autoload();
}

function get_generated_html($contents) {
    if(ini_get('allow_url_include') | 0) {
        error_reporting(E_ALL & ~E_NOTICE);
        ob_start();
        include "data://text/plain;base64," . base64_encode($contents);
        $contents = ob_get_contents();
        ob_end_clean();
        error_reporting(E_ALL);
    } else {
        $file = tempnam(sys_get_temp_dir(), 'jade');
        file_put_contents($file, $contents);
        $contents = `php -d error_reporting="E_ALL & ~E_NOTICE" {$file}`;
        unlink($file);
    }
    return $contents;
}

function get_tests_results($verbose = false) {

    global $argv;

    if(! (ini_get('allow_url_include') | 0)) {
        echo "To accelerate the test execution, set in php.ini :\nallow_url_include = On\n\n";
    }

    $initialDirectory = getcwd();
    chdir(dirname(__FILE__));

    init_tests();

    $nav_list = build_list(find_tests());

    $success = 0;
    $failures = 0;
    $results = array();

    foreach($nav_list as $type => $arr) {
        foreach($arr as $e) {
        	$name = $e['name'];
            if($name == 'index' || (
                isset($argv[1]) &&
                false === stripos($argv[0], 'phpunit') &&
                $name != $argv[1] &&
                $argv[1] != '.'
            )) {
                continue;
            }

            $expectedHtml = @file_get_contents($name . '.html');
            if($expectedHtml === FALSE) {
                if($verbose) {
                    echo "! sample for test '$name' not found.\n";
                }
                continue;
            }

            if($verbose) {
                echo "* rendering test '$name'\n";
            }
            try {
                $new = get_php_code($name . '.jade');
            } catch(Exception $err) {
                if($verbose) {
                    echo "! FATAL: php exception: " . str_replace("\n", "\n\t", $err) . "\n";
                }
                $new = null;
            }

            if($new !== null) {

                $actualHtml = get_generated_html($new);

                // automatically compare $code and $html here
                $from = array("\n", "\r", "\t", " ", '"', "<!DOCTYPEhtml>");
                $to = array('', '', '', '', "'", '');
                $minifiedExpectedHtml = str_replace($from, $to, $expectedHtml);
                $minifiedActualHtml = str_replace($from, $to, $actualHtml);
                $results[] = array($name, $minifiedExpectedHtml, $minifiedActualHtml);
                if(strcmp($minifiedExpectedHtml, $minifiedActualHtml)) {
                    $failures++;
                    if($verbose) {
                        echo "  Expected: $expectedHtml\n";
                        echo "  Actual:   $actualHtml\n\n";
                    }
                    // render until first difference
                    if(isset($argv[1]) && $argv[1] == '.') {
                        exit;
                    }
                } else {
                    $success++;
                }
            }
        }
    }

    chdir($initialDirectory);

    return array(
        'success' => $success,
        'failures' => $failures,
        'results' => $results
    );
}<commit_msg>Init tests on bootstrap load<commit_after><?php

function setup_autoload() {
    // quick setup for autoloading
    $path = str_replace('/', DIRECTORY_SEPARATOR, dirname(__FILE__) . '/../');
    $path = realpath($path);
    set_include_path(get_include_path() . PATH_SEPARATOR . $path);

    spl_autoload_register(function ($class) {
        $file = dirname(__FILE__) . '/../src/' . str_replace("\\", DIRECTORY_SEPARATOR, $class) . '.php';
        if(file_exists($file)) {
            require_once($file);
        }
    });
}

function find_tests() {
    // find the tests
    $path = str_replace('/', DIRECTORY_SEPARATOR, dirname(__FILE__) . '/');
    $path = realpath($path);
    return glob($path . DIRECTORY_SEPARATOR . '*.jade');
}

function build_list($test_list) {
    $group_list = array();
    foreach ($test_list as $test) {
        $name = basename($test, '.jade');
        $parts = preg_split('/[.-]/', $name);

        if (!isset($group_list[$parts[0]])) {
            $group_list[$parts[0]] = array();
        }
        $group_list[$parts[0]][] = array('link' => $test, 'name' => $name);
    }

    return $group_list;
}

function get_php_code($file) {
    $jade = new \Jade\Jade(array(
        'prettyprint' => true
    ));
    return $jade->render($file);
}

function init_tests() {
    mb_internal_encoding('utf-8');
    error_reporting(E_ALL);
    setup_autoload();
}

function get_generated_html($contents) {
    if(ini_get('allow_url_include') | 0) {
        error_reporting(E_ALL & ~E_NOTICE);
        ob_start();
        include "data://text/plain;base64," . base64_encode($contents);
        $contents = ob_get_contents();
        ob_end_clean();
        error_reporting(E_ALL);
    } else {
        $file = tempnam(sys_get_temp_dir(), 'jade');
        file_put_contents($file, $contents);
        $contents = `php -d error_reporting="E_ALL & ~E_NOTICE" {$file}`;
        unlink($file);
    }
    return $contents;
}

function get_tests_results($verbose = false) {

    global $argv;

    if(! (ini_get('allow_url_include') | 0)) {
        echo "To accelerate the test execution, set in php.ini :\nallow_url_include = On\n\n";
    }

    $initialDirectory = getcwd();
    chdir(dirname(__FILE__));

    $nav_list = build_list(find_tests());

    $success = 0;
    $failures = 0;
    $results = array();

    foreach($nav_list as $type => $arr) {
        foreach($arr as $e) {
        	$name = $e['name'];
            if($name == 'index' || (
                isset($argv[1]) &&
                false === stripos($argv[0], 'phpunit') &&
                $name != $argv[1] &&
                $argv[1] != '.'
            )) {
                continue;
            }

            $expectedHtml = @file_get_contents($name . '.html');
            if($expectedHtml === FALSE) {
                if($verbose) {
                    echo "! sample for test '$name' not found.\n";
                }
                continue;
            }

            if($verbose) {
                echo "* rendering test '$name'\n";
            }
            try {
                $new = get_php_code($name . '.jade');
            } catch(Exception $err) {
                if($verbose) {
                    echo "! FATAL: php exception: " . str_replace("\n", "\n\t", $err) . "\n";
                }
                $new = null;
            }

            if($new !== null) {

                $actualHtml = get_generated_html($new);

                // automatically compare $code and $html here
                $from = array("\n", "\r", "\t", " ", '"', "<!DOCTYPEhtml>");
                $to = array('', '', '', '', "'", '');
                $minifiedExpectedHtml = str_replace($from, $to, $expectedHtml);
                $minifiedActualHtml = str_replace($from, $to, $actualHtml);
                $results[] = array($name, $minifiedExpectedHtml, $minifiedActualHtml);
                if(strcmp($minifiedExpectedHtml, $minifiedActualHtml)) {
                    $failures++;
                    if($verbose) {
                        echo "  Expected: $expectedHtml\n";
                        echo "  Actual:   $actualHtml\n\n";
                    }
                    // render until first difference
                    if(isset($argv[1]) && $argv[1] == '.') {
                        exit;
                    }
                } else {
                    $success++;
                }
            }
        }
    }

    chdir($initialDirectory);

    return array(
        'success' => $success,
        'failures' => $failures,
        'results' => $results
    );
}

init_tests();
{'fraction_non_alphanumeric': 0.16518591641987496, 'fraction_numerical': 0.003948667324777887, 'mean_word_length': 1.7472130159686652, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 20, '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': '7179041', 'n_tokens_mistral': 2863, 'n_tokens_neox': 2722, 'n_words': 508}