File: oo-api.t

package info (click to toggle)
libfile-libmagic-perl 1.02-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 524 kB
  • ctags: 849
  • sloc: perl: 1,616; pascal: 88; ansic: 54; makefile: 13
file content (165 lines) | stat: -rw-r--r-- 4,136 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use strict;
use warnings;

use lib 't/lib';

use Test::AnyOf;
use Test::More 0.88;

use File::LibMagic;

my %standard = (
    'foo.foo' => [
        'ASCII text', qr{text/plain(?:; charset=us-ascii)?} ],
    'foo.c'   => [
        [ 'ASCII C program text', 'C source, ASCII text' ],
        qr{text/x-c(?:; charset=us-ascii)?}
    ],
);

my %custom = (
    'foo.foo' => [ 'A foo file', qr{text/plain(?:; charset=us-ascii)?} ],
    'foo.c'   => [
        [ 'ASCII text', 'ASCII C program text', 'C source, ASCII text' ],
        qr{text/(?:plain|(?:x-)?c)(?:; charset=us-ascii)?}
    ],
);

# try using a the standard magic database
my $flm = File::LibMagic->new();
isa_ok( $flm, 'File::LibMagic' );

for my $file ( sort keys %standard ) {
    my ( $descr, $mime ) = @{ $standard{$file} };
    $file = "t/samples/$file";

    # the original file utility uses text/plain;...  so does gentoo, debian,
    # etc ..., but OpenSUSE returns text/plain... (no semicolon)
    $mime =~ s/;/;?/g;
    like(
        $flm->checktype_filename($file), qr/$mime/,
        "MIME $file - standard magic file"
    );

    if ( ref $descr ) {
        is_any_of(
            $flm->describe_filename($file), $descr,
            "Describe $file - standard magic file"
        );
    }
    else {
        is(
            $flm->describe_filename($file), $descr,
            "Describe $file - standard magic file"
        );
    }

    my $data = do {
        local $/;
        open my $fh, '<', $file or die $!;
        <$fh>;
    };

    like(
        $flm->checktype_contents($data), qr/$mime/,
        "MIME data $file file contents - standard magic file"
    );

    like(
        $flm->checktype_contents(\$data), qr/$mime/,
        "MIME data $file contents as ref - standard magic file"
    );

    if ( ref $descr ) {
        is_any_of(
            $flm->describe_contents($data), $descr,
            "Describe data $file contents - standard magic file"
        );
        is_any_of(
            $flm->describe_contents(\$data), $descr,
            "Describe data $file contents as ref - standard magic file"
        );
    }
    else {
        is(
            $flm->describe_contents($data), $descr,
            "Describe data $file contents - standard magic file"
        );
        is(
            $flm->describe_contents(\$data), $descr,
            "Describe data $file contents as ref - standard magic file"
        );
    }
}

# try using a custom magic database
$flm = File::LibMagic->new('t/samples/magic');
isa_ok( $flm, 'File::LibMagic' );

for my $file ( sort keys %custom ) {
    my ( $descr, $mime ) = @{ $custom{$file} };
    $file = "t/samples/$file";

    # OpenSUSE fix
    $mime =~ s/;/;?/g;

    # text/x-foo to keep netbsd and older solaris installations happy
    like(
        $flm->checktype_filename($file), qr{$mime|text/x-foo},
        "MIME $file - custom magic file"
    );

    if ( ref $descr ) {
        is_any_of(
            $flm->describe_filename($file), $descr,
            "Describe $file - custom magic file"
        );
    }
    else {
        is(
            $flm->describe_filename($file), $descr,
            "Describe $file - custom magic file"
        );
    }
    my $data = do {
        local $/;
        open my $fh, '<', $file or die $!;
        <$fh>;
    };

    # text/x-foo to keep netbsd and older solaris installations happy
    like(
        $flm->checktype_contents($data), qr{$mime|text/x-foo},
        "MIME data $file - custom magic file"
    );

    if ( ref $descr ) {
        is_any_of(
            $flm->describe_contents($data), $descr,
            "Describe data $file - custom magic file"
        );
    }
    else {
        is(
            $flm->describe_contents($data), $descr,
            "Describe data $file - custom magic file"
        );
    }
}

my $subclass = My::Magic::Subclass->new();
isa_ok( $subclass, 'My::Magic::Subclass', 'subclass' );
is(
    $subclass->checktype_filename('t/samples/missing'),
    'text/x-test-passes'
);

done_testing();

{
    package My::Magic::Subclass;

    use base qw( File::LibMagic );

    sub checktype_filename { 'text/x-test-passes' }
}