test/lib/RandomLine.pm

filter: Statementstmt 100% 149 / 149 Branchbran 100% 2 / 2 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 45 / 45 Podpod 0.0% 0 / 12 totaltotal 90.8% 198 / 218 CC 8 SCAR 3.3
CC
8
Cov
97%
get_really_lines
3.1
multiple_lines_are_the_expected_ones
3.0
wrong_parameters
3.0
CRAP 1.4
« test/lib/RandomFileCheckOption.pm ↑ Summary test/lib/RandomFileWithUnknownParameters.pm »
1 package
2 RandomLine;
3 1 ▶use base qw/Test::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Test::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Test::Warn;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Test::ManyParams;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use File::Random qw/random_line/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use constant LINES => <<'EOF';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶use constant WRONG_PARAMS => (undef, '', '&');
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use constant SAMPLE_SIZE => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 10 ▶sub fill_tempfile : Test(setup) {
Subroutine fill_tempfile: called CC=1 SCAR=0.0
Pod undocumented
33 10 my $self = shift;
34 10 (my $normal_fh, $self->{normal_file}) = tempfile();
35 10 (undef, $self->{empty_file}) = tempfile();
36 10 print $normal_fh LINES;
37 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 10 ▶sub clear_tempfile : Test(teardown) {
Subroutine clear_tempfile: called CC=1 SCAR=0.0
Pod undocumented
40 10 my $self = shift;
41 10 close $self->{'normal_file'};
42 10 close $self->{'empty_file'};
43 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 ▶sub empty_file_returns_undef : Test(1) {
Subroutine empty_file_returns_undef: called CC=1 SCAR=0.0
Pod undocumented
46 1 my $self = shift;
47 1 is random_line($self->{empty_file}), undef, "random_file( Empty file )";
48 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 1 ▶sub lines_are_the_expected_ones : Test(1) {
Subroutine lines_are_the_expected_ones: called CC=2 SCAR=6.9
Pod undocumented
51 1 my $self = shift;
52 1 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
53 1 my $got = Set::Scalar->new();
54 1 $got->insert( scalar random_line($self->{normal_file}) ) for (1 .. SAMPLE_SIZE);
55 1 is $got, $exp, "random_line( Normal File )";
56 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶sub multiple_lines_are_the_expected_ones : Test(2) {
Subroutine multiple_lines_are_the_expected_ones: called CC=3 SCAR=11.0
Pod undocumented
59 1 my $self = shift;
60 1 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
61 1 my $got = Set::Scalar->new();
62 1 $got->insert( random_line($self->{normal_file},3) ) for (1 .. SAMPLE_SIZE()/3);
63 1 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
64 1 $got->clear;
65 1 for (1 .. SAMPLE_SIZE()/3) {
66 66 my ($line1, $line2, $line3) = random_line($self->{normal_file});
67 66 $got->insert($line1, $line2, $line3);
68 }
69 1 is $got, $exp, '($line1, $line2, $line3) = random_line $fname [expected files]';
70 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 1 ▶sub multiple_lines_are_the_expected_ones_random_line_with_sample_size : Test(1) {
Subroutine multiple_lines_are_the_expected_ones_random_line_with_sample_size: called CC=1 SCAR=0.0
Pod undocumented
73 1 my $self = shift;
74 1 my $exp = Set::Scalar->new( map {"$_\n"} split /\n/, LINES);
75 1 my $got = Set::Scalar->new(random_line($self->{normal_file}, SAMPLE_SIZE()));
76 1 is $got, $exp, "random_line( Normal File, 3 ) [expected files]";
77 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 1 ▶sub get_really_lines : Test(8) {
Subroutine get_really_lines: called CC=3 SCAR=11.2
Pod undocumented
80 1 my $self = shift;
81  
82 1 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 my ($line1, $line2, $line3) = random_line($self->{normal_file},3);
86 1 print STDERR "Lines:", $line1, $line2, $line3, "\n";
87 1 ok defined($line1), "1st returned line of random_line should be defined";
88 1 ok defined($line2), "2nd returned line of random_line should be defined";
89 1 ok defined($line3), "3rd returned line of random_line should be defined";
90 1 ▶ ok( ($line1 ne $line2 or $line1 ne $line3),
Condition33%1/3
$line1 ne $line2 || $line1 ne $line3
l!l&&r!l&&!r
100
MC/DC0%0/2
$line1 ne $line2 || $line1 ne $line3
$line1 ne $line2 $line1 ne $line3
Truth table33%1/3
A || B
A= $line1 ne $line2B= $line1 ne $line3
ABresult
1X1
011
000
91 "3 random lines should be a bit different" );
92 }
93  
94 SIZE_IS_UNKNOWN: {
95 1 my ($line1, $line2, $line3) = random_line($self->{normal_file});
96 1 ok defined($line1), "1st returned line of random_line should be defined";
97 1 ok defined($line2), "2nd returned line of random_line should be defined";
98 1 ok defined($line3), "3rd returned line of random_line should be defined";
99 1 ▶ ok( ($line1 ne $line2 or $line1 ne $line3),
Condition33%1/3
$line1 ne $line2 || $line1 ne $line3
l!l&&r!l&&!r
100
MC/DC0%0/2
$line1 ne $line2 || $line1 ne $line3
$line1 ne $line2 $line1 ne $line3
Truth table33%1/3
A || B
A= $line1 ne $line2B= $line1 ne $line3
ABresult
1X1
011
000
100 "3 random lines should be a bit different" );
101 }
102 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 1 ▶sub warns_if_called_with_line_nr_in_scalar_context : Test(1) {
Subroutine warns_if_called_with_line_nr_in_scalar_context: called CC=1 SCAR=0.0
Pod undocumented
105 1 my $self = shift;
106 1 ▶ warning_like {scalar random_line($self->{normal_file},3)}
Subroutine __ANON__: called CC=1 SCAR=0.0
107 1 {carped => qr/called in scalar context/},
108 "should give a warning random_line(fname,3) is called in scalar context";
109 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 1 ▶sub warns_if_zero_random_lines : Test(1) {
Subroutine warns_if_zero_random_lines: called CC=1 SCAR=0.0
Pod undocumented
112 1 my $self = shift();
113 1 ▶ warning_like { (random_line($self->{normal_file},0)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
114 1 {carped => qr/0 random lines/},
115 "should give a warning random_line(fname,0)";
116 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 1 ▶sub warns_not_if_called_in_list_context_without_line_nr_specification : Test(1) {
Subroutine warns_not_if_called_in_list_context_without_line_nr_specification: called CC=1 SCAR=0.0
Pod undocumented
119 1 my $self = shift();
120 1 ▶ warnings_are { [ random_line($self->{normal_file}) ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
121 1 [],
122 "should give a warning if random_line(fname) in list context";
123 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 1 ▶sub nr_of_lines_greater_than_lines_in_file : Test(2) {
Subroutine nr_of_lines_greater_than_lines_in_file: called CC=1 SCAR=0.0
Pod undocumented
126 1 my $self = shift;
127 1 my @line = random_line($self->{normal_file},100);
128 1 ok @line == 100, "random_line(file, 100) should return a list of 100 elements";
129 1 ▶ all_ok {defined($_[0])} \@line, "random_line(file, 100) - all lines should be defined";
Subroutine __ANON__: called CC=1 SCAR=0.0
130 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 1 ▶sub wrong_parameters : Test(5) {
Subroutine wrong_parameters: called CC=3 SCAR=11.0
Pod undocumented
133 1 my $self = shift;
134 1 ▶ no warnings; # $_ shall be undefined for a moment !
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 foreach (WRONG_PARAMS) {
136 3 ▶ dies_ok(sub {random_line($_)}, "random_line( '$_' ) should die");
Subroutine __ANON__: called CC=1 SCAR=0.0
137 3 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 1 2 3
138 2 ▶ dies_ok(sub {(random_line($self->{normal_file},$_))},
Subroutine __ANON__: called CC=1 SCAR=0.0
139 2 "random_line(fname, '$_' ) should die");
140 }
141 1 ▶ use warnings; # warnings back
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 1 1;
« test/lib/RandomFileCheckOption.pm ↑ Summary test/lib/RandomFileWithUnknownParameters.pm »