From 2b11442f0123b057102cb4592a1bda113fbdb095 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Fri, 6 Dec 2024 18:27:09 -0500 Subject: [PATCH] Add more edge case testing with failing case --- tests/Support/SupportStrTest.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 6fa497718b9..6691cd97d65 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -221,6 +221,7 @@ public function testStrExtract() { // Basic extraction $this->assertSame(['last_4' => '5000'], Str::extract('4242-4242-4242-5000', '*-*-*-{last_4}')); + $this->assertSame(['last_4' => '{5000}'], Str::extract('4242-4242-4242-{5000}', '*-*-*-{last_4}')); $this->assertSame(['area_code' => '800'], Str::extract('Phone Number: 1-(800)-555-5555', '*1-({area_code})-*')); $this->assertSame(['email' => 'john.doe@example.com'], Str::extract('Contact us at john.doe@example.com.', '* at {email}.')); $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2', '/users/{user_id}/posts/{post_id}')); @@ -229,16 +230,16 @@ public function testStrExtract() // Extraction with wildcard $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2', '/users/{user_id}/posts/{post_id}')); - $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2', '/users/{user_id}*/posts/{post_id}')); - $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2/comments', '/users/{user_id}/posts/{post_id}*')); - $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('https://foo.com/users/1/posts/2/comments', '*users/{user_id}/posts/{post_id}*')); + $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2', '/users/{user_id}*posts/{post_id}')); + $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2/comments', '/users/{user_id}/posts/{post_id}/*')); + $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('https://foo.com/users/1/posts/2/comments', '*users/{user_id}/posts/{post_id}/*')); // Extraction with different characters $this->assertSame(['user_id' => '1?', 'post_id' => '2-'], Str::extract('/users/1?/posts/2-', '/users/{user_id}/posts/{post_id}')); $this->assertSame(['user_id' => '1_a', 'post_id' => '2.b'], Str::extract('/users/1_a/posts/2.b', '/users/{user_id}/posts/{post_id}')); // Extraction with multiple wildcards - $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2/comments/3', '/users/{user_id}/posts/{post_id}*')); + $this->assertSame(['user_id' => '1', 'post_id' => '2'], Str::extract('/users/1/posts/2/comments/3', '/users/{user_id}/posts/{post_id}/*')); $this->assertSame(['user_id' => '1', 'post_id' => '2', 'comment_id' => '3'], Str::extract('/users/1/posts/2/comments/3/replies/4', '/users/{user_id}/posts/{post_id}*/comments/{comment_id}*')); $this->assertSame(['user_id' => '1', 'comment_id' => '3'], Str::extract('/users/1/posts/2/comments/3/replies/4', '*users/{user_id}/*/comments/{comment_id}*')); @@ -250,6 +251,14 @@ public function testStrExtract() $this->assertSame(['uuid' => 'a1b2c3d4-e5f6-7890-1234-567890abcdef'], Str::extract('/users/a1b2c3d4-e5f6-7890-1234-567890abcdef/profile', '/users/{uuid}/profile')); // Edge cases + // $this->assertSame(['foo' => '{bar}'], Str::extract('{\\{bar}\\}', '*{foo}\\}')); + + $this->assertSame(['foo' => '{bar}'], Str::extract('{bar}', '{foo}')); + $this->assertSame(['foo' => '{bar}'], Str::extract('foo bar {{bar}}', '* {{foo}}')); + $this->assertSame(['foo' => '{{bar}}'], Str::extract('foo bar {{{bar}}}', '* {{foo}}')); + $this->assertSame(['foo' => 'bar'], Str::extract('{(|\*&!@$/\bar/|}', '*\{foo}/*')); + $this->assertSame(['foo' => 'bar'], Str::extract('{(|\*&!@$/\bar/|}', '{(|\*&!@$/\{foo}/|}')); + $this->assertSame([], Str::extract('/users/1/posts/2', '')); // Empty pattern $this->assertSame([], Str::extract('', '/users/{user_id}/posts/{post_id}')); // Empty haystack $this->assertSame([], Str::extract('/users/1/posts', '/users/{user_id}/posts/{post_id}')); // Missing segment in haystack