File: hash.t

package info (click to toggle)
libtemplate-perl 2.24-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,660 kB
  • sloc: perl: 14,518; makefile: 15; sh: 5
file content (148 lines) | stat: -rw-r--r-- 2,969 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
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
#============================================================= -*-perl-*-
#
# t/vmethods/hash.t
#
# Testing hash virtual variable methods.
#
# Written by Andy Wardley <abw@cpan.org>
#
# Copyright (C) 1996-2006 Andy Wardley.  All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id$
#
#========================================================================

use strict;
use warnings;
use lib qw( ./lib ../lib ../../lib );
use Template::Test;

# make sure we're using the Perl stash
$Template::Config::STASH = 'Template::Stash';

my $params = {
    hash      => { a => 'b', c => 'd' },
    uhash     => { tobe => '2b', nottobe => undef },
};

my $tt = Template->new() || die Template->error();
my $tc = $tt->context();

$tc->define_vmethod(hash  => dump => sub {
    my $hash = shift;
    return '{ ' 
        . join(', ', map { "$_ => '$hash->{$_}'" } sort keys %$hash)
        . ' }';
});

test_expect(\*DATA, undef, $params);

__DATA__

#------------------------------------------------------------------------
# hash virtual methods
#------------------------------------------------------------------------

-- test --
-- name hash keys --
[% hash.keys.sort.join(', ') %]
-- expect --
a, c

-- test --
-- name hash values --
[% hash.values.sort.join(', ') %]
-- expect --
b, d

-- test --
-- name hash each --
[% hash.each.sort.join(', ') %]
-- expect --
a, b, c, d

-- test --
-- name hash items --
[% hash.items.sort.join(', ') %]
-- expect --
a, b, c, d

-- test --
-- name hash size --
[% hash.size %]
-- expect --
2

-- test --
[% hash.defined('a') ? 'good' : 'bad' %]
[% hash.a.defined ? 'good' : 'bad' %]
[% hash.defined('x') ? 'bad' : 'good' %]
[% hash.x.defined ? 'bad' : 'good' %]
[% hash.defined ? 'good def' : 'bad def' %]
[% no_such_hash.defined ? 'bad no def' : 'good no def' %]
-- expect --
good
good
good
good
good def
good no def

-- test --
[% uhash.defined('tobe') ? 'good' : 'bad' %]
[% uhash.tobe.defined ? 'good' : 'bad' %]
[% uhash.exists('tobe') ? 'good' : 'bad' %]
[% uhash.defined('nottobe') ? 'bad' : 'good' %]
[% hash.nottobe.defined ? 'bad' : 'good' %]
[% uhash.exists('nottobe') ? 'good' : 'bad' %]
-- expect --
good
good
good
good
good
good

-- test --
-- name hash.pairs --
[% FOREACH pair IN hash.pairs -%]
* [% pair.key %] => [% pair.value %]
[% END %]
-- expect --
* a => b
* c => d

-- test --
-- name hash.list (old style) --
[% FOREACH pair IN hash.list -%]
* [% pair.key %] => [% pair.value %]
[% END %]
-- expect --
* a => b
* c => d



#------------------------------------------------------------------------
# user defined hash virtual methods
#------------------------------------------------------------------------

-- test --
-- name dump hash --
[% product = {
     id = 'abc-123',
     name = 'ABC Widget #123'
     price = 7.99
   };
   product.dump
%]
-- expect --
{ id => 'abc-123', name => 'ABC Widget #123', price => '7.99' }