File: 01-node.t

package info (click to toggle)
libtext-vcard-perl 3.06-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 352 kB
  • ctags: 110
  • sloc: perl: 1,535; makefile: 2
file content (226 lines) | stat: -rw-r--r-- 6,282 bytes parent folder | download | duplicates (4)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl -w

use strict;

use lib qw(./lib);

use Test::More tests => 41;
use Data::Dumper;

# Check we can load module
BEGIN { use_ok('Text::vCard::Node'); }

local $SIG{__WARN__} = sub { die $_[0] };

#####
# Set up some test data
#####

# ok data
my %data = (
    'params' => [ { 'type' => 'HOME,PREF', }, ],
    'value' =>
        ';;First work address - street;Work city;London;Work PostCode;CountryName',
);

# Address fields
my $fields = [
    'po_box', 'extended',  'street', 'city',
    'region', 'post_code', 'country'
];

#####
# Test new()
#####

my $foo = Text::vCard::Node::new( 'foo::bar', { fields => ['value'] } );

is( ref($foo), 'foo::bar', 'Can use as a base class' );

my $hash
    = Text::vCard::Node::new( { foo => 'bar' }, { 'fields' => ['value'] } );
is( ref($hash), 'HASH', 'new() retruns HASH when supplied hash' );

eval { Text::vCard::Node::new( undef, { 'fields' => ['value'] } ); };
like( $@, qr/Use of uninitialized value/, 'Errors if no class supplied' );
$@ = 'foo';

eval { Text::vCard::Node->new( {} ); };
like( $@, qr/No fields defined/, 'new() carps when no fields supplied' );

eval { Text::vCard::Node->new( { 'fields' => { 'duff' => 'hash' } } ); };
like(
    $@,
    qr/fields is not an array ref/,
    'new() carps when fields is not an array ref'
);

my %too_many_value_data
    = ( 'value' =>
        'asd;Street;Work city;London;Work PostCode;CountryName;more;values',
    );
eval {
    my $duff = Text::vCard::Node->new(
        {   fields => $fields,
            data   => \%too_many_value_data,
        }
    );
};
like(
    $@,
    qr/Data value had 8 elements expecting 7 or less/,
    'new() carp on wrong number of elements in value comp to fields'
);

my %a_few_data_points = ( 'value' => 'x;s;Street;City', );

# Working nodes
my $nod_few_fields = Text::vCard::Node->new(
    {   fields => $fields,
        data   => \%a_few_data_points,
    }
);
is( $nod_few_fields->street(),
    'Street', 'new() - less data than fields, field set ok' );
is( $nod_few_fields->post_code(),
    undef, 'new() - less data, empty field returns undef' );
$nod_few_fields->post_code('postcode');
is( $nod_few_fields->post_code(),
    'postcode', 'new() - less data, set empty field' );

# Create without a node_type - should be fine
my $no = Text::vCard::Node->new(
    {   fields => $fields,
        data   => \%data,
    }
);

# Create without a data - should be fine
my $no_data = Text::vCard::Node->new( { fields => $fields, } );
is( $no_data->street(), undef,
    'Created node with no data and methods created' );

# Create 'working' node
my $node = Text::vCard::Node->new(
    {   node_type => 'address',    # Auto upper cased
        fields    => $fields,
        data      => \%data,
        group     => 'item1',
    }
);

is( $no->street(), $node->street(),
    'new() without node_type still works ok' );
is( $node->group(),       'item1', 'got group as it was set' );
is( $node->group('FooF'), 'foof',  'set node worked' );
###
# ORG
###
my %orgdata = ( 'value' => 'name;unit;extra', );
my $org = Text::vCard::Node->new(
    {   node_type => 'ORG',
        fields    => [ 'name', 'unit' ],
        data      => \%orgdata,
    }
);
is( scalar( @{ $org->unit() } ), 2, 'org - Got two elements back from unit' );
my @new_org = qw(a b c);
is( scalar( @{ $org->unit( \@new_org ) } ),
    3, 'org - Got the elements back from setting unit' );

is( scalar( @{ $org->unit('foo') } ),
    3, 'org - Got the elements back from trying to set unit with string' );

my %single_org = ( 'value' => 'just_name', );
my $org_name = Text::vCard::Node->new(
    {   node_type => 'ORG',
        fields    => [ 'name', 'unit' ],
        data      => \%single_org,
    }
);
is( $org_name->unit(), undef, 'org - copes with unit being empty' );

#####
# types()
#####
my $types = $node->types();
my @types = $node->types();
ok( scalar(@types), 'types() returns stuff' );
ok( eq_array( $types, \@types ), 'types() ok in array or scalar context' );
is( $no_data->types(), undef, 'types() get undef when there are none' );

#####
# is_type()
#####
ok( $node->is_type('home'),  'is_type() home type matches' );
ok( !$node->is_type('work'), 'is_type() not work address type' );
is( $no_data->is_type('work'), undef, 'is_type() undef when no params' );

#####
# is_pref()
#####
ok( $node->is_pref(), 'is_pref() this is a prefered address' );
$node->remove_types('pref');
is( $node->is_pref(),    undef, 'is_pref() get undef when not pref' );
is( $no_data->is_pref(), undef, 'is_pref() get undef if no params' );

#####
# remove_types()
#####
is( $no_data->remove_types('wibble'),
    undef, 'remove_types() when no params - no error' );
is( $node->remove_types('wibble'),
    undef, 'remove_types() undef when scalar, node has params and no match' );
is( $node->remove_types( ['home'] ),
    1, 'remove_types() get a true value in array context when sucess' );

#####
# add_types()
#####
# Test the types
$node->add_types('WoRk');
ok( $node->is_type('wOrk'),
    'is_type() Added work type and check non-cases sensative' );
$node->remove_types( [ 'Work', 'Home' ] );
ok( !$node->is_type('Work'),
    'is_type() Removed work type and check non-cases sensative' );
ok( !$node->is_type('home'), 'is_type() Removed several types' );
$node->add_types( [ 'work', 'home' ] );
ok( $node->is_type('work') && $node->is_type('home'),
    'is_type() Added two types ok' );
$no_data->add_types('work');
ok( $no_data->is_type('work'),
    'is_type() Added type to node with no params' );

#####
# AUTOLOAD
#####

is( $node->po_box(), '', 'AUTOLOAD - Po box empty as expected' );
is( $node->street(),
    'First work address - street',
    'AUTOLOAD - Street address matches'
);
is( $node->country('Moose vill'), 'Moose vill', 'AUTOLOAD - set ok' );

eval { $node->duff_method(); };
like(
    $@,
    qr/duff_method method which is not valid for this node/,
    'AUTOLOAD - carp when method not valid'
);

####
# export_data()
####
my $export
    = ';;First work address - street;Work city;London;Work PostCode;Moose vill';
is( $node->export_data(), $export,
    'export_data() - Node returns expected data' );

delete $node->{'po_box'};
is( $node->export_data(), $export,
    'export_data() - Node returns expected data, with undef entry' );

# Test non-existant methods