mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-23 13:46:48 +00:00
Improve code coverage for AbuseFilterParser
Add some tests and improve others to raise coverage percentage. This should lead to almost 100% for the AbuseFilterParser class. Aside from this, a couple of changes: * Remove an unused function * Let equals_to_any return a genuine result with empty strings * Remove an if which will never be true in skipOverBraces, since the function is called after checking the same conditions. Bug: T201193 Change-Id: I7020b2ed996236c38c5784d161ad98ec44163406
This commit is contained in:
parent
50a295a6e7
commit
4f3b020f5d
|
@ -159,12 +159,6 @@ class AbuseFilterParser {
|
|||
* @throws AFPUserVisibleException
|
||||
*/
|
||||
protected function skipOverBraces() {
|
||||
if ( !( $this->mCur->type == AFPToken::TBRACE && $this->mCur->value == '(' ) ||
|
||||
!$this->mShortCircuit
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$braces = 1;
|
||||
while ( $this->mCur->type != AFPToken::TNONE && $braces > 0 ) {
|
||||
$this->move();
|
||||
|
@ -215,19 +209,6 @@ class AbuseFilterParser {
|
|||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return int
|
||||
*/
|
||||
public static function lengthCompare( $a, $b ) {
|
||||
if ( strlen( $a ) == strlen( $b ) ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ( strlen( $a ) < strlen( $b ) ) ? -1 : 1;
|
||||
}
|
||||
|
||||
/* Levels */
|
||||
|
||||
/**
|
||||
|
@ -1346,10 +1327,6 @@ class AbuseFilterParser {
|
|||
protected static function equalsToAny( $string, $values ) {
|
||||
$string = $string->toString();
|
||||
|
||||
if ( $string === '' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $values as $needle ) {
|
||||
$needle = $needle->toString();
|
||||
|
||||
|
|
1
tests/parserTests/atombraces.r
Normal file
1
tests/parserTests/atombraces.r
Normal file
|
@ -0,0 +1 @@
|
|||
NOT MATCH
|
1
tests/parserTests/atombraces.t
Normal file
1
tests/parserTests/atombraces.t
Normal file
|
@ -0,0 +1 @@
|
|||
( )
|
1
tests/parserTests/containsfunction.r
Normal file
1
tests/parserTests/containsfunction.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
2
tests/parserTests/containsfunction.t
Normal file
2
tests/parserTests/containsfunction.t
Normal file
|
@ -0,0 +1,2 @@
|
|||
contains_any( "", "a") === false &
|
||||
contains_any( "a", "", "a") === true
|
|
@ -1,6 +1,8 @@
|
|||
count("a,b,c,d") = 4 &
|
||||
count(",", "a,b,c,d") = 3 &
|
||||
count("", "abcd") = 0 &
|
||||
count("a", "abab") = 2 &
|
||||
count("ab", "abab") = 2 &
|
||||
count("aa", "aaaaa") = 2
|
||||
count("a,b,c,d") === 4 &
|
||||
count(",", "a,b,c,d") === 3 &
|
||||
count("", "abcd") === 0 &
|
||||
count("a", "abab") === 2 &
|
||||
count("ab", "abab") === 2 &
|
||||
count("aa", "aaaaa") === 2 &
|
||||
count( [ "a", "b", "c" ] ) === 3 &
|
||||
count( [] ) === 0
|
||||
|
|
|
@ -1 +1,4 @@
|
|||
equals_to_any("foo", "bar", "foo", "pizza") & equals_to_any(15, 3, 77, 18, 15)
|
||||
equals_to_any( "foo", "bar", "foo", "pizza" ) &
|
||||
equals_to_any( 15, 3, 77, 18, 15 ) &
|
||||
equals_to_any( "", 3, 77, 18, 15, "duh" ) === false &
|
||||
equals_to_any( "", 3, 77, 18, 15, "duh", "" )
|
|
@ -1 +1,2 @@
|
|||
(if 1 then 2 else 3 end) == 2
|
||||
(if 1 then 2 else 3 end) === 2 &
|
||||
(if false then 2 else 3 end) === 3
|
||||
|
|
1
tests/parserTests/lazyboolinvert.r
Normal file
1
tests/parserTests/lazyboolinvert.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/lazyboolinvert.t
Normal file
1
tests/parserTests/lazyboolinvert.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | !true
|
1
tests/parserTests/lazyfunction.r
Normal file
1
tests/parserTests/lazyfunction.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/lazyfunction.t
Normal file
1
tests/parserTests/lazyfunction.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | contains_any( "a", "b")
|
1
tests/parserTests/lazykeyword.r
Normal file
1
tests/parserTests/lazykeyword.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/lazykeyword.t
Normal file
1
tests/parserTests/lazykeyword.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | "a" like "b"
|
1
tests/parserTests/lazypow.r
Normal file
1
tests/parserTests/lazypow.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/lazypow.t
Normal file
1
tests/parserTests/lazypow.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | 2**3 === 8
|
1
tests/parserTests/lazysum.r
Normal file
1
tests/parserTests/lazysum.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/lazysum.t
Normal file
1
tests/parserTests/lazysum.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | 2 + 3 === 5
|
1
tests/parserTests/lazyunarys.r
Normal file
1
tests/parserTests/lazyunarys.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/lazyunarys.t
Normal file
1
tests/parserTests/lazyunarys.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | -4 !== 4
|
1
tests/parserTests/multipleskipbraces.r
Normal file
1
tests/parserTests/multipleskipbraces.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
1
tests/parserTests/multipleskipbraces.t
Normal file
1
tests/parserTests/multipleskipbraces.t
Normal file
|
@ -0,0 +1 @@
|
|||
1 === 1 | ( ( ( 3 === 2 ) ) )
|
1
tests/parserTests/rcount.r
Normal file
1
tests/parserTests/rcount.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
3
tests/parserTests/rcount.t
Normal file
3
tests/parserTests/rcount.t
Normal file
|
@ -0,0 +1,3 @@
|
|||
rcount("a,b,c,d") = 4 &
|
||||
rcount(".", "abcd") = 4
|
||||
|
1
tests/parserTests/rmwhitespace.r
Normal file
1
tests/parserTests/rmwhitespace.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
2
tests/parserTests/rmwhitespace.t
Normal file
2
tests/parserTests/rmwhitespace.t
Normal file
|
@ -0,0 +1,2 @@
|
|||
rmwhitespace( "foobar" ) === "foobar" &
|
||||
rmwhitespace( "foo bar bar foo" ) === "foobarbarfoo"
|
|
@ -1 +1,2 @@
|
|||
specialratio("foó;") = 0.25
|
||||
specialratio("foó;") === 0.25 &
|
||||
specialratio("") === 0.0
|
||||
|
|
1
tests/parserTests/strpos.r
Normal file
1
tests/parserTests/strpos.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
4
tests/parserTests/strpos.t
Normal file
4
tests/parserTests/strpos.t
Normal file
|
@ -0,0 +1,4 @@
|
|||
strpos( "foobarfoo", "foo" ) === 0 &
|
||||
strpos( "foobarfoo", "" ) === -1 &
|
||||
strpos( "foobarfoo", "foo", 1 ) === 6 &
|
||||
strpos( "foobarfoo", "lol" ) === -1
|
1
tests/parserTests/substr.r
Normal file
1
tests/parserTests/substr.r
Normal file
|
@ -0,0 +1 @@
|
|||
MATCH
|
2
tests/parserTests/substr.t
Normal file
2
tests/parserTests/substr.t
Normal file
|
@ -0,0 +1,2 @@
|
|||
substr( "foobar", 0, 3 ) === "foo" &
|
||||
substr( "barfoo", 4 ) === "oo"
|
|
@ -52,8 +52,9 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
static $parsers = null;
|
||||
if ( !$parsers ) {
|
||||
$parsers = [
|
||||
new AbuseFilterParser(),
|
||||
new AbuseFilterCachingParser()
|
||||
new AbuseFilterParser()
|
||||
// @ToDo: Here we should also instantiate an AbuseFilterCachingParser as we'll have
|
||||
// fixed its problems (T156095). Right now it may break otherwise working tests (see T201193)
|
||||
];
|
||||
}
|
||||
return $parsers;
|
||||
|
@ -147,26 +148,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* get_matches should throw an exception with an invalid number of arguments.
|
||||
* @expectedException AFPUserVisibleException
|
||||
* @covers AbuseFilterParser::funcGetMatches
|
||||
*/
|
||||
public function testGetMatchesInvalidArgs() {
|
||||
$parser = self::getParser();
|
||||
$parser->parse( "get_matches('')" );
|
||||
}
|
||||
|
||||
/**
|
||||
* get_matches should throw an exception when given an invalid regular expression.
|
||||
* @expectedException AFPUserVisibleException
|
||||
* @covers AbuseFilterParser::funcGetMatches
|
||||
*/
|
||||
public function testGetMatchesInvalidRegex() {
|
||||
$parser = self::getParser();
|
||||
$parser->parse( "get_matches('this (should fail')" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure get_matches function captures returns expected output.
|
||||
* @param string $needle Regex to pass to get_matches.
|
||||
|
@ -498,6 +479,7 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::funcRCount
|
||||
* @covers AbuseFilterParser::funcGetMatches
|
||||
* @dataProvider regexFailure
|
||||
*/
|
||||
public function testRegexFailureException( $expr, $caller ) {
|
||||
|
@ -514,6 +496,7 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
public function regexFailure() {
|
||||
return [
|
||||
[ "rcount('(','a')", 'funcRCount' ],
|
||||
[ "get_matches('this (should fail', 'any haystack')", 'funcGetMatches' ],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue