File size: 7,572 Bytes
1e94262 | 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | #!/usr/bin/env php
<?php
if ($argc < 2) {
die("Usage: php bless_tests.php dir/\n");
}
$files = getFiles(array_slice($argv, 1));
foreach ($files as $path) {
if (!preg_match('/^(.*)\.phpt$/', $path, $matches)) {
// Not a phpt test
continue;
}
$outPath = $matches[1] . '.out';
if (!file_exists($outPath)) {
// Test did not fail
continue;
}
$phpt = file_get_contents($path);
$out = file_get_contents($outPath);
if (false !== strpos($phpt, '--XFAIL--')) {
// Don't modify expected output of XFAIL tests
continue;
}
// Don't update EXPECTREGEX tests
if (!preg_match('/--EXPECT(F?)--(.*)$/s', $phpt, $matches)) {
continue;
}
$oldExpect = trim($matches[2]);
$isFormat = $matches[1] == 'F';
if ($isFormat) {
$out = generateMinimallyDifferingOutput($out, $oldExpect);
} else {
$out = normalizeOutput($out);
}
$phpt = insertOutput($phpt, $out);
file_put_contents($path, $phpt);
}
function getFiles(array $dirsOrFiles): \Iterator {
foreach ($dirsOrFiles as $dirOrFile) {
if (is_dir($dirOrFile)) {
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirOrFile),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($it as $file) {
yield $file->getPathName();
}
} else if (is_file($dirOrFile)) {
yield $dirOrFile;
} else {
die("$dirOrFile is not a directory or file\n");
}
}
}
function normalizeOutput(string $out): string {
$out = preg_replace('/in (\/|[A-Z]:\\\\)\S+ on line \d+/m', 'in %s on line %d', $out);
$out = preg_replace('/in (\/|[A-Z]:\\\\)\S+:\d+/m', 'in %s:%d', $out);
$out = preg_replace('/\{closure:(\/|[A-Z]:\\\\)\S+:\d+\}/', '{closure:%s:%d}', $out);
$out = preg_replace('/object\(([A-Za-z0-9]*)\)#\d+/', 'object($1)#%d', $out);
$out = preg_replace('/^#(\d+) (\/|[A-Z]:\\\\)\S+\(\d+\):/m', '#$1 %s(%d):', $out);
$out = preg_replace('/Resource id #\d+/', 'Resource id #%d', $out);
$out = preg_replace('/resource\(\d+\) of type/', 'resource(%d) of type', $out);
$out = preg_replace(
'/Resource ID#\d+ used as offset, casting to integer \(\d+\)/',
'Resource ID#%d used as offset, casting to integer (%d)',
$out);
$out = preg_replace('/string\(\d+\) "([^"]*%d)/', 'string(%d) "$1', $out);
$out = str_replace("\0", '%0', $out);
return $out;
}
function formatToRegex(string $format): string {
$result = preg_quote($format, '/');
$result = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $result);
$result = str_replace('%s', '[^\r\n]+', $result);
$result = str_replace('%S', '[^\r\n]*', $result);
$result = str_replace('%w', '\s*', $result);
$result = str_replace('%i', '[+-]?\d+', $result);
$result = str_replace('%d', '\d+', $result);
$result = str_replace('%x', '[0-9a-fA-F]+', $result);
$result = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $result);
$result = str_replace('%c', '.', $result);
$result = str_replace('%0', '\0', $result);
return "/^$result$/s";
}
function generateMinimallyDifferingOutput(string $out, string $oldExpect) {
$outLines = explode("\n", $out);
$oldExpectLines = explode("\n", $oldExpect);
$differ = new Differ(function($oldExpect, $new) {
if (strpos($oldExpect, '%') === false) {
return $oldExpect === $new;
}
return preg_match(formatToRegex($oldExpect), $new);
});
$diff = $differ->diff($oldExpectLines, $outLines);
$result = [];
foreach ($diff as $elem) {
if ($elem->type == DiffElem::TYPE_KEEP) {
$result[] = $elem->old;
} else if ($elem->type == DiffElem::TYPE_ADD) {
$result[] = normalizeOutput($elem->new);
}
}
return implode("\n", $result);
}
function insertOutput(string $phpt, string $out): string {
return preg_replace_callback('/--EXPECTF?--.*?(--CLEAN--|$)/sD', function($matches) use($out) {
$hasWildcard = preg_match('/%[resSaAwidxfc0]/', $out);
$F = $hasWildcard ? 'F' : '';
return "--EXPECT$F--\n" . $out . "\n" . $matches[1];
}, $phpt);
}
/**
* Implementation of the the Myers diff algorithm.
*
* Myers, Eugene W. "An O (ND) difference algorithm and its variations."
* Algorithmica 1.1 (1986): 251-266.
*/
class DiffElem
{
const TYPE_KEEP = 0;
const TYPE_REMOVE = 1;
const TYPE_ADD = 2;
/** @var int One of the TYPE_* constants */
public $type;
/** @var mixed Is null for add operations */
public $old;
/** @var mixed Is null for remove operations */
public $new;
public function __construct(int $type, $old, $new) {
$this->type = $type;
$this->old = $old;
$this->new = $new;
}
}
class Differ
{
private $isEqual;
/**
* Create differ over the given equality relation.
*
* @param callable $isEqual Equality relation with signature function($a, $b) : bool
*/
public function __construct(callable $isEqual) {
$this->isEqual = $isEqual;
}
/**
* Calculate diff (edit script) from $old to $new.
*
* @param array $old Original array
* @param array $new New array
*
* @return DiffElem[] Diff (edit script)
*/
public function diff(array $old, array $new) {
list($trace, $x, $y) = $this->calculateTrace($old, $new);
return $this->extractDiff($trace, $x, $y, $old, $new);
}
private function calculateTrace(array $a, array $b) {
$n = \count($a);
$m = \count($b);
$max = $n + $m;
$v = [1 => 0];
$trace = [];
for ($d = 0; $d <= $max; $d++) {
$trace[] = $v;
for ($k = -$d; $k <= $d; $k += 2) {
if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
$x = $v[$k+1];
} else {
$x = $v[$k-1] + 1;
}
$y = $x - $k;
while ($x < $n && $y < $m && ($this->isEqual)($a[$x], $b[$y])) {
$x++;
$y++;
}
$v[$k] = $x;
if ($x >= $n && $y >= $m) {
return [$trace, $x, $y];
}
}
}
throw new \Exception('Should not happen');
}
private function extractDiff(array $trace, int $x, int $y, array $a, array $b) {
$result = [];
for ($d = \count($trace) - 1; $d >= 0; $d--) {
$v = $trace[$d];
$k = $x - $y;
if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
$prevK = $k + 1;
} else {
$prevK = $k - 1;
}
$prevX = $v[$prevK];
$prevY = $prevX - $prevK;
while ($x > $prevX && $y > $prevY) {
$result[] = new DiffElem(DiffElem::TYPE_KEEP, $a[$x-1], $b[$y-1]);
$x--;
$y--;
}
if ($d === 0) {
break;
}
while ($x > $prevX) {
$result[] = new DiffElem(DiffElem::TYPE_REMOVE, $a[$x-1], null);
$x--;
}
while ($y > $prevY) {
$result[] = new DiffElem(DiffElem::TYPE_ADD, null, $b[$y-1]);
$y--;
}
}
return array_reverse($result);
}
}
|