-class ParserTest {
- function runTestsFromFile( $filename ) {
- $infile = fopen( $filename, 'rt' );
- if( !$infile ) {
- die( "Couldn't open parserTests.txt\n" );
- }
-
- $data = array();
- $section = null;
- $success = 0;
- $total = 0;
- $n = 0;
- while( false !== ($line = fgets( $infile ) ) ) {
- $n++;
- if (is_null($section) && preg_match('/^#/', $line)) {
- # skip comment
- continue;
- }
- if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
- $section = strtolower( $matches[1] );
- if( $section == 'end' ) {
- if( !isset( $data['test'] ) ) {
- die( "'end' without 'test' at line $n\n" );
- }
- if( !isset( $data['input'] ) ) {
- die( "'end' without 'input' at line $n\n" );
- }
- if( !isset( $data['result'] ) ) {
- die( "'end' without 'result' at line $n\n" );
- }
- if( $this->runTest(
- rtrim( $data['test'] ),
- rtrim( $data['input'] ),
- rtrim( $data['result'] ) ) ) {
- $success++;
- }
- $total++;
- $data = array();
- $section = null;
- continue;
- }
- $data[$section] = '';
- continue;
- }
- if( $section ) {
- $data[$section] .= $line;
- }
- }
- if( $total > 0 ) {
- $ratio = IntVal( 100.0 * $success / $total );
- print "\nPassed $success of $total tests ($ratio%)\n";
- }
- }