summaryrefslogtreecommitdiff
path: root/www/wiki/tests/parser/TestFileReader.php
blob: a96485d4d704f6c9457c8070f63fe361bd1e22a9 (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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Testing
 */

class TestFileReader {
	private $file;
	private $fh;
	private $section = null;
	/** String|null: current test section being analyzed */
	private $sectionData = [];
	private $sectionLineNum = [];
	private $lineNum = 0;
	private $runDisabled;
	private $runParsoid;
	private $regex;

	private $articles = [];
	private $requirements = [];
	private $tests = [];

	public static function read( $file, array $options = [] ) {
		$reader = new self( $file, $options );
		$reader->execute();

		$requirements = [];
		foreach ( $reader->requirements as $type => $reqsOfType ) {
			foreach ( $reqsOfType as $name => $unused ) {
				$requirements[] = [
					'type' => $type,
					'name' => $name
				];
			}
		}

		return [
			'requirements' => $requirements,
			'tests' => $reader->tests,
			'articles' => $reader->articles
		];
	}

	private function __construct( $file, $options ) {
		$this->file = $file;
		$this->fh = fopen( $this->file, "rt" );

		if ( !$this->fh ) {
			throw new MWException( "Couldn't open file '$file'\n" );
		}

		$options = $options + [
			'runDisabled' => false,
			'runParsoid' => false,
			'regex' => '//',
		];
		$this->runDisabled = $options['runDisabled'];
		$this->runParsoid = $options['runParsoid'];
		$this->regex = $options['regex'];
	}

	private function addCurrentTest() {
		// "input" and "result" are old section names allowed
		// for backwards-compatibility.
		$input = $this->checkSection( [ 'wikitext', 'input' ], false );
		$nonTidySection = $this->checkSection(
			[ 'html/php', 'html/*', 'html', 'result' ], false );
		// Some tests have "with tidy" and "without tidy" variants
		$tidySection = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );

		// Remove trailing newline
		$data = array_map( 'ParserTestRunner::chomp', $this->sectionData );

		// Apply defaults
		$data += [
			'options' => '',
			'config' => ''
		];

		if ( $input === false ) {
			throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
				"lacks input section" );
		}

		if ( preg_match( '/\\bdisabled\\b/i', $data['options'] ) &&	!$this->runDisabled ) {
			// Disabled
			return;
		}

		if ( $tidySection === false && $nonTidySection === false ) {
			if ( isset( $data['html/parsoid'] ) || isset( $data['wikitext/edited'] ) ) {
				// Parsoid only
				return;
			} else {
				throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
					"lacks result section" );
			}
		}

		if ( preg_match( '/\\bparsoid\\b/i', $data['options'] ) && $nonTidySection === 'html'
			&& !$this->runParsoid
		) {
			// A test which normally runs on Parsoid but can optionally be run with MW
			return;
		}

		if ( !preg_match( $this->regex, $data['test'] ) ) {
			// Filtered test
			return;
		}

		$commonInfo = [
			'test' => $data['test'],
			'desc' => $data['test'],
			'input' => $data[$input],
			'options' => $data['options'],
			'config' => $data['config'],
			'line' => $this->sectionLineNum['test'],
			'file' => $this->file
		];

		if ( $nonTidySection !== false ) {
			// Add non-tidy test
			$this->tests[] = [
				'result' => $data[$nonTidySection],
				'resultSection' => $nonTidySection
			] + $commonInfo;

			if ( $tidySection !== false ) {
				// Add tidy subtest
				$this->tests[] = [
					'desc' => $data['test'] . ' (with tidy)',
					'result' => $data[$tidySection],
					'resultSection' => $tidySection,
					'options' => $data['options'] . ' tidy',
					'isSubtest' => true,
				] + $commonInfo;
			}
		} elseif ( $tidySection !== false ) {
			// No need to override desc when there is no subtest
			$this->tests[] = [
				'result' => $data[$tidySection],
				'resultSection' => $tidySection,
				'options' => $data['options'] . ' tidy'
			] + $commonInfo;
		} else {
			throw new MWException( "Test at {$this->file}:{$this->sectionLineNum['test']} " .
				"lacks result section" );
		}
	}

	private function execute() {
		while ( false !== ( $line = fgets( $this->fh ) ) ) {
			$this->lineNum++;
			$matches = [];

			if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
				$this->section = strtolower( $matches[1] );

				if ( $this->section == 'endarticle' ) {
					$this->checkSection( 'text' );
					$this->checkSection( 'article' );

					$this->addArticle(
						ParserTestRunner::chomp( $this->sectionData['article'] ),
						$this->sectionData['text'], $this->lineNum );

					$this->clearSection();

					continue;
				}

				if ( $this->section == 'endhooks' ) {
					$this->checkSection( 'hooks' );

					foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
						$line = trim( $line );

						if ( $line ) {
							$this->addRequirement( 'hook', $line );
						}
					}

					$this->clearSection();

					continue;
				}

				if ( $this->section == 'endfunctionhooks' ) {
					$this->checkSection( 'functionhooks' );

					foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
						$line = trim( $line );

						if ( $line ) {
							$this->addRequirement( 'functionHook', $line );
						}
					}

					$this->clearSection();

					continue;
				}

				if ( $this->section == 'endtransparenthooks' ) {
					$this->checkSection( 'transparenthooks' );

					foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
						$line = trim( $line );

						if ( $line ) {
							$this->addRequirement( 'transparentHook', $line );
						}
					}

					$this->clearSection();

					continue;
				}

				if ( $this->section == 'end' ) {
					$this->checkSection( 'test' );
					$this->addCurrentTest();
					$this->clearSection();
					continue;
				}

				if ( isset( $this->sectionData[$this->section] ) ) {
					throw new MWException( "duplicate section '$this->section' "
						. "at line {$this->lineNum} of $this->file\n" );
				}

				$this->sectionLineNum[$this->section] = $this->lineNum;
				$this->sectionData[$this->section] = '';

				continue;
			}

			if ( $this->section ) {
				$this->sectionData[$this->section] .= $line;
			}
		}
	}

	/**
	 * Clear section name and its data
	 */
	private function clearSection() {
		$this->sectionLineNum = [];
		$this->sectionData = [];
		$this->section = null;
	}

	/**
	 * Verify the current section data has some value for the given token
	 * name(s) (first parameter).
	 * Throw an exception if it is not set, referencing current section
	 * and adding the current file name and line number
	 *
	 * @param string|array $tokens Expected token(s) that should have been
	 * mentioned before closing this section
	 * @param bool $fatal True iff an exception should be thrown if
	 * the section is not found.
	 * @return bool|string
	 * @throws MWException
	 */
	private function checkSection( $tokens, $fatal = true ) {
		if ( is_null( $this->section ) ) {
			throw new MWException( __METHOD__ . " can not verify a null section!\n" );
		}
		if ( !is_array( $tokens ) ) {
			$tokens = [ $tokens ];
		}
		if ( count( $tokens ) == 0 ) {
			throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
		}

		$data = $this->sectionData;
		$tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
			return isset( $data[$token] );
		} );

		if ( count( $tokens ) == 0 ) {
			if ( !$fatal ) {
				return false;
			}
			throw new MWException( sprintf(
				"'%s' without '%s' at line %s of %s\n",
				$this->section,
				implode( ',', $tokens ),
				$this->lineNum,
				$this->file
			) );
		}
		if ( count( $tokens ) > 1 ) {
			throw new MWException( sprintf(
				"'%s' with unexpected tokens '%s' at line %s of %s\n",
				$this->section,
				implode( ',', $tokens ),
				$this->lineNum,
				$this->file
			) );
		}

		return array_values( $tokens )[0];
	}

	private function addArticle( $name, $text, $line ) {
		$this->articles[] = [
			'name' => $name,
			'text' => $text,
			'line' => $line,
			'file' => $this->file
		];
	}

	private function addRequirement( $type, $name ) {
		$this->requirements[$type][$name] = true;
	}
}