File: citation.t

package info (click to toggle)
libpandoc-elements-perl 0.38-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: perl: 1,630; makefile: 15; sh: 1
file content (77 lines) | stat: -rw-r--r-- 2,246 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
use strict;
use Test::More 0.96; # 0.96 for subtests
use Pandoc::Elements;

use constant STRINGLIKE => 'Pandoc::Document::Citation::Test::Stringlike';

{
    package # no index
        Pandoc::Document::Citation::Test::Stringlike;
    use overload q[""] => sub {  ${$_[0]} }, fallback => 1;
    sub new {
        my($class, $value) = @_;
        return bless \$value => $class;
    }
}

my @accessors = (
    [ hash   => "citationHash" ],
    [ id     => "citationId" ],
    [ mode   => "citationMode" ],
    [ num    => "citationNoteNum" ],
    [ prefix => "citationPrefix" ],
    [ suffix => "citationSuffix" ],
);

my @methods = qw[ new TO_JSON ], map {; @$_ } @accessors;

my $c = citation { 
        id => 'foo', 
        prefix => [ Str "see" ], 
        suffix => [ Str "p.", Space, Str "42" ]
    };

isa_ok $c, 'Pandoc::Document::Citation';

can_ok $c, @methods;

is_deeply $c, {
   citationId => 'foo',
   citationHash => 1,
   citationMode => NormalCitation,
   citationNoteNum => 0,
   citationPrefix => [ Str "see" ],
   citationSuffix => [ Str "p.", Space, Str "42" ],
}, 'structure';

subtest accessors => sub {
    for my $aliases ( @accessors ) {
        my($name, $key) = @$aliases;
        is_deeply $c->$name, $c->{$key}, $name;
        is_deeply $c->$key, $c->{$key}, $key;
        is_deeply $c->$key, $c->$name, "$key/$name";
    }
};

subtest id => sub {
    my $bar = STRINGLIKE->new('bar');
    my $baz = STRINGLIKE->new('baz');
    isa_ok $bar, STRINGLIKE, 'test object';
    my $c = citation { id => $bar };
    is $c->id, 'bar', 'initial value';
    ok !ref($c->id), 'coercion through constructor';
    $c->id($baz);
    is $c->id, 'baz', 'changed value';
    ok !ref($c->id), 'coercion through setter';
};

my $doc = pandoc_json(<<'END_OF_JSON');
{"blocks":[{"t":"Para","c":[{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"NormalCitation"},"citationPrefix":[],"citationId":"foo","citationHash":0}],[{"t":"Str","c":"[@foo]"}]]}]}],"pandoc-api-version":[1,17,5,1],"meta":{}}
END_OF_JSON

subtest 'blessed by document constructor' => sub {
    my $cites = $doc->query( Cite => sub { $_ } );
    isa_ok eval { $cites->[0]->citations->[0] }, 'Pandoc::Document::Citation';
};

done_testing;