File: 0-examples-xrefs.t

package info (click to toggle)
libconstant-defer-perl 6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 288 kB
  • sloc: perl: 1,603; sh: 128; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 2,240 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl -w

# Copyright 2011, 2013 Kevin Ryde

# 0-examples-xrefs.t is shared by several distributions.
#
# 0-examples-xrefs.t is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# 0-examples-xrefs.t is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this file.  If not, see <http://www.gnu.org/licenses/>.

BEGIN { require 5 }
use strict;
use ExtUtils::Manifest;
use Test::More;

use lib 't';
use MyTestHelpers;
BEGIN { MyTestHelpers::nowarnings(); }

my $manifest = ExtUtils::Manifest::maniread();
my @example_files = grep m{examples/.*\.pl$}, keys %$manifest;
my @lib_files = grep m{lib/.*\.(pm|pod)$}, keys %$manifest;

sub any_file_contains_example {
  my ($example) = @_;
  my $filename;
  foreach $filename (@lib_files) {
    if (pod_contains_example($filename, $example)) {
      return 1;
    }
  }
  foreach $filename (@example_files) {
    if ($filename ne $example
        && raw_contains_example($filename, $example)) {
      return 1;
    }
  }
  return 0;
}

sub pod_contains_example {
  my ($filename, $example) = @_;
  open FH, "< $filename" or die "Cannot open $filename: $!";
  my $content = do { local $/; <FH> }; # slurp
  close FH or die "Error closing $filename: $!";
  return scalar ($content =~ /F<\Q$example\E>
                            |F<examples>\s+directory
                             /xs);
}
sub raw_contains_example {
  my ($filename, $example) = @_;
  $example =~ s{^examples/}{};
  open FH, "< $filename" or die "Cannot open $filename: $!";
  my $ret = scalar (grep /\b\Q$example\E\b/, <FH>);
  close FH or die "Error closing $filename: $!";
  return $ret > 0;
}


plan tests => scalar(@example_files) + 1;
my $example;
foreach $example (@example_files) {
  is (any_file_contains_example($example), 1,
      "$example mentioned in some lib/ file");
}
ok(1);

exit 0;