File: grep.t

package info (click to toggle)
libfile-grep-perl 0.02-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 88 kB
  • sloc: perl: 188; makefile: 2
file content (66 lines) | stat: -rwxr-xr-x 1,322 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w

use File::Grep qw( fgrep fmap fdo );
use Test::More tests=>11;

my @files = qw( t/test.txt t/test2.txt );

# Void context:
if ( fgrep { /Bob/ } @files ) {
	pass "Void context";
} else {
	fail "Void context";
}

if ( fgrep { /Steve/ } @files ) {
	fail "Void context";
} else {
	pass "Void context";
}

my $count = fgrep { /Bob/ } @files;

is( $count, 5, "Scalar context" );

my @matches = fgrep { /Bob/ } @files;

is( $matches[0]->{ count }, 5, "Hash context" );
is( $matches[1]->{ count }, 0, "Hash context" );

@matches = fgrep { /\WBob\W/ } @files;

is( $matches[0]->{ count }, 4, "Hash context" );

my @lced = fmap { chomp; lc; } @files;
is( "--$lced[4]--", "--by this test.  if there are--", "Mapping" );


open FILE1, "<t/test.txt" or die $!;
my $f1 = \*FILE1;
open FILE2, "<t/test2.txt" or die $!;
my $f2 = \*FILE2;

my $count2 = fgrep { /Bob/ } $f1, $f2;
is ( $count2, 5, "Filehandle context" );

close $f1;
close $f2;

open FILE2, "<t/test2.txt" or die $!;
my $f3 = \*FILE2;
my $count3 = fgrep { /Bob/ } $f3;
is ( $count3, 0, "Filehandle context" );
close $f3;

my $count4 = fgrep { /Bob/ } [ qw( wrong argument here ) ];
is ( $count4, 0, "Illegal context" );

my $words = 0;
fdo { my @w = split /\s+/, $_;
      $words += scalar @w } @files;
is ( $words, 68, "fdo" );



close $f1;
close $f2;