File: Decoder.pm

package info (click to toggle)
libmaxmind-db-reader-perl 1.000014-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,040 kB
  • sloc: perl: 1,668; makefile: 10
file content (74 lines) | stat: -rw-r--r-- 1,778 bytes parent folder | download | duplicates (3)
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
package Test::MaxMind::DB::Reader::Decoder;

use strict;
use warnings;
use autodie;

use List::AllUtils qw( natatime );
use MaxMind::DB::Reader::Decoder;
use Scalar::Util 1.42 qw( blessed );
use Test::More;
use Test::Number::Delta;

use lib 't/lib';
use Test::MaxMind::DB::Reader;

use Exporter qw( import );

our @EXPORT_OK = qw(
    test_decoding_of_type
);

sub test_decoding_of_type {
    my $type  = shift;
    my $tests = shift;

    my $iter = natatime 2, @{$tests};
    while ( my ( $expect, $input ) = $iter->() ) {
        my $desc = "decoded $type - ";

        if ( ref $expect && !blessed $expect ) {
            $desc .=
                ref $expect eq 'HASH'
                ? 'hash with ' . ( scalar keys %{$expect} ) . ' keys'
                : 'array with ' . ( scalar @{$expect} ) . ' keys';
        }
        else {
            $desc .=
                length($expect) > 50
                ? substr( $expect, 0, 50 ) . '...(' . length($expect) . ')'
                : $expect;
        }

        my $encoded = join q{}, map { chr($_) } @{$input};
        open my $fh, '<', \$encoded;

        my $decoder = MaxMind::DB::Reader::Decoder->new(
            data_source       => $fh,
            _data_source_size => length($encoded),
        );

        my $value = $decoder->decode(0);

        # blessed objects are big ints
        if ( ref $expect && !blessed $expect ) {
            is_deeply(
                $value,
                $expect,
                $desc
            );
        }
        elsif ( $type eq 'float' || $type eq 'double' ) {
            delta_ok( $value, $decoder->decode(0), $desc );
        }
        else {
            is(
                $value,
                $expect,
                $desc
            );
        }
    }
}

1;