forked from jbboehr/php-mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-tests.php
90 lines (82 loc) · 2.95 KB
/
generate-tests.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
<?php
// Meh
error_reporting(E_ALL | E_STRICT);
ini_set('memory_limit', '256M');
ini_set('display_errors', true);
define('MY_EOL', "\n"); // PHP_EOL
if( !extension_loaded('yaml') ) {
echo 'Requires php-yaml' . PHP_EOL;
exit(1);
}
// Argv
if( !empty($argv[1]) && is_dir($argv[1]) ) {
$specDir = $argv[1];
} else if( is_dir('./spec/specs') ) {
$specDir = './spec/specs';
} else {
echo 'Unable to find specs' . MY_EOL;
exit(1);
}
$specs = array();
$specData = array();
foreach( scandir($specDir) as $file ) {
if( $file[0] == '~' ) {
continue;
}
//if( strlen($file) > 5 && substr($file, -5) == '.json' ) {
if( strlen($file) > 4 && substr($file, -4) == '.yml' ) {
$spec = substr($file, 0, -4);
$specs[] = $spec;
$contents = file_get_contents($specDir . DIRECTORY_SEPARATOR . $file);
//$specData[$spec] = json_decode($contents, true);
$specData[$spec] = yaml_parse($contents);
}
}
if( empty($specs) ) {
echo 'No specs found in specified directory' . MY_EOL;
exit(1);
}
// Generate tests
foreach( $specData as $spec => $data ) {
$tests = $data['tests'];
foreach( $tests as $test ) {
$output = '';
$output .= '--TEST--' . MY_EOL;
$output .= $test['name'] . MY_EOL;
$output .= '--DESCRIPTION--' . MY_EOL;
$output .= $test['desc'] . MY_EOL;
$output .= '--SKIPIF--' . MY_EOL;
$output .= "<?php if(!extension_loaded('mustache')) die('skip '); ?>" . MY_EOL;
$output .= '--FILE--' . MY_EOL;
$output .= '<?php' . MY_EOL;
// MAIN
$output .= '$test = ' . var_export($test, true) . ';' . MY_EOL;
$output .= '$mustache = new Mustache();' . MY_EOL;
if( !empty($test['partials']) && is_array($test['partials']) ) {
$output .= 'echo $mustache->render($test["template"], $test["data"], $test["partials"]);' . MY_EOL;
} else {
$output .= 'echo $mustache->render($test["template"], $test["data"]);' . MY_EOL;
}
// END MAIN
$output .= '?>' . MY_EOL;
// $output .= '--EXPECT--' . MY_EOL;
// $output .= $test['expected'];
$output .= '--EXPECTREGEX--' . MY_EOL;
$tmp = array();
foreach( preg_split('/\s+/', $test['expected']) as $chunk ) {
$tmp[] = preg_quote($chunk, '/');
}
$expected = join("\s*", $tmp);
// Hack in XFAIL
if( ($spec == 'partials' && $test['name'] == 'Standalone Line Endings') ||
($spec == 'partials' && $test['name'] == 'Standalone Without Previous Line') ) {
//$output .= MY_EOL;
//$output .= '--XFAIL--' . MY_EOL;
//$output .= 'This extension does not follow the spec\'s whitespace rules.';
$expected = "\s*" . join("\s*", str_split(preg_replace('/\s+/', '', $test['expected']), 1)) . "\s*";
}
$output .= $expected;
$cleanName = strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $test['name']), '-'));
file_put_contents('./tests/mustache-spec-' . $spec . '-' . $cleanName . '.phpt', $output);
}
}