Skip to content

Commit ef5c21e

Browse files
committed
Pin down REGEXP_* edge cases shared across functions
Adds a final layer of tests that exercise behaviors which involve more than one of REGEXP_LIKE / _REPLACE / _SUBSTR / _INSTR at once and only become testable once all four functions are available: - Empty pattern raises "Illegal argument to a regular expression." uniformly (MySQL ERROR 3685). - Empty subject with a zero-width-matching pattern still produces a match (LIKE = 1, SUBSTR = "", INSTR = 1). - Zero-width anchors ^ / $ report sensible 1-based positions and an empty-string match for SUBSTR rather than NULL. - Astral-plane (4-byte UTF-8) characters are counted as one code point by both SUBSTR and INSTR. - Negative pos rejects consistently across REPLACE / SUBSTR / INSTR.
1 parent 16b2895 commit ef5c21e

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,63 @@ public static function regexpOperators() {
266266
);
267267
}
268268

269+
public function testRegexpFunctionsWithTableColumns() {
270+
$this->assertQuery(
271+
"INSERT INTO _options (option_name, option_value) VALUES
272+
('test-ignore', 'unchanged'),
273+
('test-remove', 'unchanged'),
274+
('keep', 'unchanged');"
275+
);
276+
277+
$this->assertQuery(
278+
"SELECT
279+
option_name,
280+
REGEXP_REPLACE(option_name, '(-ignore|-remove)\$', '') AS replacement_result,
281+
REGEXP_SUBSTR(option_name, '[^-]+\$') AS substring_result,
282+
REGEXP_INSTR(option_name, '-') AS position_result
283+
FROM _options
284+
WHERE REGEXP_LIKE(option_name, '^test-')
285+
ORDER BY option_name"
286+
);
287+
$this->assertEquals(
288+
array(
289+
(object) array(
290+
'option_name' => 'test-ignore',
291+
'replacement_result' => 'test',
292+
'substring_result' => 'ignore',
293+
'position_result' => '5',
294+
),
295+
(object) array(
296+
'option_name' => 'test-remove',
297+
'replacement_result' => 'test',
298+
'substring_result' => 'remove',
299+
'position_result' => '5',
300+
),
301+
),
302+
$this->last_result
303+
);
304+
305+
$this->assertQuery(
306+
"UPDATE _options
307+
SET option_value = REGEXP_REPLACE(option_name, '^test-', '')
308+
WHERE REGEXP_LIKE(option_name, '^test-')"
309+
);
310+
$this->assertQuery( "SELECT option_name, option_value FROM _options WHERE option_value != 'unchanged' ORDER BY option_name" );
311+
$this->assertEquals(
312+
array(
313+
(object) array(
314+
'option_name' => 'test-ignore',
315+
'option_value' => 'ignore',
316+
),
317+
(object) array(
318+
'option_name' => 'test-remove',
319+
'option_value' => 'remove',
320+
),
321+
),
322+
$this->last_result
323+
);
324+
}
325+
269326
/**
270327
* @dataProvider regexpLikeCases
271328
*/
@@ -840,6 +897,57 @@ public function testRegexpInstrNumericSubject() {
840897
$this->assertSame( '2', $this->last_result[0]->r );
841898
}
842899

900+
public function testRegexpEmptyPatternRejected() {
901+
// MySQL rejects the empty pattern with ERROR 3685.
902+
$this->assertQueryError(
903+
"SELECT REGEXP_LIKE('abc', '')",
904+
'Illegal argument to a regular expression.'
905+
);
906+
$this->assertQueryError(
907+
"SELECT REGEXP_REPLACE('abc', '', 'x')",
908+
'Illegal argument to a regular expression.'
909+
);
910+
$this->assertQueryError(
911+
"SELECT REGEXP_SUBSTR('abc', '')",
912+
'Illegal argument to a regular expression.'
913+
);
914+
$this->assertQueryError(
915+
"SELECT REGEXP_INSTR('abc', '')",
916+
'Illegal argument to a regular expression.'
917+
);
918+
}
919+
920+
public function testRegexpEmptySubject() {
921+
// A pattern that matches empty string still matches against an empty subject.
922+
$this->assertQuery( "SELECT REGEXP_LIKE('', 'a*') AS r" );
923+
$this->assertSame( '1', $this->last_result[0]->r );
924+
$this->assertQuery( "SELECT REGEXP_SUBSTR('', 'a*') AS r" );
925+
$this->assertSame( '', $this->last_result[0]->r );
926+
$this->assertQuery( "SELECT REGEXP_INSTR('', 'a*') AS r" );
927+
$this->assertSame( '1', $this->last_result[0]->r );
928+
}
929+
930+
public function testRegexpZeroWidthAnchors() {
931+
// ^ matches at position 1 (length 0).
932+
$this->assertQuery( "SELECT REGEXP_INSTR('abc', '^') AS r" );
933+
$this->assertSame( '1', $this->last_result[0]->r );
934+
// $ matches one past the last character.
935+
$this->assertQuery( "SELECT REGEXP_INSTR('abc', '\$') AS r" );
936+
$this->assertSame( '4', $this->last_result[0]->r );
937+
// SUBSTR of a zero-width anchor is the empty string, not NULL.
938+
$this->assertQuery( "SELECT REGEXP_SUBSTR('abc', '^') AS r" );
939+
$this->assertSame( '', $this->last_result[0]->r );
940+
}
941+
942+
public function testRegexpAstralPlaneCharacter() {
943+
// 4-byte UTF-8 encodes as one code point; char offsets should reflect that.
944+
// "x😀y" has three characters (x at 1, 😀 at 2, y at 3).
945+
$this->assertQuery( "SELECT REGEXP_SUBSTR('x😀y', '.', 2) AS r" );
946+
$this->assertSame( '😀', $this->last_result[0]->r );
947+
$this->assertQuery( "SELECT REGEXP_INSTR('😀z', 'z', 1, 1, 1) AS r" );
948+
$this->assertSame( '3', $this->last_result[0]->r );
949+
}
950+
843951
public function testInsertDateNow() {
844952
$this->assertQuery(
845953
"INSERT INTO _dates (option_name, option_value) VALUES ('first', now());"

0 commit comments

Comments
 (0)