Skip to content

Commit

Permalink
Bringing over tests from the branch
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Nov 30, 2024
1 parent 1aa412c commit a28f428
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 5 deletions.
38 changes: 35 additions & 3 deletions ack
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,22 @@ matches for lines within the range.
Specifies an ackrc file to load after all others; see L</"ACKRC LOCATION SEMANTICS">.
=item B<--and=PATTERN>
Specifies a I<PATTERN> that MUST ALSO be found on a given line for a match to
occur. This option can be repeated.
If you want to find all the lines with both "dogs" or "cats", use:
ack dogs --and cats
Note that the options that affect "dogs" also affect "cats", so if you have
ack -i -w dogs --and cats
then the search for both "dogs" and "cats" will be case-insensitive and be
word-limited.
=item B<-A I<NUM>>, B<--after-context=I<NUM>>
Print I<NUM> lines of trailing context after matching lines.
Expand Down Expand Up @@ -1547,14 +1563,30 @@ if you have
ack -i -w dogs --not cats
the the search for both "dogs" and "cats" will be case-insensitive and be
then the search for both "dogs" and "cats" will be case-insensitive and be
word-limited.
=item B<-o>
Show only the part of each line matching PATTERN (turns off text
highlighting). This is exactly the same as C<--output=$&>.
=item B<--or=PATTERN>
Specifies a I<PATTERN> that MAY be found on a given line for a match to
occur. This option can be repeated.
If you want to find all the lines with "dogs" or "cats", use:
ack dogs --or cats
Note that the options that affect "dogs" also affect "cats", so if you have
ack -i -w dogs --or cats
then the search for both "dogs" and "cats" will be case-insensitive and be
word-limited.
=item B<--output=I<expr>>
Output the evaluation of I<expr> for each line (turns off text
Expand Down Expand Up @@ -1585,7 +1617,7 @@ The number of the line in the file.
=item C<$&>, C<$`> and C<$'>
C<$&> is the the string matched by the pattern, C<$`> is what
C<$&> is the string matched by the pattern, C<$`> is what
precedes the match, and C<$'> is what follows it. If the pattern
is C<gra(ph|nd)> and the string is "lexicographic", then C<$&> is
"graph", C<$`> is "lexico" and C<$'> is "ic".
Expand Down Expand Up @@ -1844,7 +1876,7 @@ an F<.ackrc> file - then you do not have to define your types over and
over again. In the following examples the options will always be shown
on one command line so that they can be easily copy & pasted.
File types can be specified both with the the I<--type=xxx> option,
File types can be specified both with the I<--type=xxx> option,
or the file type as an option itself. For example, if you create
a filetype of "cobol", you can specify I<--type=cobol> or simply
I<--cobol>. File types must be at least two characters long. This
Expand Down
6 changes: 5 additions & 1 deletion lib/App/Ack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ Searching:
--range-start PATTERN Specify PATTERN as the start of a match range.
--range-end PATTERN Specify PATTERN as the end of a match range.
--match PATTERN Specify PATTERN explicitly. Typically omitted.
--not PATTERN Specifies PATTERN that must not be found on
--and PATTERN Specifies PATTERN that MUST also be found on
the line for a match to occur. Repeatable.
--or PATTERN Specifies PATTERN that MAY also be found on
the line for a match to occur. Repeatable.
--not PATTERN Specifies PATTERN that must NOT be found on
the line for a match to occur. Repeatable.
Search output:
Expand Down
2 changes: 2 additions & 0 deletions t/ack-c.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ DASH_C: {
t/text/bill-of-rights.txt:0
t/text/constitution.txt:0
t/text/gettysburg.txt:1
t/text/movies.txt:0
t/text/number.txt:0
t/text/numbered-text.txt:0
t/text/ozymandias.txt:0
Expand All @@ -38,6 +39,7 @@ WITH_DASH_V: {
t/text/bill-of-rights.txt:45
t/text/constitution.txt:259
t/text/gettysburg.txt:15
t/text/movies.txt:25
t/text/number.txt:1
t/text/numbered-text.txt:20
t/text/ozymandias.txt:9
Expand Down
4 changes: 4 additions & 0 deletions t/ack-g.t
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ subtest '-v works on -g' => sub {
my @expected = qw(
t/text/bill-of-rights.txt
t/text/gettysburg.txt
t/text/movies.txt
);
my $file_regex = 'n';

Expand Down Expand Up @@ -291,6 +292,7 @@ subtest 'test -g on a path' => sub {
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/gettysburg.txt
t/text/movies.txt
t/text/number.txt
t/text/numbered-text.txt
t/text/ozymandias.txt
Expand All @@ -311,6 +313,7 @@ t/(text)/amontillado.txt
t/(text)/bill-of-rights.txt
t/(text)/constitution.txt
t/(text)/gettysburg.txt
t/(text)/movies.txt
t/(text)/number.txt
t/(text)/numbered-(text).txt
t/(text)/ozymandias.txt
Expand Down Expand Up @@ -343,6 +346,7 @@ t/text/amontillado.txt
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/gettysburg.txt
t/text/movies.txt
t/text/number.txt
t/text/numbered-text.txt
t/text/ozymandias.txt
Expand Down
3 changes: 3 additions & 0 deletions t/ack-l.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ my @matching = qw(
my @nonmatching = qw(
t/text/amontillado.txt
t/text/gettysburg.txt
t/text/movies.txt
t/text/number.txt
t/text/numbered-text.txt
t/text/ozymandias.txt
Expand Down Expand Up @@ -59,6 +60,7 @@ DASH_CAPITAL_L: {
my @expected = qw(
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/movies.txt
t/text/number.txt
t/text/numbered-text.txt
t/text/ozymandias.txt
Expand All @@ -83,6 +85,7 @@ DASH_LV: {
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/gettysburg.txt
t/text/movies.txt
t/text/number.txt
t/text/numbered-text.txt
t/text/ozymandias.txt
Expand Down
2 changes: 2 additions & 0 deletions t/ack-print0.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ G_NO_PRINT0: {
t/text/amontillado.txt
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/movies.txt
t/text/ozymandias.txt
);

Expand All @@ -32,6 +33,7 @@ G_PRINT0: {
t/text/amontillado.txt
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/movies.txt
t/text/ozymandias.txt
) ) . "\0"; # string of filenames separated and concluded with null byte

Expand Down
181 changes: 180 additions & 1 deletion t/boolean.t
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,185 @@ HERE
}


done_testing();
NOT: {
_movies_are(
[qw( Murray )],
[
'Caddyshack',
'Ghostbusters',
'Groundhog Day',
'Little Shop of Horrors',
'Stripes',
]
);

_movies_are(
[qw( Murray --not Ramis )],
[
'Caddyshack',
'Groundhog Day',
'Little Shop of Horrors',
]
);
}


AND: {
_movies_are(
[qw( Aykroyd )],
[
'1941',
'The Blues Brothers',
'Dragnet',
'Ghostbusters',
'Neighbors',
]
);

_movies_are(
[qw( Aykroyd --and Belushi )],
[
'1941',
'The Blues Brothers',
'Neighbors',
]
);

_movies_are(
[qw( Belushi --and Aykroyd )],
[
'1941',
'The Blues Brothers',
'Neighbors',
]
);

# Permute through all combinations.
my @who = qw( Aykroyd Belushi Candy );
for my $permutation ( permutate( @who ) ) {
my @args = _argjoin( '--and', $permutation );
_movies_are(
[ @args ],
[
'The Blues Brothers',
]
);
}
}


OR: {
_movies_are(
[qw( Fisher )],
[
'The Blues Brothers',
'When Harry Met Sally',
]
);

_movies_are(
[qw( Crystal )],
[
'This is Spinal Tap',
'When Harry Met Sally',
]
);

_movies_are(
[qw( Crystal --or Fisher )],
[
'The Blues Brothers',
'This is Spinal Tap',
'When Harry Met Sally',
]
);

_movies_are(
[qw( Fisher --or Crystal )],
[
'The Blues Brothers',
'This is Spinal Tap',
'When Harry Met Sally',
]
);

# One of the OR options doesn't exist.
_movies_are(
[qw( Crystal --or Hemsworth )],
[
'This is Spinal Tap',
'When Harry Met Sally',
]
);
_movies_are(
[qw( Hemsworth --or Crystal )],
[
'This is Spinal Tap',
'When Harry Met Sally',
]
);

_movies_are(
[qw( Crystal --or Fisher --or Hemsworth )],
[
'The Blues Brothers',
'This is Spinal Tap',
'When Harry Met Sally',
]
);

# Permute through all combinations.
my @who = qw( Crystal Fisher Hemsworth Moranis );
for my $permutation ( permutate( @who ) ) {
my @args = _argjoin( '--or', $permutation );
_movies_are(
[ @args ],
[
'The Blues Brothers',
'Ghostbusters',
'Little Shop of Horrors',
'My Blue Heaven',
'Spaceballs',
'This is Spinal Tap',
'When Harry Met Sally',
]
);
}
}



# XXX We need tests on highlighting, too.
#
# XXX And tests on none of them matching.
# XXX And tests that --and themselves out of matching.
done_testing();
exit 0;


sub _movies_are {
local $Test::Builder::Level = $Test::Builder::Level + 1;

my $args = shift;
my $exp = shift;

my @base_args = qw( -w -i --noenv );
my @files = reslash( 't/text/movies.txt' );
my @got = run_ack( @base_args, @{$args}, @files );

s/:.+// for @got;

return is_deeply( \@got, $exp, join( ' ', @{$args} ) ) || die;
}


sub _argjoin {
my $opt = shift;
my $args = shift;

my @args = map { ($_, $opt) } @{$args};

pop @args;

return @args;
}
2 changes: 2 additions & 0 deletions t/config-loader.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use App::Ack::ConfigLoader;
delete @ENV{qw( PAGER ACK_PAGER ACK_PAGER_COLOR )};

my %defaults = (
and => [],
'break' => undef,
c => undef,
color => undef,
Expand All @@ -31,6 +32,7 @@ my %defaults = (
m => undef,
n => undef,
not => [],
or => [],
output => undef,
p => undef,
pager => undef,
Expand Down
Loading

0 comments on commit a28f428

Please sign in to comment.