aboutsummaryrefslogtreecommitdiff
path: root/t/steps/rest-xml.t
blob: 1d9f95629f1701449fe02e0c5397411b9cb5bff6 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/perl 
use utf8;
use strict;
use warnings;
use Path::Class;
use Test::More 'no_plan';
use Test::Differences;
use Encode;
use WebCoso::Resource;
 
BEGIN {use_ok('WebCoso::Step::ReST::ToXml')}
my $step=WebCoso::Step::ReST::ToXml->new();
 
my $srckey='rstdoc';
sub make_res {
    my $resource=WebCoso::Resource->new();
    # serve solo per il path 
    $resource->set_property({filename=>'/tmp/mydoc.rest.txt'},datastream=>undef);
    if (@_==1) { # monolingua 
        $resource->set_property($srckey=>$_[0]);
    }
    else { # multilingua 
        my %rst_doc=@_;
        while (my ($lang,$str)=each %rst_doc) {
            $resource->set_property(
                {language=>$lang},
                $srckey=>$str
            );
        }
    }
 
    return $resource;
}
 
my $resource=make_res(<<'END_REST');
Documento
=========
 
paragrafo àè
END_REST
 
$step->process($resource,'gen');
ok(!defined $resource->get_property('xmldom'),'no action on second pass');
 
$step->process($resource,'meta');
my $dom=$resource->get_property('xmldom');
isa_ok($dom,'XML::LibXML::Document','parsed ok on first pass');
 
is($dom->findvalue('/document/@source'),
   '/tmp/mydoc.rest.txt',
   'source path');
is($dom->findvalue('/document/title'),
   'Documento',
   'title');
is($dom->findvalue('count(/document/paragraph)'),
   1,
   '1 paragraph');
is($dom->findvalue('/document/paragraph'),
   'paragrafo àè',
   'paragraph content (unicode)');
 
$resource=make_res(it=><<'END_REST_IT',en=><<'END_REST_EN');
Documento
=========
 
paragrafo
END_REST_IT
Document
========
 
paragraph
END_REST_EN
 
$step->process($resource,'meta');
$dom=$resource->get_property({language=>'it'},'xmldom');
isa_ok($dom,'XML::LibXML::Document','parsed it');
 
is($dom->findvalue('/document/@source'),
   '/tmp/mydoc.rest.txt',
   'source path');
is($dom->findvalue('/document/title'),
   'Documento',
   'title');
is($dom->findvalue('/document/paragraph'),
   'paragrafo',
   'paragraph content');
 
$dom=$resource->get_property({language=>'en'},'xmldom');
isa_ok($dom,'XML::LibXML::Document','parsed en');
 
is($dom->findvalue('/document/@source'),
   '/tmp/mydoc.rest.txt',
   'source path');
is($dom->findvalue('/document/title'),
   'Document',
   'title');
is($dom->findvalue('/document/paragraph'),
   'paragraph',
   'paragraph content');
 
# test per chiavi arbitrarie
$step=WebCoso::Step::ReST::ToXml->new({from=>'rest',to=>'xml'});
$srckey='rest';
$resource=make_res(<<'END_REST');
testo
END_REST
$step->process($resource,'meta');
$dom=$resource->get_property('xml');
isa_ok($dom,'XML::LibXML::Document','chiavi arbitrarie');
 
# test dei metadati
$step=WebCoso::Step::ReST::ToXml->new();
$srckey='rstdoc';
$resource=make_res(<<'END_REST');
========
 Titolo
========
 
sottotitolo
===========
 
:Authors: - dakkar
          - doraemon
:Version: 12
:Status: draft
:Date: 2006-02-07
:CreationDate: 2006-01-30
:Collections: - uno
              - due
              - tre
 
testo
 
END_REST
$step->process($resource,'meta');
my %meta_fields=(
    author => ['dakkar','doraemon'],
    title => 'Titolo',
    subtitle => 'sottotitolo',
    version => 12,
    status => 'draft',
    date => '2006-02-07',
    creation_date => '2006-01-30',
);
while (my ($f,$v)=each %meta_fields) {
    if (ref $v) {
        is_deeply([sort @{$resource->get_property($f)}],
                  $v,
                  "meta: $f");
    }
    else {
        is($resource->get_property($f),
           $v,
           "meta: $f");
    }
}