File: typemap_default_json.t

package info (click to toggle)
libkiokudb-perl 0.57-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,396 kB
  • sloc: perl: 13,314; makefile: 12
file content (78 lines) | stat: -rw-r--r-- 1,845 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Exception;
use Test::Moose;

use Scalar::Util qw(reftype);

use KiokuDB::TypeMap;
use KiokuDB::TypeMap::Default::JSON;
use KiokuDB::TypeMap::Resolver;

my $t = KiokuDB::TypeMap::Default::JSON->new;

my $tr = KiokuDB::TypeMap::Resolver->new(
    typemap => $t,
);

isa_ok( $tr, "KiokuDB::TypeMap::Resolver" );

foreach my $class ( qw(DateTime DateTime::Duration Path::Class::Entity URI Tie::RefHash Authen::Passphrase JSON::Boolean JSON::PP::Boolean SCALAR) ) {
    my $e = $t->resolve($class);

    does_ok( $e, "KiokuDB::TypeMap::Entry", "entry for $class" );

    my $method = $tr->expand_method($class);

    ok( $method, "compiled" );

    is( reftype($method), "CODE", "expand method" );
}

SKIP: {
    skip "JSON required ($@)", 3 unless eval { require JSON };

    my $json = JSON->new->decode('{ "id": "lala", "data": { "yes": true, "no": false } }');

    {
        package KiokuDB_Test_My::Object;
        use Moose;

        has yes => ( is => "ro", default => sub { JSON::true() } );
        has no  => ( is => "ro", default => sub { JSON::false() } );
    }

    my $obj = KiokuDB_Test_My::Object->new;

    require KiokuDB::Collapser;
    require KiokuDB::LiveObjects;
    require KiokuDB::Backend::Hash;

    my $l = KiokuDB::LiveObjects->new;

    my $c = KiokuDB::Collapser->new(
        backend => KiokuDB::Backend::Hash->new,
        live_objects => $l,
        typemap_resolver => $tr,
    );

    my $s = $l->new_scope;

    my ( $buffer, $id ) = $c->collapse(objects => [ $obj ]);

    my $entry = $buffer->id_to_entry($id);

    # see JSON.pm changelog
    my $boolean_class = $JSON::VERSION < 2.90
        ? "JSON::Boolean"
        : "JSON::PP::Boolean";
    isa_ok( $entry->data->{yes}, $boolean_class, "boolean passed through" );

}


done_testing;