Skip to content

Commit

Permalink
[i] Regex::Object::Matches->count implemented + tests
Browse files Browse the repository at this point in the history
Took 10 minutes

Took 2 minutes
  • Loading branch information
AlexP007 committed Jan 13, 2022
1 parent e7c425d commit d9261c8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.22 2022-01-13 10:39:07 Europe/Moscow
- Regex::Object::Matches->count implemented
- Minor format changes in CHANGES

1.21 2022-01-13 08:49:06 Europe/Moscow
- Regex::Object::Matches->captures_all typo in method body with collection spelled with one 'l'

Expand Down
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = Perl_5
copyright_holder = Alexander Panteleev
copyright_year = 2022

version = 1.21
version = 1.22

[AutoPrereqs]

Expand Down
10 changes: 8 additions & 2 deletions lib/Regex/Object.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Regex::Object::Matches;
use Moo;
use namespace::clean;

our $VERSION = '1.21';
our $VERSION = '1.22';

tie my %nc, "Tie::Hash::NamedCapture";
tie my %nca, "Tie::Hash::NamedCapture", all => 1;
Expand Down Expand Up @@ -84,7 +84,7 @@ Regex::Object - solves problems with global Regex variables side effects.
=head1 VERSION
version 1.21
version 1.22
=head1 SYNOPSIS
Expand Down Expand Up @@ -256,6 +256,12 @@ Returns array ref with all Regex::Object::Match objects.
my $first_match = $matches->collection->[0];
=head3 count()
Returns length of the collection.
my $count = $matches->count;
=head3 match_all()
Return array ref with all matches, i.e $MATCH[].
Expand Down
11 changes: 9 additions & 2 deletions lib/Regex/Object/Matches.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ has collection => (
required => 1,
);

sub match_all {
has count => (
is => 'rwp',
);

sub BUILD {
my $self = shift;
$self->_set_count(scalar @{$self->collection});
}

sub match_all {
my $self = shift;
return [map { $_->match } @{$self->collection}];
}

sub captures_all {
my $self = shift;

return [map { $_->captures } @{$self->collection}];
}

Expand Down
39 changes: 37 additions & 2 deletions t/02_matches.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings qw/FATAL/;
use utf8;

use Test::Simple tests => 9;
use Test::Simple tests => 11;
use Regex::Object;

$|=1;
Expand Down Expand Up @@ -142,4 +142,39 @@ ok(!$result,
sprintf('Returns wrong value: %s, expected: undef',
$result,
)
);
);


## TEST 10
# Test global matching with scoped regex: count unsuccessfully

$re = Regex::Object->new(
regex => qr/(\d+?) (\d+)/,
);

$expected = 0;
$result = $re->match_all('John Doe Eric Lide')->count;

ok($result == $expected,
sprintf('Returns wrong value: %s, expected: %s',
$result,
$expected,
)
);

## TEST 11
# Test global matching with scoped regex: count successfully

$re = Regex::Object->new(
regex => qr/(\w+?) (\w+)/,
);

$expected = 2;
$result = $re->match_all('John Doe Eric Lide')->count;

ok($result == $expected,
sprintf('Returns wrong value: %s, expected: %s',
$result,
$expected,
)
);

0 comments on commit d9261c8

Please sign in to comment.