| 1 |
<?php |
| 2 |
|
| 3 |
class TestFileEditor { |
| 4 |
|
| 5 |
private $lines; |
| 6 |
|
| 7 |
private $numLines; |
| 8 |
|
| 9 |
private $deletions; |
| 10 |
|
| 11 |
private $changes; |
| 12 |
|
| 13 |
private $pos = 0; |
| 14 |
|
| 15 |
private $warningCallback; |
| 16 |
|
| 17 |
private $result = ''; |
| 18 |
|
| 19 |
public static function edit( $text, array $deletions, array $changes, $warningCallback = null ) { |
| 20 |
$editor = new self( $text, $deletions, $changes, $warningCallback ); |
| 21 |
$editor->execute(); |
| 22 |
return $editor->result; |
| 23 |
} |
| 24 |
|
| 25 |
private function __construct( $text, array $deletions, array $changes, $warningCallback ) { |
| 26 |
$this->lines = explode( "\n", $text ); |
| 27 |
$this->numLines = count( $this->lines ); |
| 28 |
$this->deletions = array_fill_keys( $deletions, true ); |
| 29 |
$this->changes = $changes; |
| 30 |
$this->warningCallback = $warningCallback; |
| 31 |
} |
| 32 |
|
| 33 |
private function execute() { |
| 34 |
while ( $this->pos < $this->numLines ) { |
| 35 |
$line = $this->lines[$this->pos]; |
| 36 |
switch ( $this->getHeading( $line ) ) { |
| 37 |
case 'test': |
| 38 |
$this->parseTest(); |
| 39 |
break; |
| 40 |
case 'hooks': |
| 41 |
case 'functionhooks': |
| 42 |
$this->parseHooks(); |
| 43 |
break; |
| 44 |
default: |
| 45 |
if ( $this->pos < $this->numLines - 1 ) { |
| 46 |
$line .= "\n"; |
| 47 |
} |
| 48 |
$this->emitComment( $line ); |
| 49 |
$this->pos++; |
| 50 |
} |
| 51 |
} |
| 52 |
foreach ( $this->deletions as $deletion => $unused ) { |
| 53 |
$this->warning( "Could not find test \"$deletion\" to delete it" ); |
| 54 |
} |
| 55 |
foreach ( $this->changes as $test => $sectionChanges ) { |
| 56 |
foreach ( $sectionChanges as $section => $change ) { |
| 57 |
$this->warning( "Could not find section \"$section\" in test \"$test\" " . |
| 58 |
"to {$change['op']} it" ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private function warning( $text ) { |
| 64 |
$cb = $this->warningCallback; |
| 65 |
if ( $cb ) { |
| 66 |
$cb( $text ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
private function getHeading( $line ) { |
| 71 |
if ( preg_match( '/^!!\s*(\S+)/', $line, $m ) ) { |
| 72 |
return $m[1]; |
| 73 |
} else { |
| 74 |
return false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
private function parseTest() { |
| 79 |
$test = []; |
| 80 |
$line = $this->lines[$this->pos++]; |
| 81 |
$heading = $this->getHeading( $line ); |
| 82 |
$section = [ |
| 83 |
'name' => $heading, |
| 84 |
'headingLine' => $line, |
| 85 |
'contents' => '' |
| 86 |
]; |
| 87 |
|
| 88 |
while ( $this->pos < $this->numLines ) { |
| 89 |
$line = $this->lines[$this->pos++]; |
| 90 |
$nextHeading = $this->getHeading( $line ); |
| 91 |
if ( $nextHeading === 'end' ) { |
| 92 |
$test[] = $section; |
| 93 |
|
| 94 |
|
| 95 |
$trail = ''; |
| 96 |
while ( $this->lines[$this->pos] === '' && $this->pos < $this->numLines - 1 ) { |
| 97 |
$trail .= "\n"; |
| 98 |
$this->pos++; |
| 99 |
} |
| 100 |
|
| 101 |
$test[] = [ |
| 102 |
'name' => 'end', |
| 103 |
'headingLine' => $line, |
| 104 |
'contents' => $trail |
| 105 |
]; |
| 106 |
$this->emitTest( $test ); |
| 107 |
return; |
| 108 |
} elseif ( $nextHeading !== false ) { |
| 109 |
$test[] = $section; |
| 110 |
$heading = $nextHeading; |
| 111 |
$section = [ |
| 112 |
'name' => $heading, |
| 113 |
'headingLine' => $line, |
| 114 |
'contents' => '' |
| 115 |
]; |
| 116 |
} else { |
| 117 |
$section['contents'] .= "$line\n"; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
throw new OutOfBoundsException( 'Unexpected end of file' ); |
| 122 |
} |
| 123 |
|
| 124 |
private function parseHooks() { |
| 125 |
$line = $this->lines[$this->pos++]; |
| 126 |
$heading = $this->getHeading( $line ); |
| 127 |
$expectedEnd = 'end' . $heading; |
| 128 |
$contents = "$line\n"; |
| 129 |
|
| 130 |
do { |
| 131 |
$line = $this->lines[$this->pos++]; |
| 132 |
$nextHeading = $this->getHeading( $line ); |
| 133 |
$contents .= "$line\n"; |
| 134 |
} while ( $this->pos < $this->numLines && $nextHeading !== $expectedEnd ); |
| 135 |
|
| 136 |
if ( $nextHeading !== $expectedEnd ) { |
| 137 |
throw new UnexpectedValueException( 'Unexpected end of file' ); |
| 138 |
} |
| 139 |
$this->emitHooks( $heading, $contents ); |
| 140 |
} |
| 141 |
|
| 142 |
protected function emitComment( $contents ) { |
| 143 |
$this->result .= $contents; |
| 144 |
} |
| 145 |
|
| 146 |
protected function emitTest( $test ) { |
| 147 |
$testName = false; |
| 148 |
foreach ( $test as $section ) { |
| 149 |
if ( $section['name'] === 'test' ) { |
| 150 |
$testName = rtrim( $section['contents'], "\n" ); |
| 151 |
} |
| 152 |
} |
| 153 |
if ( isset( $this->deletions[$testName] ) ) { |
| 154 |
|
| 155 |
unset( $this->deletions[$testName] ); |
| 156 |
return; |
| 157 |
} |
| 158 |
if ( isset( $this->changes[$testName] ) ) { |
| 159 |
$changes =& $this->changes[$testName]; |
| 160 |
foreach ( $test as $i => $section ) { |
| 161 |
$sectionName = $section['name']; |
| 162 |
if ( isset( $changes[$sectionName] ) ) { |
| 163 |
$change = $changes[$sectionName]; |
| 164 |
switch ( $change['op'] ) { |
| 165 |
case 'rename': |
| 166 |
$test[$i]['name'] = $change['value']; |
| 167 |
$test[$i]['headingLine'] = "!! {$change['value']}"; |
| 168 |
break; |
| 169 |
case 'update': |
| 170 |
$test[$i]['contents'] = $change['value']; |
| 171 |
break; |
| 172 |
case 'delete': |
| 173 |
$test[$i]['deleted'] = true; |
| 174 |
break; |
| 175 |
default: |
| 176 |
throw new UnexpectedValueException( "Unknown op: {$change['op']}" ); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
unset( $changes[$sectionName] ); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
foreach ( $test as $section ) { |
| 185 |
if ( isset( $section['deleted'] ) ) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
$this->result .= $section['headingLine'] . "\n"; |
| 189 |
$this->result .= $section['contents']; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
protected function emitHooks( $heading, $contents ) { |
| 194 |
$this->result .= $contents; |
| 195 |
} |
| 196 |
} |