File Coverage

test/lib/RandomLine.pm
Criterion Covered Total %
statement 148 149 99.3
branch 2 2 100.0
condition 2 6 33.3
subroutine 45 45 100.0
pod 0 12 0.0
total 197 214 92.0


line stmt bran cond sub pod time code
1             package
2             RandomLine;
3 1     1   10954 use base qw/Test::Class/;
  1         2  
  1         776  
4              
5 1     1   23712 use strict;
  1         3  
  1         26  
6 1     1   5 use warnings;
  1         2  
  1         30  
7              
8 1     1   5 use Test::More;
  1         2  
  1         15  
9 1     1   1094 use Test::Exception;
  1         3613  
  1         5  
10 1     1   1349 use File::Temp qw/tempfile/;
  1         25594  
  1         81  
11 1     1   599 use Set::Scalar;
  1         12744  
  1         60  
12 1     1   637 use Test::Warn;
  1         2747  
  1         93  
13 1     1   750 use Test::ManyParams;
  1         4714  
  1         7  
14              
15 1     1   261 use File::Random qw/random_line/;
  1         2  
  1         75  
16              
17 1     1   6 use constant LINES => <<'EOF';
  1         2  
  1         84  
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         1  
  1         61  
29              
30 1     1   7 use constant SAMPLE_SIZE => 200;
  1         1  
  1         105  
31              
32             sub fill_tempfile : Test(setup) {
33 10     10 0 8269 my $self = shift;
34 10         74 (my $normal_fh, $self->{normal_file}) = tempfile();
35 10         5525 (undef, $self->{empty_file}) = tempfile();
36 10         4150 print $normal_fh LINES;
37 1     1   6 }
  1         1  
  1         8  
38              
39             sub clear_tempfile : Test(teardown) {
40 10     10 0 12604 my $self = shift;
41 10         81 close $self->{'normal_file'};
42 10         59 close $self->{'empty_file'};
43 1     1   452 }
  1         2  
  1         5  
44              
45             sub empty_file_returns_undef : Test(1) {
46 1     1 0 873 my $self = shift;
47 1         16 is random_line($self->{empty_file}), undef, "random_file( Empty file )";
48 1     1   311 }
  1         2  
  1         4  
49              
50             sub lines_are_the_expected_ones : Test(1) {
51 1     1 0 713 my $self = shift;
52 1         7 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         31  
53 1         333 my $got = Set::Scalar->new();
54 1         64 $got->insert( scalar random_line($self->{normal_file}) ) for (1 .. SAMPLE_SIZE);
55 1         122 is $got, $exp, "random_line( Normal File )";
56 1     1   388 }
  1         2  
  1         6  
57              
58             sub multiple_lines_are_the_expected_ones : Test(2) {
59 1     1 0 734 my $self = shift;
60 1         7 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         22  
61 1         146 my $got = Set::Scalar->new();
62 1         64 $got->insert( random_line($self->{normal_file},3) ) for (1 .. SAMPLE_SIZE()/3);
63 1         77 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
64 1         475 $got->clear;
65 1         77 for (1 .. SAMPLE_SIZE()/3) {
66 66         4493 my ($line1, $line2, $line3) = random_line($self->{normal_file});
67 66         257 $got->insert($line1, $line2, $line3);
68             }
69 1         75 is $got, $exp, '($line1, $line2, $line3) = random_line $fname [expected files]';
70 1     1   478 }
  1         2  
  1         5  
71              
72             sub multiple_lines_are_the_expected_ones_random_line_with_sample_size : Test(1) {
73 1     1 0 724 my $self = shift;
74 1         9 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
  8         24  
75 1         145 my $got = Set::Scalar->new(random_line($self->{normal_file}, SAMPLE_SIZE()));
76 1         940 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
77 1     1   366 }
  1         2  
  1         13  
78              
79             sub get_really_lines : Test(8) {
80 1     1 0 703 my $self = shift;
81              
82 1         5 srand(42); # Fix random seed to have deterministic tests, see also https://github.com/hanfried/File-Random/issues/6 or https://rt.cpan.org/Public/Bug/Display.html?id=123680
83              
84             SIZE_IS_KNOWN: {
85 1         3 my ($line1, $line2, $line3) = random_line($self->{normal_file},3);
  1         7  
86 1         61 print STDERR "Lines:", $line1, $line2, $line3, "\n";
87 1         9 ok defined($line1), "1st returned line of random_line should be defined";
88 1         463 ok defined($line2), "2nd returned line of random_line should be defined";
89 1         320 ok defined($line3), "3rd returned line of random_line should be defined";
90 1   33     316 ok( ($line1 ne $line2 or $line1 ne $line3),
91             "3 random lines should be a bit different" );
92             }
93              
94             SIZE_IS_UNKNOWN: {
95 1         308 my ($line1, $line2, $line3) = random_line($self->{normal_file});
  1         5  
96 1         6 ok defined($line1), "1st returned line of random_line should be defined";
97 1         345 ok defined($line2), "2nd returned line of random_line should be defined";
98 1         309 ok defined($line3), "3rd returned line of random_line should be defined";
99 1   33     308 ok( ($line1 ne $line2 or $line1 ne $line3),
100             "3 random lines should be a bit different" );
101             }
102 1     1   442 }
  1         3  
  1         4  
103              
104             sub warns_if_called_with_line_nr_in_scalar_context : Test(1) {
105 1     1 0 725 my $self = shift;
106 1     1   78 warning_like {scalar random_line($self->{normal_file},3)}
107 1         29 {carped => qr/called in scalar context/},
108             "should give a warning random_line(fname,3) is called in scalar context";
109 1     1   346 }
  1         3  
  1         23  
110              
111             sub warns_if_zero_random_lines : Test(1) {
112 1     1 0 700 my $self = shift();
113 1     1   72 warning_like { (random_line($self->{normal_file},0)) }
114 1         15 {carped => qr/0 random lines/},
115             "should give a warning random_line(fname,0)";
116 1     1   321 }
  1         18  
  1         7  
117              
118             sub warns_not_if_called_in_list_context_without_line_nr_specification : Test(1) {
119 1     1 0 698 my $self = shift();
120 1     1   57 warnings_are { [ random_line($self->{normal_file}) ] }
121 1         9 [],
122             "should give a warning if random_line(fname) in list context";
123 1     1   364 }
  1         2  
  1         4  
124              
125             sub nr_of_lines_greater_than_lines_in_file : Test(2) {
126 1     1 0 722 my $self = shift;
127 1         7 my @line = random_line($self->{normal_file},100);
128 1         11 ok @line == 100, "random_line(file, 100) should return a list of 100 elements";
129 1     100   518 all_ok {defined($_[0])} \@line, "random_line(file, 100) - all lines should be defined";
  100         453  
130 1     1   340 }
  1         1  
  1         4  
131              
132 0           sub wrong_parameters : Test(5) {
133 1     1 0 725 my $self = shift;
134 1     1   309 no warnings; # $_ shall be undefined for a moment !
  1         2  
  1         172  
135 1         4 foreach (WRONG_PARAMS) {
136 3     3   346 dies_ok(sub {random_line($_)}, "random_line( '$_' ) should die");
  3         112  
137 3 100       1051 next unless defined;
138 2     2   62 dies_ok(sub {(random_line($self->{normal_file},$_))},
139 2         16 "random_line(fname, '$_' ) should die");
140             }
141 1     1   8 use warnings; # warnings back
  1         2  
  1         52  
142 1     1   6 }
  1         2  
  1         4  
143              
144             1;