forked from MarcWeber/phamlp
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test.php
executable file
·150 lines (135 loc) · 3.84 KB
/
test.php
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
<!-- Just load this in a browser and the tests will run! -->
<html>
<head>
<title>PHamlP Test Suite</title>
<link rel="stylesheet" type="text/css" href="test.css">
</head>
<body>
<?php
/* Testing for Sassy.
* Looks in tests* and compiles any .sass/.scss files
* and compares to them to their twin .css files by
* filename.
*
* That is, if we have three files:
* test.scss
* test.sass
* test.css
*
* The tester will compile test.scss and test.sass seperately
* and compare their outputs both to each other and to test.css
*
* Testing is eased by stripping out all whitespace, which may
* introduce bugs of their own.
*/
require 'SassParser.php';
$test_dir = './tests';
$files = find_files($test_dir);
$i = 0;
foreach ($files['by_name'] as $name => $test) {
if (isset($_GET['name']) && $name != $_GET['name']) {
continue;
}
if (isset($_GET['skip']) && $name && preg_match('/(^|,)(' . preg_quote($name) . ')(,|$)/', $_GET['skip'])) {
continue;
}
if (count($test) > 1) {
$result = test_files($test, $test_dir);
if ($result === TRUE) {
print "\n\t<p class='pass'><em>PASS</em> $name</p>";
}
else {
print "\n\t<p class='fail'><em>FAIL</em> $name</p>";
print "<pre>$result</pre>";
}
flush();
if ($i++ == 100) {
die;
}
}
}
function test_files($files, $dir = '.') {
$result = null;
sort($files);
foreach ($files as $i => $file) {
$name = explode('.', $file);
$ext = array_pop($name);
$fn = 'parse_' . $ext;
if (function_exists($fn)) {
try {
$result = $fn($dir . '/' . $file);
} catch (Exception $e) {
return $e->__toString();
}
file_put_contents('/tmp/scss_test_' . $i, trim($result) . "\n");
}
}
exec('diff -ibwB /tmp/scss_test_0 /tmp/scss_test_1', $out);
if (count($out)) {
if (isset($_GET['full'])) {
$out[] = "\n\n\n" . $result;
}
return implode("\n", $out);
} else {
return TRUE;
}
}
function parse_scss($file) {
return __parse($file, 'scss');
}
function parse_sass($file) {
return __parse($file, 'sass');
}
function parse_css($file) {
return file_get_contents($file);
}
function __parse($file, $syntax, $style = 'nested') {
$options = array(
'style' => $style,
'cache' => FALSE,
'syntax' => $syntax,
'debug' => FALSE,
'callbacks' => array(
'warn' => 'cb_warn',
'debug' => 'cb_debug',
),
);
// Execute the compiler.
$parser = new SassParser($options);
return $parser->toCss($file);
}
function cb_warn($message, $context) {
print "<p class='warn'>WARN : ";
print_r($message);
print "</p>";
}
function cb_debug($message) {
print "<p class='debug'>DEBUG : ";
print_r($message);
print "</p>";
}
function find_files($dir) {
$op = opendir($dir);
$return = array('by_type' => array(), 'by_name' => array());
if ($op) {
while (false !== ($file = readdir($op))) {
if (substr($file, 0, 1) == '.') {
continue;
}
$name = explode('.', $file);
$ext = array_pop($name);
$return['by_type'][$ext] = $file;
$name = implode('.', $name);
if (!isset($return['by_name'][$name])) {
$return['by_name'][$name] = array();
}
$return['by_name'][$name][] = $name . '.' . $ext;
}
}
asort($return['by_name']);
asort($return['by_type']);
return $return;
}
?>
</body>
</html>