1 <?php declare(strict_types=1);
7 public function parseTest($code, $chunksPerTest) {
8 $code = canonicalize($code);
10 // evaluate @@{expr}@@ expressions
11 $code = preg_replace_callback(
14 return eval('return ' . $matches[1] . ';');
20 $parts = preg_split("/\n-----(?:\n|$)/", $code);
22 // first part is the name
23 $name = array_shift($parts);
25 // multiple sections possible with always two forming a pair
26 $chunks = array_chunk($parts, $chunksPerTest);
28 foreach ($chunks as $i => $chunk) {
29 $lastPart = array_pop($chunk);
30 list($lastPart, $mode) = $this->extractMode($lastPart);
31 $tests[] = [$mode, array_merge($chunk, [$lastPart])];
34 return [$name, $tests];
37 public function reconstructTest($name, array $tests) {
39 foreach ($tests as list($mode, $parts)) {
40 $lastPart = array_pop($parts);
41 foreach ($parts as $part) {
42 $result .= "\n-----\n$part";
45 $result .= "\n-----\n";
47 $result .= "!!$mode\n";
54 private function extractMode($expected) {
55 $firstNewLine = strpos($expected, "\n");
56 if (false === $firstNewLine) {
57 $firstNewLine = strlen($expected);
60 $firstLine = substr($expected, 0, $firstNewLine);
61 if (0 !== strpos($firstLine, '!!')) {
62 return [$expected, null];
65 $expected = (string) substr($expected, $firstNewLine + 1);
66 return [$expected, substr($firstLine, 2)];