File: get_modules.t

package info (click to toggle)
libmodule-extract-use-perl 1.047-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 184 kB
  • sloc: perl: 572; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,809 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
use strict;

use Test::More 1.0;
use File::Basename;
use File::Spec::Functions qw(catfile);

my $class = "Module::Extract::Use";

use_ok( $class );

my $extor = $class->new;

subtest setup => sub {
	isa_ok( $extor, $class );
	can_ok( $extor, 'get_modules' );
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Try it with a file that doesn't exist, should fail
subtest missing => sub {
	my $not_there = 'not_there';
	ok( ! -e $not_there, "Missing file is actually missing" );

	$extor->get_modules( $not_there );
	like( $extor->error, qr/does not exist/, "Missing file give right error" );
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Try it with a file that doesn't exist, should fail
subtest non_perl => sub {
	my $non_perl = 'corpus';
	ok( -e $non_perl, "Non-perl file is actually missing" );

	my $result = $extor->get_modules( $non_perl );
	is( $result, 0, "Non-perl file returns 0 in scalar context" );

	my @results = $extor->get_modules( $non_perl );
	is( scalar @results, 0, "Non-perl file returns empty list in list context" );
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Try it with this file
subtest this_file => sub {
	my $test = $0;
	ok( -e $test, "Test file is there" );

	my %modules = map { $_, 1 } $extor->get_modules( $test );
	ok( ! $extor->error, "No error for parseable file [$test]" );

	foreach my $module ( qw(Test::More File::Basename File::Spec::Functions strict) ) {
		ok( exists $modules{$module}, "Found $module" );
		}

	foreach my $module ( qw(Foo Bar::Baz) ) {
		ok( ! exists $modules{$module}, "Didn't find $module" );
		}
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Try it with a file that has repeated use lines
# I should only get unique names
subtest repeated => sub {
	my $file = catfile( qw(corpus Repeated.pm) );
	ok( -e $file, "Test file [$file] is there" );

	my @modules = sort { $a cmp $b } $extor->get_modules( $file );
	is( scalar @modules, 3 );

	is_deeply( \@modules, [qw(constant strict warnings)] );
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
subtest rt79273 => sub {
	my $file = catfile( qw(corpus RT79273.pm) );
	ok( -e $file, "Test file [$file] is there" );

	my @modules = sort { $a cmp $b } $extor->get_modules( $file );
	is( scalar @modules, 3 );

	is_deeply( \@modules, [qw(CGI::Snapp Capture::Tiny parent)] );
	};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
subtest expression => sub {
	my $file = catfile( qw(corpus state_require.pm) );
	ok( -e $file, "Test file [$file] is there" );

	my @modules = sort { $a cmp $b } $extor->get_modules( $file );
	is( scalar @modules, 2 );

	is_deeply( \@modules, [qw(ConfigReader::Simple Mojo::Util)] );
	};

done_testing();