#!/www/server/bin/php -qC
<?php

# Tests for it.class

require 'searchlib/search_test.class';

function match( $regex, $string, $exp, $name )
{
	$GLOBALS['TEST_MORE_LEVEL'] = 1;
	$pass = is( it::match( $regex, $string ), $exp, $name );
	if( !$pass ) {
		diag( "        regex given: $regex" );
		diag( "    regex converted: " . it::convertregex( $regex ) );
	} 
	$GLOBALS['TEST_MORE_LEVEL'] = 0;
}

match(
	'b', 'aaaabaaaa',
	'b',
	'simple regex'
	);
match(
	'a/b', '   a/b   ',
	'a/b',
	'regex with /'
);
match(
	'aa(bb)aa(cc)aa(dd)qq', 'aabbaaccaaddqq',
	array( 'bb', 'cc', 'dd' ),
	'return array of captures'
	);
match(
	'\bblah\b', ' blah ',
	'blah',
	'match \b at spaces'
	);
match(
	'\bblah\b', 'blah',
	'blah',
	'match \b at end of string'
	);
match(
	'\bblah\b', 'ablahc',
	false,
	'don\'t match \b at word chars'
	);
match(
	'\bblah\b', '�blah�',
	false,
	'don\'t match \b at umlaute in latin1'
	);
match(
	'\Bblah\B', ' blah ',
	false,
	'don\'t match \B at spaces'
	);
match(
	'\Bblah\B', 'blah',
	false,
	'don\'t match \B at end of string'
	);
match(
	'\Bblah\B', 'ablahc',
	'blah',
	'match \B at word chars'
	);
match(
	'\Bblah\B', '�blah�',
	'blah',
	'match \B at umlaute in latin1'
	);
match(
	'\w+', '  |#�blah�   ',
	'�blah�',
	'include umlaute in \w'
	);
match(
	'\W+', '  |#�blah�  ',
	'  |#',
	'don\'t include umlaute in \W'
	);

eval( '$escapedwordregex = "' . it::convertregex( '\w' ) . '";' );
$escapedwordregex = preg_replace( '|[\\\\/]|', '', $escapedwordregex );

match(
	'\\\\w+',  $escapedwordregex,
	false,
	'don\'t parse \w in \\\\w at beginning (no match)'
	);
match(
	'aaa\\\\w+', '   aaa\www  ',
	'aaa\www',
	'don\'t parse \w in \\\\w at beginning (match)'
	);
match(
	'aaa\\\\w+', 'aaa' . $escapedwordregex,
	false,
	'don\'t parse \w in \\\\w after chars (no match)'
	);
match(
	'aaa\\\\w+', '   aaa\www  ',
	'aaa\www',
	'don\'t parse \w in \\\\w after chars (match)'
	);
match(
	'\\\\\\\\w+', '\\' . $escapedwordregex,
	false,
	'don\'t parse \w in \\\\\\\w (no match)'
	);
match(
	'\\\\\\\\w+', '  \\\\www  ',
	'\\\\www',
	'don\'t parse \\\\\\\\w as \w (match)'
	);
match(
	'[\w]+', '[[[]]]---',
	false,
	'replace \w in [\w] correctly (no match)'
	);
match(
	'[\w]+', '  \\\\aword[[[]]]   ',
	'aword',
	'replace \w in [\w] correctly (match)'
	);
match(
	'[\\\\w]+', ' blabergna ',
	false,
	'don\'t parse \w in [\\\\w] (no match)'
	);
match(
	'[\\\\w]+', '  \\\\worda[[[]',
	'\\\\w',
	'don\'t parse \w in [\\\\w] (match)'
	);
match(
	'[a\W]+', 'bbbbbbb a a%$+ accccc',
	' a a%$+ a',
	'\W in []'
	);
match(
	'\\\\\\w+', '  \�blah�  ',
	'\�blah�',
	'parse \w in \\\\\\w at beginning'
	);
match(
	'aaa\\\\\\w+', '  aaa\�blah�  ',
	'aaa\�blah�',
	'parse \w in \\\\\\w after chars'
	);
is(
	it::replace(
		array(
			'regex1' => 'repl1',
			'regex2' => 'repl2',
			'regex3' => 'repl3' ),
		'regex2 regex1 regex3' ),
	'repl2 repl1 repl3',
	'test tr regex function'
	);
is(
	it::match( '\w+', 'word1 w�rd2 word_3', array('all' => true )),
	array( 'word1', 'w�rd2', 'word_3' ),
	"test match_all function"
	);
match(
	'aBcD', '  aBcD  ',
	'aBcD',
	"caseinsensitive is default"
	);
match(
	'�', '�',
	'�',
	'match umlaute in latin1 case insensitive'
	);
is(
	it::match( 'abc', "aBc", array('casesensitive' => 1 )),
	false,
	"set case sensitivity by parameter"
	);

is(
	it::match( '\w+', 'word1 w�rd2 word_3', array('all' => 1 )),
	array( 'word1', 'w�rd2', 'word_3' ),
	"test all=>1 without captures"
	);
is(
	it::match( '\w+\s+(\d+)', 'word1 12 w�rd2 3 word_3 4', array('all' => 1 )),
	array( '12', '3', '4' ),
	"test all=>1 with one capture"
	);
is(
	it::match( '(\w+)\s+(\d+)', 'word1 12 w�rd2 3 word_3 4', array('all' => 1 )),
	array( array( 'word1', '12' ), array( 'w�rd2', '3' ), array( 'word_3', '4' ) ),
	"test all=>1 with captures"
	);
is(
	it::match( '(\w+)\s+(\d+)', 'word1 12 w�rd2 3 word_3 4', array('all' => 1, 'pattern_order' => 1 )),
	array( array( 'word1', 'w�rd2', 'word_3' ), array( '12', '3', '4' ) ),
	"test all=>1,pattern_order=>1"
	);
?>