View source
<?php
class CoderTestFile extends SimpleExpectation {
var $filename;
var $test;
var $input = array();
var $expect = array();
var $actual = array();
var $full = 0;
var $only = array();
function load($filename) {
$this->filename = $filename;
$fh = fopen($filename, 'r');
$state = '';
$unit = 0;
while (($line = fgets($fh)) !== false) {
$line = rtrim($line, "\n\r");
if (substr($line, 0, 2) == '--') {
$state = trim($line, ' -');
if ($state == 'INPUT') {
if ($this->only[$unit]) {
break;
}
$unit++;
}
continue;
}
if (!$state) {
list($keyword, $line) = explode(': ', $line, 2);
}
else {
$keyword = $state;
}
switch ($keyword) {
case 'TEST':
$this->test = $line;
break;
case 'FULL':
$this->full = (bool) $line;
break;
case 'INPUT':
$this->input[$unit] .= $line . "\n";
break;
case 'EXPECT':
$this->expect[$unit] .= $line . "\n";
break;
case 'ONLY':
$this->only[$unit] = TRUE;
break;
}
}
fclose($fh);
foreach (range(1, $unit) as $unit) {
if (!isset($this->expect[$unit])) {
$this->expect[$unit] = $this->input[$unit];
}
if (!$this->full) {
$prepend = "<?php\n// \$" . "Id\$\n\n";
$this->input[$unit] = $prepend . rtrim($this->input[$unit], "\n") . "\n\n";
$this->expect[$unit] = $prepend . rtrim($this->expect[$unit], "\n") . "\n\n";
}
}
if (!empty($this->only[$unit])) {
$this->input = array(
$this->input[$unit],
);
$this->expect = array(
$this->expect[$unit],
);
}
}
function test($filename = false) {
if ($filename) {
$this
->load($filename);
}
$valid = TRUE;
foreach ($this->input as $unit => $content) {
$this->actual[$unit] = coder_format_string_all($this->input[$unit]);
if ($this->expect[$unit] !== $this->actual[$unit]) {
$valid = FALSE;
}
}
return $valid;
}
function testMessage() {
$message = $this->test . ' test in ' . htmlspecialchars(basename($this->filename));
return $message;
}
function render() {
drupal_add_css(drupal_get_path('module', 'coder') . '/scripts/coder_format/tests/coder-diff.css', 'module', 'all', false);
foreach ($this->input as $unit => $content) {
if ($this->expect[$unit] === $this->actual[$unit]) {
continue;
}
$diff = new Text_Diff('auto', array(
explode("\n", $this->expect[$unit]),
explode("\n", $this->actual[$unit]),
));
$renderer = new Text_Diff_Renderer_parallel($this->test . ' test in ' . htmlspecialchars(basename($this->filename)));
$message .= $renderer
->render($diff);
}
return $message;
}
}
class Text_Diff_Renderer_parallel extends Text_Diff_Renderer {
var $original = 'Expected';
var $final = 'Actual';
var $_leading_context_lines = 10000;
var $_trailing_context_lines = 10000;
var $title;
function Text_Diff_Renderer_parallel($title) {
$this->title = $title;
}
function _blockHeader() {
}
function _startDiff() {
return '<table class="diff"><thead><tr><th colspan="2">' . $this->title . '</th></tr><tr><th>' . $this->original . '</th><th>' . $this->final . '</th></tr></thead><tbody>';
}
function _endDiff() {
return '</tbody></table>';
}
function _context($lines) {
return '<tr><td><pre>' . $this
->_renderLines($lines) . '</pre></td>
<td><pre>' . $this
->_renderLines($lines) . '</pre></td></tr>';
}
function _added($lines) {
return '<tr><td> </td><td class="added"><pre>' . $this
->_renderLines($lines) . '</pre></td></tr>';
}
function _deleted($lines) {
return '<tr><td class="deleted"><pre>' . $this
->_renderLines($lines) . '</pre></td><td> </td></tr>';
}
function _changed($orig, $final) {
return '<tr class="changed"><td><pre>' . $this
->_renderLines($orig) . '</pre></td>
<td><pre>' . $this
->_renderLines($final) . '</pre></td></tr>';
}
function _renderLines($lines) {
return str_replace("\n", "<strong>¶</strong>\n", htmlspecialchars(implode("\n", $lines) . "\n"));
}
}