summaryrefslogtreecommitdiff
path: root/www/wiki/tests/parser/TestFileEditor.php
blob: 1bee31eaa4f88aa31f6fbfcd5af17a4cfa47858b (plain)
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
<?php

class TestFileEditor {
	private $lines;
	private $numLines;
	private $deletions;
	private $changes;
	private $pos;
	private $warningCallback;
	private $result;

	public static function edit( $text, array $deletions, array $changes, $warningCallback = null ) {
		$editor = new self( $text, $deletions, $changes, $warningCallback );
		$editor->execute();
		return $editor->result;
	}

	private function __construct( $text, array $deletions, array $changes, $warningCallback ) {
		$this->lines = explode( "\n", $text );
		$this->numLines = count( $this->lines );
		$this->deletions = array_flip( $deletions );
		$this->changes = $changes;
		$this->pos = 0;
		$this->warningCallback = $warningCallback;
		$this->result = '';
	}

	private function execute() {
		while ( $this->pos < $this->numLines ) {
			$line = $this->lines[$this->pos];
			switch ( $this->getHeading( $line ) ) {
				case 'test':
					$this->parseTest();
					break;
				case 'hooks':
				case 'functionhooks':
				case 'transparenthooks':
					$this->parseHooks();
					break;
				default:
					if ( $this->pos < $this->numLines - 1 ) {
						$line .= "\n";
					}
					$this->emitComment( $line );
					$this->pos++;
			}
		}
		foreach ( $this->deletions as $deletion => $unused ) {
			$this->warning( "Could not find test \"$deletion\" to delete it" );
		}
		foreach ( $this->changes as $test => $sectionChanges ) {
			foreach ( $sectionChanges as $section => $change ) {
				$this->warning( "Could not find section \"$section\" in test \"$test\" " .
					"to {$change['op']} it" );
			}
		}
	}

	private function warning( $text ) {
		$cb = $this->warningCallback;
		if ( $cb ) {
			$cb( $text );
		}
	}

	private function getHeading( $line ) {
		if ( preg_match( '/^!!\s*(\S+)/', $line, $m ) ) {
			return $m[1];
		} else {
			return false;
		}
	}

	private function parseTest() {
		$test = [];
		$line = $this->lines[$this->pos++];
		$heading = $this->getHeading( $line );
		$section = [
			'name' => $heading,
			'headingLine' => $line,
			'contents' => ''
		];

		while ( $this->pos < $this->numLines ) {
			$line = $this->lines[$this->pos++];
			$nextHeading = $this->getHeading( $line );
			if ( $nextHeading === 'end' ) {
				$test[] = $section;

				// Add trailing line breaks to the "end" section, to allow for neat deletions
				$trail = '';
				for ( $i = 0; $i < $this->numLines - $this->pos - 1; $i++ ) {
					if ( $this->lines[$this->pos + $i] === '' ) {
						$trail .= "\n";
					} else {
						break;
					}
				}
				$this->pos += strlen( $trail );

				$test[] = [
					'name' => 'end',
					'headingLine' => $line,
					'contents' => $trail
				];
				$this->emitTest( $test );
				return;
			} elseif ( $nextHeading !== false ) {
				$test[] = $section;
				$heading = $nextHeading;
				$section = [
					'name' => $heading,
					'headingLine' => $line,
					'contents' => ''
				];
			} else {
				$section['contents'] .= "$line\n";
			}
		}

		throw new Exception( 'Unexpected end of file' );
	}

	private function parseHooks() {
		$line = $this->lines[$this->pos++];
		$heading = $this->getHeading( $line );
		$expectedEnd = 'end' . $heading;
		$contents = "$line\n";

		do {
			$line = $this->lines[$this->pos++];
			$nextHeading = $this->getHeading( $line );
			$contents .= "$line\n";
		} while ( $this->pos < $this->numLines && $nextHeading !== $expectedEnd );

		if ( $nextHeading !== $expectedEnd ) {
			throw new Exception( 'Unexpected end of file' );
		}
		$this->emitHooks( $heading, $contents );
	}

	protected function emitComment( $contents ) {
		$this->result .= $contents;
	}

	protected function emitTest( $test ) {
		$testName = false;
		foreach ( $test as $section ) {
			if ( $section['name'] === 'test' ) {
				$testName = rtrim( $section['contents'], "\n" );
			}
		}
		if ( isset( $this->deletions[$testName] ) ) {
			// Acknowledge deletion
			unset( $this->deletions[$testName] );
			return;
		}
		if ( isset( $this->changes[$testName] ) ) {
			$changes =& $this->changes[$testName];
			foreach ( $test as $i => $section ) {
				$sectionName = $section['name'];
				if ( isset( $changes[$sectionName] ) ) {
					$change = $changes[$sectionName];
					switch ( $change['op'] ) {
						case 'rename':
							$test[$i]['name'] = $change['value'];
							$test[$i]['headingLine'] = "!! {$change['value']}";
							break;
						case 'update':
							$test[$i]['contents'] = $change['value'];
							break;
						case 'delete':
							$test[$i]['deleted'] = true;
							break;
						default:
							throw new Exception( "Unknown op: ${change['op']}" );
					}
					// Acknowledge
					// Note that we use the old section name for the rename op
					unset( $changes[$sectionName] );
				}
			}
		}
		foreach ( $test as $section ) {
			if ( isset( $section['deleted'] ) ) {
				continue;
			}
			$this->result .= $section['headingLine'] . "\n";
			$this->result .= $section['contents'];
		}
	}

	protected function emitHooks( $heading, $contents ) {
		$this->result .= $contents;
	}
}