Data-CompactReadonly

 view release on metacpan or  search on metacpan

t/root-node-dictionary.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::Differences;
use Test::Exception;

use File::Temp qw(tempfile);
use String::Binary::Interpolation;

use Data::CompactReadonly;

my $header_bytes = "CROD\x00"; # version 0, byte pointers

my $DICTBYTE = $b10000000;
my $TEXTBYTE = $b00000000;
my $NULL     = $b11101000;
my $SHORT    = $b11001000;

subtest "empty dict", sub {
    open(my $fh, '<', \"$header_bytes$DICTBYTE\x00");
    isa_ok(
        my $dict = Data::CompactReadonly->read($fh),
        "Data::CompactReadonly::V0::Dictionary::Byte"
    );
    is($dict->count(), 0, "0 element dict");
    is($dict->_ptr_size(), 1, "1 byte pointers");
    eq_or_diff($dict->indices(), [], "can list collection indices");
};

subtest "1 element dict", sub {
    open(my $fh, '<', \(
        "\x00\x00".       # these don't count, we'll seek past them before we start
        "$header_bytes".                                     # 0x00
        "$DICTBYTE\x01".                                     # 0x05
        "\x09".    "\x0e".                                   # 0x07 and 0x08
        "$TEXTBYTE\x03cow".                                  # 0x09
        "$TEXTBYTE\x04calf"                                  # 0x0e
    ));
    read($fh, my $blah, 2);
    my $dict = Data::CompactReadonly->read($fh);
    is($dict->_db_base(), 2, "the fh was opened after having already been partially read");
    is($dict->count(), 1, "1 element dict");
    eq_or_diff($dict->indices(), ['cow'], "can list collection indices");
    is($dict->element('cow'), 'calf', "can fetch from a 1 element dict");
};

subtest "dict with Null key", sub {
    open(my $fh, '<', \(
        "$header_bytes".                                     # 0x00
        "$DICTBYTE\x01".                                     # 0x05
        "\x09".    "\x0a".                                   # 0x07 and 0x08
        "$NULL".                                             # 0x09
        "$TEXTBYTE\x04calf"                                  # 0x0a
    ));
    my $dict = Data::CompactReadonly->read($fh);
    is($dict->count(), 1, "1 element dict");
    throws_ok { $dict->indices() }
        qr/Invalid type: Null: Dictionary keys must be Text/,
        "finding a bad key in the index is fatal";
    throws_ok { $dict->element(undef) }
        qr/Invalid element: \[undef\] isn't Text/,
        "asking for a Null key is fatal";
    throws_ok { $dict->element(\"cow") }
        qr/Invalid element: SCALAR.* isn't Text/,



( run in 0.702 second using v1.01-cache-2.11-cpan-13bb782fe5a )