File Coverage

blib/lib/ChordPro/Testing.pm
Criterion Covered Total %
statement 90 114 78.9
branch 24 38 63.1
condition 14 29 48.2
subroutine 20 21 95.2
pod 0 6 0.0
total 148 208 71.1


line stmt bran cond sub pod time code
1             #! perl
2              
3 89     89   3421772 use strict;
  89         218  
  89         3720  
4 89     89   472 use warnings;
  89         201  
  89         5602  
5 89     89   590 use utf8;
  89         188  
  89         558  
6 89     89   3238 use Carp;
  89         278  
  89         11329  
7              
8 89     89   51671 use FindBin;
  89         139455  
  89         8044  
9 89     89   57204 use lib "$FindBin::Bin/../lib";
  89         79753  
  89         769  
10 89     89   16283 use lib "$FindBin::Bin/../lib/ChordPro/lib";
  89         205  
  89         458  
11              
12             binmode STDOUT => ':utf8';
13             binmode STDERR => ':utf8';
14              
15             package ChordPro::Testing;
16              
17 89     89   58060 use parent 'Exporter';
  89         29380  
  89         613  
18             our @EXPORT = qw( $config );
19              
20 89     89   64063 use Test::More ();
  89         9040150  
  89         3545  
21              
22 89     89   53456 use ChordPro::Files;
  89         440  
  89         15953  
23 89     89   70363 use ChordPro::Config;
  89         483  
  89         44036  
24 89     89   1300 use ChordPro::Paths;
  89         214  
  89         6447  
25 89     89   665 use ChordPro::Chords;
  89         225  
  89         62013  
26              
27             sub import {
28 89     89   1414 my $pkg = shift;
29              
30             # This is dirty...
31 89 100       3967 -d "t" && chdir "t";
32              
33 89         1822 $::running_under_test = 1;
34 89         27194 Test::More->export_to_level(1);
35 89         52781 $pkg->export_to_level( 1, undef, @EXPORT );
36             }
37              
38             sub is_deeply {
39 138     138 0 384754 my ( $got, $expect, $tag ) = @_;
40              
41 138 100 66     1346 if ( ref($got) eq 'HASH' && ref($expect) eq 'HASH' ) {
42 107 100       688 fixchords($got) if $got->{body};
43              
44 107         309 for ( qw( config generate ) ) {
45 214 50       954 delete $got->{$_} unless exists $expect->{$_};
46             }
47 107 100       582 if ( $got->{chordsinfo} ) {
48 89 100 100     215 if ( !%{$got->{chordsinfo}} && !$expect->{chordsinfo} ) {
49 51         158 delete $got->{chordsinfo};
50             }
51             else {
52 38         154 foreach ( keys %{ $got->{chordsinfo} } ) {
  38         190  
53 101         395 $got->{chordsinfo}{$_} = $got->{chordsinfo}{$_}->name;
54             }
55             }
56             }
57 107         375 for ( qw( instrument user key_from key_actual chords numchords
58             _configversion bookmark
59             ) ) {
60 856 100       2870 delete $got->{meta}->{$_} unless exists $expect->{meta}->{$_};
61             }
62             }
63              
64 138         768 Test::More::is_deeply( $got, $expect, $tag );
65             }
66              
67             push( @EXPORT, 'is_deeply' );
68              
69             sub testconfig {
70             # May change later.
71 89     89 0 615 ChordPro::Config::configurator;
72             }
73              
74             push( @EXPORT, 'testconfig' );
75              
76             our $config = testconfig();
77              
78             ChordPro::Chords::add_config_chord
79             ( { name => "NC", base => 1, frets => [ (-1)x6 ], fingers => [] } );
80              
81             {
82 89     89   828 no warnings 'redefine';
  89         202  
  89         120829  
83              
84             sub getresource {
85 11     11 0 2633077 CP->findres($_[0]);
86             }
87             }
88              
89             push( @EXPORT, 'getresource' );
90              
91             sub cmp {
92             # Perl version of the 'cmp' program.
93             # Returns 1 if the files differ, 0 if the contents are equal.
94 0     0 0 0 my ($old, $new) = @_;
95 0 0       0 unless ( open (F1, $old) ) {
96 0         0 print STDERR ("$old: $!\n");
97 0         0 return 1;
98             }
99 0 0       0 unless ( open (F2, $new) ) {
100 0         0 print STDERR ("$new: $!\n");
101 0         0 return 1;
102             }
103 0         0 my ($buf1, $buf2);
104 0         0 my ($len1, $len2);
105 0         0 while ( 1 ) {
106 0         0 $len1 = sysread (F1, $buf1, 10240);
107 0         0 $len2 = sysread (F2, $buf2, 10240);
108 0 0 0     0 return 0 if $len1 == $len2 && $len1 == 0;
109 0 0 0     0 return 1 if $len1 != $len2 || ( $len1 && $buf1 ne $buf2 );
      0        
110             }
111             }
112              
113             sub differ {
114 79     79 0 17027 my ($file1, $file2) = @_;
115 79 50       392 $file2 = "$file1" unless $file2;
116 79         279 $file1 = "$file1";
117              
118 79         759 my @lines1 = fs_load( $file1, { fail => 'hard' } );
119 79         747 my @lines2 = fs_load( $file2, { fail => 'hard' } );
120 79 50       938 my $linesm = @lines1 > @lines2 ? @lines1 : @lines2;
121 79         485 for ( my $line = 1; $line < $linesm; $line++ ) {
122 0 0       0 next if $lines1[$line] eq $lines2[$line];
123 0         0 Test::More::diag("Files $file1 and $file2 differ at line $line");
124 0         0 Test::More::diag(" < $lines1[$line]");
125 0         0 Test::More::diag(" > $lines2[$line]");
126 0         0 return 1;
127             }
128 79 50       1750 return 0 if @lines1 == @lines2;
129 0         0 $linesm++;
130 0         0 Test::More::diag("Files $file1 and $file2 differ at line $linesm" );
131 0   0     0 Test::More::diag(" < ", $lines1[$linesm] // "***missing***");
132 0   0     0 Test::More::diag(" > ", $lines2[$linesm] // "***missing***");
133 0         0 1;
134             }
135              
136             push( @EXPORT, 'differ' );
137              
138             sub fixchords {
139 42     42 0 169 my ( $s ) = @_;
140 42   50     88 for ( @{ $s->{body} // [] } ) {
  42         318  
141 213   100     364 for ( @{ $_->{chords} // [] } ) {
  213         848  
142 191 100       854 $_ = $_->key if UNIVERSAL::can( $_, "key" );
143             }
144 213   100     379 for ( @{ $_->{chorus} // [] } ) {
  213         800  
145 28   100     64 for ( @{ $_->{chords} // [] } ) {
  28         149  
146 47 100       201 $_ = $_->key if UNIVERSAL::can( $_, "key" );
147             }
148             }
149 213   100     365 for ( @{ $_->{tokens} // [] } ) {
  213         873  
150 136 100       455 if ( $_->{class} eq "chord" ) {
    100          
151 31         60 for ( $_->{chord} ) {
152 31         160 $_ = $_->key;
153             }
154             }
155             elsif ( $_->{class} eq "chords" ) {
156 1         4 for ( @{ $_->{chords} } ) {
  1         4  
157 2         7 $_ = $_->key;
158             }
159             }
160             }
161             }
162             }
163              
164             push( @EXPORT, 'fixchords' );
165              
166             1;