Ejemplo n.º 1
0
<?php

global $th;
if (!isset($th)) {
    require_once dirname(dirname(__DIR__)) . '/src/TestHelper.class.php';
    $th = new rkphplib\TestHelper();
}
require_once dirname(dirname(__DIR__)) . '/src/Tokenizer.class.php';
$tx = new rkphplib\Tokenizer();
$th->compare('escape', [$tx->escape('abc efg'), $tx->escape('a{b}c'), $tx->escape('{:=c} {x:} {aa:bb} {:aa}')], ['abc efg', 'a{b}c', '&#123;&#58;=c&#125; &#123;x&#58;&#125; &#123;aa&#58;bb&#125; &#123;&#58;aa&#125;']);
$th->compare('unescape', [$tx->unescape('abc efg'), $tx->unescape('a{b}c'), $tx->unescape('&#123;&#58;=c&#125; &#123;x&#58;&#125; &#123;aa&#58;bb&#125; &#123;&#58;aa&#125;')], ['abc efg', 'a{b}c', '{:=c} {x:} {aa:bb} {:aa}']);
$th->runTokenizer(1, array());
Ejemplo n.º 2
0
/**
 * Run tokenizer tests t1.txt ... t$num.txt.
 * Requires t1.ok.txt ... t$num.ok.txt
 *
 * @param any $num (5 = run t1.txt ... t5.txt, 'a.txt' run only this test)
 * @param vector $plugin_list
 */
function run_tokenizer($num, $plugin_list)
{
    global $test_count;
    $src_dir = dirname(__DIR__) . '/src';
    $tdir = dirname(getcwd() . '/' . $_SERVER['SCRIPT_NAME']);
    if (!empty($test_count['path'])) {
        $tdir .= '/' . $test_count['path'];
    }
    include_once $src_dir . '/Tokenizer.class.php';
    include_once $src_dir . '/File.class.php';
    $tok = new rkphplib\Tokenizer(rkphplib\Tokenizer::TOK_DEBUG);
    for ($i = 0; $i < count($plugin_list); $i++) {
        $plugin = 'rkphplib\\' . $plugin_list[$i];
        include_once $src_dir . '/' . $plugin_list[$i] . '.class.php';
        $tok->register(new $plugin());
    }
    $test_files = array();
    if (is_string($num)) {
        array_push($test_files, $tdir . '/' . $num);
    } else {
        for ($i = 1; $i <= $num; $i++) {
            array_push($test_files, $tdir . '/t' . $i . '.txt');
        }
    }
    $i = 0;
    foreach ($test_files as $f_txt) {
        $f_out = str_replace('.txt', '.out.txt', $f_txt);
        $f_ok = str_replace('.txt', '.ok.txt', $f_txt);
        $i++;
        $tok->setText(rkphplib\File::load($f_txt));
        $ok = rkphplib\File::load($f_ok);
        $out = $tok->toString();
        $test_count['num']++;
        print "Test {$i} ... ";
        if ($out != $ok) {
            if (mb_strlen($out) > 40 || strpos($out, "\n") !== false) {
                print "ERROR! (see {$f_out})\n";
                rkphplib\File::save($f_out, $out);
            } else {
                print "{$out} != {$ok} - ERROR!\n";
            }
            $test_count['error']++;
        } else {
            $test_count['ok']++;
            print "ok\n";
        }
    }
}