/ack-1.96/t/context.t

# · Perl · 227 lines · 191 code · 26 blank · 10 comment · 4 complexity · e48f282d8399081c7845a94f8eef54ea MD5 · raw file

  1. #!perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 22;
  5. use File::Next 0.34; # for reslash function
  6. use lib 't';
  7. use Util;
  8. prep_environment();
  9. # checks also beginning of file
  10. BEFORE: {
  11. my @expected = split( /\n/, <<'EOF' );
  12. Well, my daddy left home when I was three
  13. --
  14. But the meanest thing that he ever did
  15. Was before he left, he went and named me Sue.
  16. EOF
  17. my $regex = 'left';
  18. my @files = qw( t/text/boy-named-sue.txt );
  19. my @args = ( '--text', '-B1', $regex );
  20. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex - before" );
  21. }
  22. BEFORE_WITH_LINE_NO: {
  23. my $target_file = File::Next::reslash( 't/text/boy-named-sue.txt' );
  24. my @expected = split( /\n/, <<"EOF" );
  25. $target_file-7-
  26. $target_file-8-Well, he must have thought that it was quite a joke
  27. $target_file:9:And it got a lot of laughs from a' lots of folks,
  28. $target_file-10-It seems I had to fight my whole life through.
  29. $target_file-11-Some gal would giggle and I'd turn red
  30. $target_file:12:And some guy'd laugh and I'd bust his head,
  31. --
  32. $target_file-44-But I really can't remember when,
  33. $target_file-45-He kicked like a mule and he bit like a crocodile.
  34. $target_file:46:I heard him laugh and then I heard him cuss,
  35. EOF
  36. my $regex = 'laugh';
  37. my @files = qw( t/text );
  38. my @args = ( '--text', '-B2', $regex );
  39. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex - before with line numbers" );
  40. }
  41. # checks also end of file
  42. AFTER: {
  43. my @expected = split( /\n/, <<"EOF" );
  44. I tell ya, life ain't easy for a boy named Sue.
  45. Well, I grew up quick and I grew up mean,
  46. --
  47. -- "A Boy Named Sue", Johnny Cash
  48. EOF
  49. my $regex = '[nN]amed Sue';
  50. my @files = qw( t/text/boy-named-sue.txt );
  51. my @args = ( '--text', '-A2', $regex );
  52. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex - after" );
  53. }
  54. # context defaults to 2
  55. CONTEXT_DEFAULT: {
  56. my @expected = split( /\n/, <<"EOF" );
  57. And it got a lot of laughs from a' lots of folks,
  58. It seems I had to fight my whole life through.
  59. Some gal would giggle and I'd turn red
  60. And some guy'd laugh and I'd bust his head,
  61. I tell ya, life ain't easy for a boy named Sue.
  62. EOF
  63. my $regex = 'giggle';
  64. my @files = qw( t/text/boy-named-sue.txt );
  65. my @args = ( '--text', '-C', $regex );
  66. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex - context defaults to 2" );
  67. }
  68. # -1 must not stop the ending context from displaying
  69. CONTEXT_DEFAULT: {
  70. my @expected = split( /\n/, <<"EOF" );
  71. And it got a lot of laughs from a' lots of folks,
  72. It seems I had to fight my whole life through.
  73. Some gal would giggle and I'd turn red
  74. And some guy'd laugh and I'd bust his head,
  75. I tell ya, life ain't easy for a boy named Sue.
  76. EOF
  77. my $regex = 'giggle';
  78. my @files = qw( t/text/boy-named-sue.txt );
  79. my @args = ( '--text', '-1', '-C', $regex );
  80. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex with -1" );
  81. }
  82. # -m3 should work properly and show only 3 matches with correct context
  83. # even though there is a 4th match in the after context of the third match
  84. # ("give _ya_ that name" in the last line)
  85. CONTEXT_MAX_COUNT: {
  86. my @expected = split( /\n/, <<"EOF" );
  87. And some guy'd laugh and I'd bust his head,
  88. I tell ya, life ain't easy for a boy named Sue.
  89. --
  90. I tell ya, I've fought tougher men
  91. But I really can't remember when,
  92. --
  93. And if a man's gonna make it, he's gotta be tough
  94. And I knew I wouldn't be there to help ya along.
  95. So I give ya that name and I said goodbye
  96. EOF
  97. my $regex = 'ya';
  98. my @files = qw( t/text/boy-named-sue.txt );
  99. my @args = ( '--text', '-m3', '-C1', $regex );
  100. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex with -m3" );
  101. }
  102. # highlighting works with context
  103. HIGHLIGHTING: {
  104. my @ack_args = qw( July -C5 --text --color );
  105. my @results = pipe_into_ack( 't/text/4th-of-july.txt', @ack_args );
  106. my @escaped_lines = grep { /\e/ } @results;
  107. is( scalar @escaped_lines, 2, 'Only two lines are highlighted' );
  108. is( scalar @results, 18, 'Expecting altogether 18 lines back' );
  109. }
  110. # grouping works with context (single file)
  111. GROUPING_SINGLE_FILE: {
  112. my $target_file = File::Next::reslash( 't/etc/shebang.py.xxx' );
  113. my @expected = split( /\n/, <<"EOF" );
  114. $target_file
  115. 1:#!/usr/bin/python
  116. EOF
  117. my $regex = 'python';
  118. my @args = ( '--python', '--group', '-C', $regex );
  119. ack_lists_match( [ @args ], \@expected, "Looking for $regex in Python files with grouping" );
  120. }
  121. # grouping works with context and multiple files
  122. # i.e. a separator line between different matches in the same file and no separator between files
  123. GROUPING_MULTIPLE_FILES: {
  124. my @target_file = (
  125. File::Next::reslash( 't/text/boy-named-sue.txt' ),
  126. File::Next::reslash( 't/text/me-and-bobbie-mcgee.txt' ),
  127. File::Next::reslash( 't/text/science-of-myth.txt' ),
  128. );
  129. my @expected = split( /\n/, <<"EOF" );
  130. $target_file[0]
  131. 1:Well, my daddy left home when I was three
  132. --
  133. 5-But the meanest thing that he ever did
  134. 6:Was before he left, he went and named me Sue.
  135. $target_file[1]
  136. 10-
  137. 11: Freedom's just another word for nothing left to lose
  138. --
  139. 25-
  140. 26: Freedom's just another word for nothing left to lose
  141. $target_file[2]
  142. 18-Consider the case of the woman whose faith helped her make it through
  143. 19:When she was raped and cut up, left for dead in her trunk, her beliefs held true
  144. 20-It doesn't matter if it's real or not
  145. 21:'cause some things are better left without a doubt
  146. EOF
  147. my $regex = 'left';
  148. my @files = qw( t/text/ );
  149. my @args = ( '--text', '--group', '-B1', '--sort-files', $regex );
  150. ack_lists_match( [ @args, @files ], \@expected, "Looking for $regex in multiple files with grouping" );
  151. }
  152. # context does nothing ack -g
  153. ACK_G: {
  154. my @expected = qw(
  155. t/swamp/html.htm
  156. t/swamp/html.html
  157. );
  158. my $regex = 'swam.......htm';
  159. my @files = qw( t/ );
  160. my @args = ( '-C2', '-g', $regex );
  161. ack_sets_match( [ @files, @args ], \@expected, "Looking for $regex - no change with -g" );
  162. }
  163. # ack -o disables context
  164. WITH_O: {
  165. my @files = qw( t/text/boy-named-sue.txt );
  166. my @args = qw( the\\s+\\S+ --text -o -C2 );
  167. my @expected = split( /\n/, <<'EOF' );
  168. the meanest
  169. the moon
  170. the honky-tonks
  171. the dirty,
  172. the eyes
  173. the wall
  174. the street
  175. the mud
  176. the blood
  177. the beer.
  178. the name
  179. the right
  180. the gravel
  181. the spit
  182. the son-of-a-bitch
  183. EOF
  184. s/^\s+// for @expected;
  185. ack_lists_match( [ @args, @files ], \@expected, 'Context is disabled with -o' );
  186. }