Skip to content

Commit d66c92b

Browse files
committed
Allow negated match
1 parent 8822837 commit d66c92b

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

features/bootstrap/support.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ function assert_regex( $regex, $actual ) {
1515
}
1616
}
1717

18+
function assert_not_regex( $regex, $actual ) {
19+
if ( preg_match( $regex, $actual ) ) {
20+
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
21+
}
22+
}
23+
1824
function assert_equals( $expected, $actual ) {
1925
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Deliberate loose comparison.
2026
if ( $expected != $actual ) {

features/steps/then.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,31 @@ function ( $world, $path, $type, $action, $expected = null ) {
225225
);
226226

227227
$steps->Then(
228-
'/^the contents of the (.+) file should match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
229-
function ( $world, $path, $expected ) {
228+
'/^the contents of the (.+) file should( not)? match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
229+
function ( $world, $path, $not, $expected ) {
230230
$path = $world->replace_variables( $path );
231231
// If it's a relative path, make it relative to the current test dir.
232232
if ( '/' !== $path[0] ) {
233233
$path = $world->variables['RUN_DIR'] . "/$path";
234234
}
235235
$contents = file_get_contents( $path );
236-
Support\assert_regex( $expected, $contents );
236+
if ( $not ) {
237+
Support\assert_not_regex( $expected, $contents );
238+
} else {
239+
Support\assert_regex( $expected, $contents );
240+
}
237241
}
238242
);
239243

240244
$steps->Then(
241-
'/^(STDOUT|STDERR) should match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
242-
function ( $world, $stream, $expected ) {
245+
'/^(STDOUT|STDERR) should( not)? match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
246+
function ( $world, $stream, $not, $expected ) {
243247
$stream = strtolower( $stream );
244-
Support\assert_regex( $expected, $world->result->$stream );
248+
if ( $not ) {
249+
Support\assert_not_regex( $expected, $world->result->$stream );
250+
} else {
251+
Support\assert_regex( $expected, $world->result->$stream );
252+
}
245253
}
246254
);
247255

0 commit comments

Comments
 (0)