1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
#!perl
use warnings;
use strict;
use Test::More tests => 46;
use lib 't';
use Util;
prep_environment();
DOLLAR_1: {
my @files = qw( t/text/ );
my @args = qw/ --output=x$1x free(\\S+) --sort-files /;
my @target_file = map { reslash($_) } qw(
t/text/bill-of-rights.txt
t/text/gettysburg.txt
);
my @expected = (
"$target_file[0]:4:xdomx",
"$target_file[1]:23:xdomx",
);
ack_sets_match( [ @args, @files ], \@expected, 'Find all the things with --output function' );
}
DOLLAR_UNDERSCORE: {
my @expected = line_split( <<'HERE' );
shall have a new birth of freedom -- and that government of the people,xshall have a new birth of freedom -- and that government of the people,
HERE
my @files = qw( t/text/gettysburg.txt );
my @args = qw( free --output=$_x$_ );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Matching line' );
}
ARG_MULTIPLE_FILES: {
# Note the first line is there twice because it matches twice.
my @expected = line_split( <<'HERE' );
or prohibiting the free exercise thereof; or abridging the freedom of
or prohibiting the free exercise thereof; or abridging the freedom of
A well regulated Militia, being necessary to the security of a free State,
Number of free Persons, including those bound to Service for a Term
shall have a new birth of freedom -- and that government of the people,
HERE
my @files = qw( t/text );
my @args = qw( free --sort-files -h --output=$_ );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Matching line' );
}
# Find a match in multiple files, and output it in double quotes.
DOUBLE_QUOTES: {
my @files = qw( t/text/ );
my @args = ( '--output="$1"', '(free\\w*)', '--sort-files' );
my @target_file = map { reslash($_) } qw(
t/text/bill-of-rights.txt
t/text/constitution.txt
t/text/gettysburg.txt
);
my @expected = (
qq{$target_file[0]:4:"free"},
qq{$target_file[0]:4:"freedom"},
qq{$target_file[0]:10:"free"},
qq{$target_file[1]:32:"free"},
qq{$target_file[2]:23:"freedom"},
);
ack_sets_match( [ @args, @files ], \@expected, 'Find all the things with --output function' );
}
MATCH: {
my @expected = (
'free'
);
my @files = qw( t/text/gettysburg.txt );
my @args = qw( free --output=$& );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Part of a line matching pattern' );
}
MATCH_MULTIPLE_FILES: {
my @expected = line_split( <<'HERE' );
t/text/bill-of-rights.txt:4:free
t/text/bill-of-rights.txt:4:free
t/text/bill-of-rights.txt:10:free
t/text/constitution.txt:32:free
t/text/gettysburg.txt:23:free
HERE
my @files = qw ( t/text );
my @args = qw( free --sort-files --output=$& );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Part of a line matching pattern' );
}
PREMATCH: {
# No HEREDOC here since we do not want our editor/IDE messing with trailing whitespace.
my @expected = (
'shall have a new birth of '
);
my @files = qw( t/text/gettysburg.txt );
my @args = qw( freedom --output=$` );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Part of a line preceding match' );
}
PREMATCH_MULTIPLE_FILES: {
# No HEREDOC here since we do not want our editor/IDE messing with trailing whitespace.
my @expected = (
'or prohibiting the free exercise thereof; or abridging the ',
'shall have a new birth of '
);
my @files = qw( t/text/);
my @args = qw( freedom -h --sort-files --output=$` );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Part of a line preceding match' );
}
POSTMATCH: {
my @expected = split( /\n/, <<'HERE' );
-- and that government of the people,
HERE
my @files = qw( t/text/gettysburg.txt );
my @args = qw( freedom --output=$' );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Part of a line that follows match' );
}
POSTMATCH_MULTIPLE_FILES: {
my @expected = line_split( <<'HERE' );
of
-- and that government of the people,
HERE
my @files = qw( t/text/ );
my @args = qw( freedom -h --sort-files --output=$' );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Part of a line that follows match' );
}
SUBPATTERN_MATCH: {
my @expected = (
'love-God-Montresor'
);
my @files = qw( t/text/amontillado.txt );
my @args = qw( (love).+(God).+(Montresor) --output=$1-$2-$3 );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Capturing parentheses match' );
}
SUBPATTERN_MATCH_MULTIPLE_FILES: {
my @expected = line_split( <<'HERE' );
the-free-exercise
a-free-State
of-free-Persons
HERE
my @files = qw( t/text/ );
my @args = qw( (\w+)\s(free)\s(\w+) -h --sort-files --output=$1-$2-$3 );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Capturing parentheses match' );
}
INPUT_LINE_NUMBER: {
my @expected = (
'line:15'
);
my @files = qw( t/text/bill-of-rights.txt );
my @args = qw( quartered --output=line:$. );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Line number' );
}
INPUT_LINE_NUMBER_MULTIPLE_FILES: {
my @expected = line_split( <<'HERE' );
t/text/bill-of-rights.txt:4:line:4
t/text/bill-of-rights.txt:4:line:4
t/text/bill-of-rights.txt:10:line:10
t/text/constitution.txt:32:line:32
t/text/gettysburg.txt:23:line:23
HERE
my @files = qw( t/text/ );
my @args = qw( free --sort-files --output=line:$. );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Line number' );
}
LAST_PAREN_MATCH: {
my @expected = line_split( <<'HERE' );
t/text/amontillado.txt:124:love
t/text/amontillado.txt:309:love
t/text/amontillado.txt:311:love
t/text/constitution.txt:267:hate
HERE
my @files = qw( t/text/ );
my @args = qw( (love)|(hate) --sort-files --output=$+ );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Last paren match' );
}
COMBOS_1: {
my @expected = line_split( <<'HERE' );
t/text/amontillado.txt:124:love-124-d; you are happy,
t/text/amontillado.txt:309:love-309- of God, Montresor!"
t/text/amontillado.txt:311:love-311- of God!"
t/text/constitution.txt:267:hate-267-ver, from any King, Prince, or foreign State.
HERE
my @files = qw( t/text/ );
my @args = qw( (love)|(hate) --sort-files --output=$+-$.-$' );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Combos 1' );
}
COMBOS_2: {
my @expected = line_split( <<'HERE' );
t/text/amontillado.txt:124:happy-happy-happy
t/text/raven.txt:73:happy-happy-happy
HERE
my @files = qw( t/text/ );
my @args = qw( (happy) --sort-files -i --output=$1-$&-$1 );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Combos 2' );
}
COMBOS_3: {
my @expected = line_split( <<'HERE' );
t/text/amontillado.txt:124:precious. You are rich, respected, admired, beloved; you are ---,--happy
t/text/raven.txt:73:Caught from some un--- master whom unmerciful Disaster--happy
HERE
my @files = qw( t/text/ );
my @args = qw( (happy) --sort-files -i --output=$`---$'--$+ );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Combos 2' );
}
NUMERIC_SUBSTITUTIONS: {
# Make sure that substitutions don't affect future substitutions.
my @expected = line_split( <<'HERE' );
t/text/constitution.txt:269:Section 10 on line 269
HERE
my @files = qw( t/text/bill-of-rights.txt t/text/constitution.txt );
my @args = ( '(\d\d)', '--output=Section $1 on line $.' );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Numeric substitutions' );
}
CHARACTER_SUBSTITUTIONS: {
# Make sure that substitutions don't affect future substitutions.
my @expected = line_split( <<"HERE" );
t/text/bill-of-rights.txt:15:No Soldier shall, in time of peace be
in any house, without\tin any house, without
HERE
my @files = qw( t/text/ );
my @args = ( '\s+quartered\s+(.+)', '--output=$`\n$1\t$1' );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Character substitutions' );
}
# $f is the filenname, needed for grep, emulating ack2 $filename:$lineno:$_
FILENAME_SUBSTITUTION_1 : {
my @expected = line_split( <<'HERE' );
t/text/ozymandias.txt:4:Half sunk, a shattered visage lies, whose frown,
HERE
my @files = qw( t/text/ozymandias.txt );
my @args = qw( visage --output=$f:$.:$_ );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Filename with matching line' );
}
FILENAME_SUBSTITUTION_2 : {
my @expected = line_split( <<'HERE' );
t/text/ozymandias.txt:4:visage
HERE
my @files = qw( t/text/ozymandias.txt );
my @args = qw( visage --output=$f:$.:$& );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Filename with match' );
}
FILENAME_SUBSTITUTION_3 : {
my @expected = line_split( <<'HERE' );
t/text/ozymandias.txt:4:visage
HERE
my @files = qw( t/text/ozymandias.txt );
my @args = qw( (visage) --output=$f:$.:$+ );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Filename with last match' );
}
NO_SPECIALS_IN_OUTPUT_EXPRESSION : {
my @expected = line_split( <<'HERE' );
literal
literal
HERE
my @files = qw( t/text/ozymandias.txt );
my @args = qw( sand --output=literal );
my @results = run_ack( @args, @files );
lists_match( \@results, \@expected, 'Filename with last match' );
}
done_testing();
exit 0;
|