File Coverage

test/lib/RandomLine.pm
Criterion Covered Total %
statement 146 147 99.3
branch 2 2 100.0
condition 2 6 33.3
subroutine 45 45 100.0
pod 0 12 0.0
total 195 212 91.9


line stmt bran cond sub pod time code
1             package RandomLine;
2 1     1   9299 use base qw/Test::Class/;
  1         2  
  1         664  
3              
4 1     1   20524 use strict;
  1         2  
  1         20  
5 1     1   5 use warnings;
  1         2  
  1         22  
6              
7 1     1   5 use Test::More;
  1         2  
  1         11  
8 1     1   840 use Test::Exception;
  1         3055  
  1         3  
9 1     1   1066 use File::Temp qw/tempfile/;
  1         21859  
  1         70  
10 1     1   502 use Set::Scalar;
  1         11015  
  1         47  
11 1     1   510 use Test::Warn;
  1         2410  
  1         75  
12 1     1   493 use Test::ManyParams;
  1         4653  
  1         6  
13              
14 1     1   156 use File::Random qw/random_line/;
  1         2  
  1         64  
15              
16 1     1   6 use constant LINES => <<'EOF';
  1         1  
  1         65  
17             Random
18             Lines
19             can
20             contain
21             randomly
22             appearing things like
23             PI = 3.1415926535
24             but of course that's not neccessary
25             EOF
26              
27 1     1   6 use constant WRONG_PARAMS => (undef, '', '&');
  1         2  
  1         52  
28              
29 1     1   6 use constant SAMPLE_SIZE => 200;
  1         1  
  1         110  
30              
31             sub fill_tempfile : Test(setup) {
32 10     10 0 7486 my $self = shift;
33 10         39 (my $normal_fh, $self->{normal_file}) = tempfile();
34 10         3494 (undef, $self->{empty_file}) = tempfile();
35 10         3500 print $normal_fh LINES;
36 1     1   6 }
  1         2  
  1         9  
37              
38             sub clear_tempfile : Test(teardown) {
39 10     10 0 10674 my $self = shift;
40 10         57 close $self->{'normal_file'};
41 10         46 close $self->{'empty_file'};
42 1     1   380 }
  1         2  
  1         3  
43              
44             sub empty_file_returns_undef : Test(1) {
45 1     1 0 812 my $self = shift;
46 1         13 is random_line($self->{empty_file}), undef, "random_file( Empty file )";
47 1     1   286 }
  1         2  
  1         4  
48              
49             sub lines_are_the_expected_ones : Test(1) {
50 1     1 0 661 my $self = shift;
51 1         7 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         35  
52 1         325 my $got = Set::Scalar->new();
53 1         62 $got->insert( scalar random_line($self->{normal_file}) ) for (1 .. SAMPLE_SIZE);
54 1         58 is $got, $exp, "random_line( Normal File )";
55 1     1   418 }
  1         1  
  1         8  
56              
57             sub multiple_lines_are_the_expected_ones : Test(2) {
58 1     1 0 667 my $self = shift;
59 1         6 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         18  
60 1         108 my $got = Set::Scalar->new();
61 1         58 $got->insert( random_line($self->{normal_file},3) ) for (1 .. SAMPLE_SIZE()/3);
62 1         68 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
63 1         370 $got->clear;
64 1         73 for (1 .. SAMPLE_SIZE()/3) {
65 66         4238 my ($line1, $line2, $line3) = random_line($self->{normal_file});
66 66         225 $got->insert($line1, $line2, $line3);
67             }
68 1         68 is $got, $exp, '($line1, $line2, $line3) = random_line $fname [expected files]';
69 1     1   466 }
  1         2  
  1         4  
70              
71             sub multiple_lines_are_the_expected_ones_random_line_with_sample_size : Test(1) {
72 1     1 0 692 my $self = shift;
73 1         8 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         19  
74 1         111 my $got = Set::Scalar->new(random_line($self->{normal_file}, SAMPLE_SIZE()));
75 1         852 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
76 1     1   373 }
  1         2  
  1         5  
77              
78             sub get_really_lines : Test(8) {
79 1     1 0 649 my $self = shift;
80             SIZE_IS_KNOWN: {
81 1         3 my ($line1, $line2, $line3) = random_line($self->{normal_file},3);
  1         6  
82 1         45 ok defined($line1), "1st returned line of random_line should be defined";
83 1         348 ok defined($line2), "2nd returned line of random_line should be defined";
84 1         362 ok defined($line3), "3rd returned line of random_line should be defined";
85 1   33     348 ok( ($line1 ne $line2 or $line1 ne $line3),
86             "3 random lines should be a bit different" );
87             }
88              
89             SIZE_IS_UNKNOWN: {
90 1         308 my ($line1, $line2, $line3) = random_line($self->{normal_file});
  1         4  
91 1         5 ok defined($line1), "1st returned line of random_line should be defined";
92 1         315 ok defined($line2), "2nd returned line of random_line should be defined";
93 1         300 ok defined($line3), "3rd returned line of random_line should be defined";
94 1   33     302 ok( ($line1 ne $line2 or $line1 ne $line3),
95             "3 random lines should be a bit different" );
96             }
97 1     1   394 }
  1         10  
  1         6  
98              
99             sub warns_if_called_with_line_nr_in_scalar_context : Test(1) {
100 1     1 0 755 my $self = shift;
101 1     1   73 warning_like {scalar random_line($self->{normal_file},3)}
102 1         25 {carped => qr/called in scalar context/},
103             "should give a warning random_line(fname,3) is called in scalar context";
104 1     1   339 }
  1         2  
  1         16  
105              
106             sub warns_if_zero_random_lines : Test(1) {
107 1     1 0 713 my $self = shift();
108 1     1   44 warning_like { (random_line($self->{normal_file},0)) }
109 1         12 {carped => qr/0 random lines/},
110             "should give a warning random_line(fname,0)";
111 1     1   324 }
  1         9  
  1         4  
112              
113             sub warns_not_if_called_in_list_context_without_line_nr_specification : Test(1) {
114 1     1 0 641 my $self = shift();
115 1     1   42 warnings_are { [ random_line($self->{normal_file}) ] }
116 1         9 [],
117             "should give a warning if random_line(fname) in list context";
118 1     1   285 }
  1         1  
  1         4  
119              
120             sub nr_of_lines_greater_than_lines_in_file : Test(2) {
121 1     1 0 653 my $self = shift;
122 1         11 my @line = random_line($self->{normal_file},100);
123 1         12 ok @line == 100, "random_line(file, 100) should return a list of 100 elements";
124 1     100   360 all_ok {defined($_[0])} \@line, "random_line(file, 100) - all lines should be defined";
  100         430  
125 1     1   327 }
  1         2  
  1         4  
126              
127 0           sub wrong_parameters : Test(5) {
128 1     1 0 641 my $self = shift;
129 1     1   254 no warnings; # $_ shall be undefined for a moment !
  1         1  
  1         161  
130 1         3 foreach (WRONG_PARAMS) {
131 3     3   368 dies_ok(sub {random_line($_)}, "random_line( '$_' ) should die");
  3         97  
132 3 100       973 next unless defined;
133 2     2   61 dies_ok(sub {(random_line($self->{normal_file},$_))},
134 2         15 "random_line(fname, '$_' ) should die");
135             }
136 1     1   8 use warnings; # warnings back
  1         1  
  1         48  
137 1     1   6 }
  1         2  
  1         4  
138              
139             1;