3 require_once 'class.Text_Diff_Renderer_parallel.php';
6 * CSSTidy CSST expectation, for testing CSS parsing.
8 class csstidy_csst
extends SimpleExpectation
10 /** Filename of test */
16 /** CSS for test to parse */
19 /** Settings for csstidy */
20 var $settings = array();
22 /** Expected var_export() output of $css->css[41] (no at block) */
25 /** Boolean whether or not to use $css->css instead for $expect */
26 var $fullexpect = false;
28 /** Print form of CSS that can be tested **/
30 var $default_media = "";
36 * Loads this class from a file.
37 * @param $filename String filename to load
39 function load($filename) {
40 $this->filename
= $filename;
41 $fh = fopen($filename, 'r');
43 while (($line = fgets($fh)) !== false) {
44 $line = rtrim($line, "\n\r"); // normalize newlines
45 if (substr($line, 0, 2) == '--') {
50 if ($state === null) continue;
53 $this->test
= trim($line);
56 $this->css
.= $line . "\n";
58 case '--FULLEXPECT--':
59 $this->fullexpect
= true;
60 $this->expect
.= $line . "\n";
63 $this->expect
.= $line . "\n";
66 list($n, $v) = array_map('trim',explode('=', $line, 2));
67 $v = eval("return $v;");
68 if ($n=="default_media" AND $this->print)
69 $this->default_media
= $v;
71 $this->settings
[$n] = $v;
75 $this->expect
.= $line . "\n";
80 $this->expect
= trim($this->expect
);
84 $this->expect
= eval("return ".$this->expect
.";");
85 if (!$this->fullexpect
)
86 $this->expect
= array(41=>$this->expect
);
92 * Implements SimpleExpectation::test().
93 * @param $filename Filename of test file to test.
95 function test($filename = false) {
96 if ($filename) $this->load($filename);
98 $css->set_cfg($this->settings
);
99 $css->parse($this->css
);
101 $this->actual
= $css->print->plain($this->default_media
);
104 $this->actual
= $css->css
;
106 return $this->expect
=== $this->actual
;
110 * Implements SimpleExpectation::testMessage().
112 function testMessage() {
113 $message = $this->test
. ' test at '. htmlspecialchars($this->filename
);
118 * Renders the test with an HTML diff table.
121 $message = '<pre>'. htmlspecialchars($this->css
) .'</pre>';
122 $diff = new Text_Diff(
125 explode("\n", $this->print?
$this->expect
:var_export($this->expect
,true)),
126 explode("\n", $this->print?
$this->actual
:var_export($this->actual
,true))
129 $renderer = new Text_Diff_Renderer_parallel();
130 $renderer->original
= 'Expected';
131 $renderer->final = 'Actual';
132 $message .= $renderer->render($diff);