File: utf8-data.t

package info (click to toggle)
libmaxmind-db-writer-perl 0.300003-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,336 kB
  • sloc: ansic: 3,059; perl: 2,895; makefile: 5; sh: 4
file content (88 lines) | stat: -rw-r--r-- 1,908 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
82
83
84
85
86
87
88
use strict;
use warnings;

use Test::Builder;
use Test::More;

use Test::Requires (
    'MaxMind::DB::Reader' => 0.040000,
);

use MaxMind::DB::Writer::Tree;

use Encode ();
use File::Temp qw( tempdir );
use MaxMind::DB::Reader;
use Net::Works::Network;

{
    my $tb = Test::Builder->new();

    for ( $tb->output, $tb->failure_output, $tb->todo_output ) {
        binmode $_, ':encoding(UTF-8)' or die $!;
    }
}

my $tempdir = tempdir( CLEANUP => 1 );

my $utf8_string = "\x{4eba}";

{
    my $filename = _write_tree();

    my $reader = MaxMind::DB::Reader->new( file => $filename );

    for my $address (qw( 1.2.3.0 1.2.3.128 1.2.3.255 )) {
        is_deeply(
            $reader->record_for_address($address),
            {
                subnet => '1.2.3.0/24',
                string => $utf8_string,
            },
            "got expected data for $address"
        );
    }

    my $string = $reader->record_for_address('1.2.3.0')->{string};

    ok(
        Encode::is_utf8($string),
        "string from lookup ($string) is marked as utf8"
    );
}

done_testing();

sub _write_tree {
    my $tree = MaxMind::DB::Writer::Tree->new(
        ip_version    => 4,
        record_size   => 24,
        database_type => 'Test',
        languages     => [ 'en', 'zh' ],
        description   => {
            en => 'Test Database',
            zh => 'Test Database Chinese',
        },
        map_key_type_callback => sub { 'utf8_string' },
    );

    my $subnet = Net::Works::Network->new_from_string(
        string  => '1.2.3.0/24',
        version => 4,
    );

    $tree->insert_network(
        $subnet,
        {
            subnet => $subnet->as_string(),
            string => $utf8_string,
        },
    );

    my $filename = $tempdir . '/Test-utf8-string.mmdb';
    open my $fh, '>', $filename or die $!;
    $tree->write_tree($fh);
    close $fh or die $!;

    return $filename;
}