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
2             RandomLine;
3 1     1   9610 use base qw/Test::Class/;
  1         2  
  1         710  
4              
5 1     1   20967 use strict;
  1         2  
  1         21  
6 1     1   4 use warnings;
  1         2  
  1         23  
7              
8 1     1   4 use Test::More;
  1         2  
  1         13  
9 1     1   820 use Test::Exception;
  1         3088  
  1         4  
10 1     1   1086 use File::Temp qw/tempfile/;
  1         22246  
  1         63  
11 1     1   543 use Set::Scalar;
  1         11065  
  1         45  
12 1     1   539 use Test::Warn;
  1         2419  
  1         78  
13 1     1   523 use Test::ManyParams;
  1         4217  
  1         5  
14              
15 1     1   142 use File::Random qw/random_line/;
  1         2  
  1         62  
16              
17 1     1   6 use constant LINES => <<'EOF';
  1         2  
  1         55  
18             Random
19             Lines
20             can
21             contain
22             randomly
23             appearing things like
24             PI = 3.1415926535
25             but of course that's not neccessary
26             EOF
27              
28 1     1   6 use constant WRONG_PARAMS => (undef, '', '&');
  1         2  
  1         53  
29              
30 1     1   5 use constant SAMPLE_SIZE => 200;
  1         1  
  1         97  
31              
32             sub fill_tempfile : Test(setup) {
33 10     10 0 7653 my $self = shift;
34 10         35 (my $normal_fh, $self->{normal_file}) = tempfile();
35 10         3638 (undef, $self->{empty_file}) = tempfile();
36 10         3659 print $normal_fh LINES;
37 1     1   7 }
  1         1  
  1         7  
38              
39             sub clear_tempfile : Test(teardown) {
40 10     10 0 11478 my $self = shift;
41 10         47 close $self->{'normal_file'};
42 10         53 close $self->{'empty_file'};
43 1     1   383 }
  1         1  
  1         4  
44              
45             sub empty_file_returns_undef : Test(1) {
46 1     1 0 790 my $self = shift;
47 1         12 is random_line($self->{empty_file}), undef, "random_file( Empty file )";
48 1     1   309 }
  1         2  
  1         4  
49              
50             sub lines_are_the_expected_ones : Test(1) {
51 1     1 0 651 my $self = shift;
52 1         6 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         28  
53 1         331 my $got = Set::Scalar->new();
54 1         61 $got->insert( scalar random_line($self->{normal_file}) ) for (1 .. SAMPLE_SIZE);
55 1         62 is $got, $exp, "random_line( Normal File )";
56 1     1   365 }
  1         1  
  1         7  
57              
58             sub multiple_lines_are_the_expected_ones : Test(2) {
59 1     1 0 653 my $self = shift;
60 1         7 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         19  
61 1         111 my $got = Set::Scalar->new();
62 1         59 $got->insert( random_line($self->{normal_file},3) ) for (1 .. SAMPLE_SIZE()/3);
63 1         70 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
64 1         344 $got->clear;
65 1         74 for (1 .. SAMPLE_SIZE()/3) {
66 66         4443 my ($line1, $line2, $line3) = random_line($self->{normal_file});
67 66         235 $got->insert($line1, $line2, $line3);
68             }
69 1         70 is $got, $exp, '($line1, $line2, $line3) = random_line $fname [expected files]';
70 1     1   503 }
  1         9  
  1         5  
71              
72             sub multiple_lines_are_the_expected_ones_random_line_with_sample_size : Test(1) {
73 1     1 0 655 my $self = shift;
74 1         7 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         18  
75 1         111 my $got = Set::Scalar->new(random_line($self->{normal_file}, SAMPLE_SIZE()));
76 1         875 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
77 1     1   355 }
  1         2  
  1         3  
78              
79             sub get_really_lines : Test(8) {
80 1     1 0 670 my $self = shift;
81             SIZE_IS_KNOWN: {
82 1         2 my ($line1, $line2, $line3) = random_line($self->{normal_file},3);
  1         5  
83 1         48 ok defined($line1), "1st returned line of random_line should be defined";
84 1         414 ok defined($line2), "2nd returned line of random_line should be defined";
85 1         316 ok defined($line3), "3rd returned line of random_line should be defined";
86 1   33     348 ok( ($line1 ne $line2 or $line1 ne $line3),
87             "3 random lines should be a bit different" );
88             }
89              
90             SIZE_IS_UNKNOWN: {
91 1         314 my ($line1, $line2, $line3) = random_line($self->{normal_file});
  1         5  
92 1         5 ok defined($line1), "1st returned line of random_line should be defined";
93 1         343 ok defined($line2), "2nd returned line of random_line should be defined";
94 1         309 ok defined($line3), "3rd returned line of random_line should be defined";
95 1   33     309 ok( ($line1 ne $line2 or $line1 ne $line3),
96             "3 random lines should be a bit different" );
97             }
98 1     1   408 }
  1         2  
  1         4  
99              
100             sub warns_if_called_with_line_nr_in_scalar_context : Test(1) {
101 1     1 0 655 my $self = shift;
102 1     1   69 warning_like {scalar random_line($self->{normal_file},3)}
103 1         23 {carped => qr/called in scalar context/},
104             "should give a warning random_line(fname,3) is called in scalar context";
105 1     1   335 }
  1         2  
  1         19  
106              
107             sub warns_if_zero_random_lines : Test(1) {
108 1     1 0 647 my $self = shift();
109 1     1   45 warning_like { (random_line($self->{normal_file},0)) }
110 1         10 {carped => qr/0 random lines/},
111             "should give a warning random_line(fname,0)";
112 1     1   314 }
  1         17  
  1         6  
113              
114             sub warns_not_if_called_in_list_context_without_line_nr_specification : Test(1) {
115 1     1 0 645 my $self = shift();
116 1     1   40 warnings_are { [ random_line($self->{normal_file}) ] }
117 1         7 [],
118             "should give a warning if random_line(fname) in list context";
119 1     1   279 }
  1         1  
  1         4  
120              
121             sub nr_of_lines_greater_than_lines_in_file : Test(2) {
122 1     1 0 660 my $self = shift;
123 1         4 my @line = random_line($self->{normal_file},100);
124 1         8 ok @line == 100, "random_line(file, 100) should return a list of 100 elements";
125 1     100   376 all_ok {defined($_[0])} \@line, "random_line(file, 100) - all lines should be defined";
  100         475  
126 1     1   319 }
  1         2  
  1         3  
127              
128 0           sub wrong_parameters : Test(5) {
129 1     1 0 646 my $self = shift;
130 1     1   271 no warnings; # $_ shall be undefined for a moment !
  1         2  
  1         148  
131 1         4 foreach (WRONG_PARAMS) {
132 3     3   337 dies_ok(sub {random_line($_)}, "random_line( '$_' ) should die");
  3         97  
133 3 100       974 next unless defined;
134 2     2   61 dies_ok(sub {(random_line($self->{normal_file},$_))},
135 2         16 "random_line(fname, '$_' ) should die");
136             }
137 1     1   6 use warnings; # warnings back
  1         2  
  1         47  
138 1     1   5 }
  1         2  
  1         3  
139              
140             1;