File: Decoder.t

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 (81 lines) | stat: -rw-r--r-- 1,665 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
75
76
77
78
79
80
81
# PP only
use strict;
use warnings;
use autodie;

use Test::Fatal;
use Test::More;

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

use MaxMind::DB::Reader::Decoder;

{
    my $ignored;
    open my $fh, '<', \$ignored;

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

    my $str = 'foo';
    is(
        $decoder->_zero_pad_left( $str, 3 ),
        $str,
        'decoder does not add left padding when it is not needed'
    );

    is(
        $decoder->_zero_pad_left( $str, 4 ),
        "\x00$str",
        'decoder added one zero byte at the left of the content'
    );

    is(
        $decoder->_zero_pad_left( $str, 6 ),
        "\x00\x00\x00$str",
        'decoder added one three bytes at the left of the content'
    );
}

{
    my $data = 'this is some data';
    open my $fh, '<', \$data;

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

    my $buffer;

    $decoder->_read( \$buffer, 0, 7 );

    is(
        $buffer,
        'this is',
        '_read( 0, 7 ) got the expected data'
    );

    $decoder->_read( \$buffer, 1, 3 );

    is(
        $buffer,
        'his',
        '_read( 1, 3 ) got the expected data'
    );

    like(
        exception { $decoder->_read( \$buffer, 5, 999 ) },
        qr{\QAttempted to read past the end of a file/memory buffer},
        'got an error when trying to read past the end of the data source'
    );

    like(
        exception { $decoder->decode() },
        qr/\QYou must provide an offset to decode from when calling ->decode/,
        'got an error when calling ->decode without an offset'
    );
}

done_testing();