Skip to content

Implement Assert::matches()#23

Merged
thekid merged 4 commits intomainfrom
feature/matches
May 18, 2023
Merged

Implement Assert::matches()#23
thekid merged 4 commits intomainfrom
feature/matches

Conversation

@thekid
Copy link
Copy Markdown
Member

@thekid thekid commented May 18, 2023

Code that asserts using preg_match() does not produce meaningful error messages:

$ xp -e '\test\Assert::equals(1, preg_match("/ABC/i", "Testing"))'
Uncaught exception: Exception test.AssertionFailed (Failed asserting that 0 is equal to 1)
# ...

This pull request solves the problem by adding a matches shorthand method which uses the test.assert.Matches class.

Usage

The pattern is included in the assertion error message:

Assert::matches('/^test/i', 'Testing');
// Pass

Assert::matches('/ABC/i', 'Testing');
// Uncaught exception: Exception test.AssertionFailed (Failed asserting that "Testing" matches /ABC/i)
//   at test.Assert::matches((0x6)'/ABC/i', (0x7)'Testing') [line 1 of (command line argument)]
//   at <main>::include((0x17)'(command line argument)') [line 163 of Code.class.php]
//   at xp.runtime.Code::run(array[1]) [line 31 of Evaluate.class.php]
//   at xp.runtime.Evaluate::main(array[1]) [line 389 of class-main.php]

Errors

If preg_match() returns an error, an exception is raised. Its stacktrace includes the error message

Assert::matches('not.a.regex', 'Testing');
// Uncaught exception: Exception lang.FormatException (Using not.a.regex)
//   at <main>::preg_match() [line 15 of Matches.class.php] preg_match(): Delimiter must not be alphanumeric, [...]
//   at test.assert.Matches::matches((0x7)'Testing') [line 27 of Assertable.class.php]
//   at test.assert.Assertable::is(test.assert.Matches{}) [line 89 of Assert.class.php]
//   at test.Assert::matches((0xb)'not.a.regex', (0x7)'Testing') [line 1 of (command line argument)]
//   at <main>::include((0x17)'(command line argument)') [line 163 of Code.class.php]
//   at xp.runtime.Code::run(array[1]) [line 31 of Evaluate.class.php]
//   at xp.runtime.Evaluate::main(array[1]) [line 389 of class-main.php]

@thekid thekid merged commit 92c847f into main May 18, 2023
@thekid thekid deleted the feature/matches branch May 18, 2023 11:48
thekid added a commit to xp-forge/mongodb that referenced this pull request May 18, 2023
thekid added a commit to xp-forge/sessions that referenced this pull request May 18, 2023
thekid added a commit to xp-framework/compiler that referenced this pull request May 18, 2023
thekid added a commit to xp-forge/frontend that referenced this pull request May 18, 2023
@thekid
Copy link
Copy Markdown
Member Author

thekid commented May 18, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant