Menu

[r90]: / trunk / testing / unit-tests.inc  Maximize  Restore  History

Download this file

152 lines (138 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**@file
* Utility functions and classes for unit testing
*/
function globr($sDir, $sPattern, $nFlags = NULL) {
$aFiles = glob("$sDir/$sPattern", $nFlags);
$files = getDir($sDir);
if (is_array($files)) {
foreach( $files as $file ) {
$aSubFiles = globr($file, $sPattern, $nFlags);
$aFiles = array_merge($aFiles,$aSubFiles);
}
}
return $aFiles;
}
function getDir($sDir) {
$i=0;
$aDirs = array();
if(is_dir($sDir)) {
if($rContents = opendir($sDir)) {
while($sNode = readdir($rContents)) {
if(is_dir($sDir.'/'.$sNode )) {
if($sNode !="." && $sNode !="..") {
$aDirs[$i] = $sDir.'/'.$sNode ;
$i++;
}
}
}
}
}
return $aDirs;
}
class csstidy_test_csst
{
/** Filename of test */
var $filename;
/** Test name */
var $test;
/** CSS for test to parse */
var $css = '';
/** Expected var_export() output of $css->css[41] (no at block) */
var $expect = '';
/** Expected var_export() output of $css->css (with at block) */
var $fullexpect = '';
/** Actual result */
var $actual;
/**
* Loads this class from a file
*/
function load($filename) {
$this->filename = $filename;
$fh = fopen($filename, 'r');
$state = null;
while (($line = fgets($fh)) !== false) {
$line = rtrim($line, "\n\r"); // normalize newlines
if (substr($line, 0, 2) == '--') {
// detected section
$state = $line;
continue;
}
if ($state === null) continue;
switch ($state) {
case '--TEST--':
$this->test = trim($line);
continue;
case '--CSS--':
$this->css .= $line . "\n";
continue;
case '--EXPECT--':
$this->expect .= $line . "\n";
continue;
case '--FULLEXPECT--':
$this->fullexpect .= $line . "\n";
continue;
}
}
$this->expect = trim($this->expect, "\n"); // trim trailing/leading newlines
fclose($fh);
}
/**
* Runs test
* @return TRUE if success
*/
function run() {
$css = new csstidy();
$css->parse($this->css);
$this->actual = var_export($css->css[41], true);
return $this->expect === $this->actual;
}
/**
* Renders the test
*/
function render() {
if ($this->actual === null) $this->run();
$heading = '<strong>'. htmlspecialchars($this->test) .'</strong> test (<tt>'. htmlspecialchars($this->filename) .'</tt>)';
if ($this->expect === $this->actual) {
return;
// comment the above return to show passes
echo "<div><span style=\"color:green;\">Passed:</span> $heading</div>";
return;
}
echo "<div><span style=\"color:red;\">Failed:</span> $heading</div>";
echo '<pre>'. htmlspecialchars($this->css) .'</pre>';
$diff = new Text_Diff('auto', array(explode("\n", $this->expect), explode("\n", $this->actual)));
$renderer = new Text_Diff_Renderer_parallel();
$renderer->original = 'Expected';
$renderer->final = 'Actual';
echo $renderer->render($diff);
}
}
class Text_Diff_Renderer_parallel extends Text_Diff_Renderer
{
var $original = 'Original';
var $final = 'Final';
var $_leading_context_lines = 10000;
var $_trailing_context_lines = 10000;
function _blockHeader() {}
function _startDiff() {
return '<table class="diff"><thead><tr><th>'. $this->original .'</th><th>'. $this->final .'</th></tr></thead><tbody>';
}
function _endDiff() {
return '</tbody></table>';
}
function _context($lines) {
return '<tr><td><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td>
<td><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td></tr>';
}
function _added($lines) {
return '<tr><td>&nbsp;</td><td class="added"><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td></tr>';
}
function _deleted($lines) {
return '<tr><td class="deleted"><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td><td>&nbsp;</td></tr>';
}
function _changed($orig, $final) {
return '<tr class="changed"><td><pre>'. htmlspecialchars(implode("\n", $orig)) .'</pre></td>
<td><pre>'. htmlspecialchars(implode("\n", $final)) .'</pre></td></tr>';
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.