forked from leafo/scssphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputTest.php
More file actions
176 lines (142 loc) · 4.3 KB
/
InputTest.php
File metadata and controls
176 lines (142 loc) · 4.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* SCSSPHP
*
* @copyright 2012-2015 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://leafo.github.io/scssphp
*/
namespace Leafo\ScssPhp\Tests;
use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\LineCommentator;
function _dump($value)
{
fwrite(STDOUT, print_r($value, true));
}
function _quote($str)
{
return preg_quote($str, '/');
}
/**
* Input test - runs all the tests in inputs/ and compares their output to ouputs/
*
* @author Leaf Corcoran <leafot@gmail.com>
*/
class InputTest extends \PHPUnit_Framework_TestCase
{
protected static $inputDir = 'inputs';
protected static $outputDir = 'outputs';
protected static $outputNumberedDir = 'outputs_numbered';
private $saveDir;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->scss = new Compiler();
$this->scss->addImportPath(self::$inputDir);
$this->saveDir = getcwd();
chdir(__DIR__);
}
/**
* {@inheritDoc}
*/
protected function tearDown()
{
chdir($this->saveDir);
}
/**
* @dataProvider fileNameProvider
*/
public function testInputFile($inFname, $outFname)
{
if (getenv('BUILD')) {
return $this->buildInput($inFname, $outFname);
}
if (! is_readable($outFname)) {
$this->fail("$outFname is missing, consider building tests with BUILD=1");
}
$input = file_get_contents($inFname);
$output = file_get_contents($outFname);
$this->assertEquals($output, $this->scss->compile($input, substr($inFname, strlen(__DIR__) + 1)));
}
/**
* Run all tests with line numbering
*
* @dataProvider numberedFileNameProvider
*/
public function testLineNumbering($inFname, $outFname)
{
$this->scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
if (getenv('BUILD')) {
return $this->buildInput($inFname, $outFname);
}
if (! is_readable($outFname)) {
$this->fail("$outFname is missing, consider building tests with BUILD=true");
}
$input = file_get_contents($inFname);
$output = file_get_contents($outFname);
$this->assertEquals($output, $this->scss->compile($input, substr($inFname, strlen(__DIR__) + 1)));
}
public function fileNameProvider()
{
return array_map(
function ($a) {
return array($a, InputTest::outputNameFor($a));
},
self::findInputNames()
);
}
public function numberedFileNameProvider()
{
return array_map(
function ($a) {
return array($a, InputTest::outputNumberedNameFor($a));
},
self::findInputNames()
);
}
// only run when env is set
public function buildInput($inFname, $outFname)
{
$css = $this->scss->compile(file_get_contents($inFname), substr($inFname, strlen(__DIR__) + 1));
file_put_contents($outFname, $css);
}
public static function findInputNames($pattern = '*')
{
$files = glob(__DIR__ . '/' . self::$inputDir . '/' . $pattern);
$files = array_filter($files, 'is_file');
if ($pattern = getenv('MATCH')) {
$files = array_filter($files, function ($fname) use ($pattern) {
return preg_match("/$pattern/", $fname);
});
}
return $files;
}
public static function outputNameFor($input)
{
$front = _quote(__DIR__ . '/');
$out = preg_replace("/^$front/", '', $input);
$in = _quote(self::$inputDir . '/');
$out = preg_replace("/$in/", self::$outputDir . '/', $out);
$out = preg_replace('/.scss$/', '.css', $out);
return __DIR__ . '/' . $out;
}
public static function outputNumberedNameFor($input)
{
$front = _quote(__DIR__ . '/');
$out = preg_replace("/^$front/", '', $input);
$in = _quote(self::$inputDir . '/');
$out = preg_replace("/$in/", self::$outputNumberedDir . '/', $out);
$out = preg_replace('/.scss$/', '.css', $out);
return __DIR__ . '/' . $out;
}
public static function buildTests($pattern)
{
$files = self::findInputNames($pattern);
foreach ($files as $file) {
}
}
}