[DEPOT] ~gitignore : exclusion d'IMG
[ptitvelo/web/www.git] / www / plugins-dist / compresseur / lib / csstidy / testing / unit-tests / class.csstidy_csst.php
1 <?php
2
3 require_once 'class.Text_Diff_Renderer_parallel.php';
4
5 /**
6 * CSSTidy CSST expectation, for testing CSS parsing.
7 */
8 class csstidy_csst extends SimpleExpectation
9 {
10 /** Filename of test */
11 var $filename;
12
13 /** Test name */
14 var $test;
15
16 /** CSS for test to parse */
17 var $css = '';
18
19 /** Settings for csstidy */
20 var $settings = array();
21
22 /** Expected var_export() output of $css->css[41] (no at block) */
23 var $expect = '';
24
25 /** Boolean whether or not to use $css->css instead for $expect */
26 var $fullexpect = false;
27
28 /** Print form of CSS that can be tested **/
29 var $print = false;
30 var $default_media = "";
31
32 /** Actual result */
33 var $actual;
34
35 /**
36 * Loads this class from a file.
37 * @param $filename String filename to load
38 */
39 function load($filename) {
40 $this->filename = $filename;
41 $fh = fopen($filename, 'r');
42 $state = '';
43 while (($line = fgets($fh)) !== false) {
44 $line = rtrim($line, "\n\r"); // normalize newlines
45 if (substr($line, 0, 2) == '--') {
46 // detected section
47 $state = $line;
48 continue;
49 }
50 if ($state === null) continue;
51 switch ($state) {
52 case '--TEST--':
53 $this->test = trim($line);
54 break;
55 case '--CSS--':
56 $this->css .= $line . "\n";
57 break;
58 case '--FULLEXPECT--':
59 $this->fullexpect = true;
60 $this->expect .= $line . "\n";
61 break;
62 case '--EXPECT--':
63 $this->expect .= $line . "\n";
64 break;
65 case '--SETTINGS--':
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;
70 else
71 $this->settings[$n] = $v;
72 break;
73 case '--PRINT--':
74 $this->print = true;
75 $this->expect .= $line . "\n";
76 break;
77 }
78 }
79 if ($this->print) {
80 $this->expect = trim($this->expect);
81 }
82 else {
83 if ($this->expect)
84 $this->expect = eval("return ".$this->expect.";");
85 if (!$this->fullexpect)
86 $this->expect = array(41=>$this->expect);
87 }
88 fclose($fh);
89 }
90
91 /**
92 * Implements SimpleExpectation::test().
93 * @param $filename Filename of test file to test.
94 */
95 function test($filename = false) {
96 if ($filename) $this->load($filename);
97 $css = new csstidy();
98 $css->set_cfg($this->settings);
99 $css->parse($this->css);
100 if ($this->print){
101 $this->actual = $css->print->plain($this->default_media);
102 }
103 else{
104 $this->actual = $css->css;
105 }
106 return $this->expect === $this->actual;
107 }
108
109 /**
110 * Implements SimpleExpectation::testMessage().
111 */
112 function testMessage() {
113 $message = $this->test . ' test at '. htmlspecialchars($this->filename);
114 return $message;
115 }
116
117 /**
118 * Renders the test with an HTML diff table.
119 */
120 function render() {
121 $message = '<pre>'. htmlspecialchars($this->css) .'</pre>';
122 $diff = new Text_Diff(
123 'auto',
124 array(
125 explode("\n", $this->print?$this->expect:var_export($this->expect,true)),
126 explode("\n", $this->print?$this->actual:var_export($this->actual,true))
127 )
128 );
129 $renderer = new Text_Diff_Renderer_parallel();
130 $renderer->original = 'Expected';
131 $renderer->final = 'Actual';
132 $message .= $renderer->render($diff);
133 return $message;
134 }
135 }