Volltext-Suche Auftrag: auch in Wiedevorlagen suchen
[kivitendo-erp.git] / t / helper / template / simple.t
1 #!/usr/bin/perl
2
3 use strict;
4 use lib 't';
5
6 use DateTime;
7 use Test::More;
8
9 use_ok qw(SL::Template::Simple);
10
11 my $t = SL::Template::Simple->new(form => {});
12
13 sub test ($$$$$) {
14   my ($form, $template_array, $query, $result, $text) = @_;
15
16   $t->{form} = bless $form, 'Form';
17   $t->{form}->{TEMPLATE_ARRAYS} = $template_array  if $template_array;
18   is_deeply $t->_get_loop_variable(@$query), $result, $text;
19 }
20
21 test { a => 1 }, {}, [ 'a', 0 ], 1, 'simple access';
22 test { }, { a => [ 1 ] }, [ 'a', 0, 0 ], 1, 'template access';
23 test { }, { a => [ 1..4 ] }, [ 'a', 0, 3 ], 4, 'template access > 1';
24 test { }, { a => [ [ 1 ] ] }, [ 'a', 0, 0, 0 ], 1, 'template access more than one layer';
25 test { }, { a => [ 1 ] }, [ 'a', 0, 3 ], undef, 'short circuit if array is missing';
26 test { a => 2 }, { a => [ 1 ] }, [ 'a', 0 ], 2, 'no template access ignores templates';
27 test { a => 2 }, { a => [ 1 ] }, [ 'a', 1 ], [ 1 ], 'array access returns array';
28
29 test { a => 2, TEMPLATE_ARRAY => [ a => [1] ] }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored';
30 test { a => 2, TEMPLATE_ARRAY => 1 }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored 2';
31
32 test { a => { b => 2 }, 'a.b' => 5 }, {}, [ 'a.b', 0 ], 2, 'dot access';
33 test { a => { b => { c => 5 } } }, {}, [ 'a.b.c', 0 ], 5, 'deep dot access';
34 test { a => { b => 2 } }, {}, [ 'a.b', 1 ], 2, 'dot access ignores array';
35 test { a => { b => 2 } }, { 'a.b' => 3 }, [ 'a.b', 0, 0 ], 2, 'dot access ignores template';
36
37 { package LXOTestDummy; sub b { 5 } }
38 my $o = bless [], 'LXOTestDummy';
39
40 test { 'a.b' => 2, a => $o }, {}, [ 'a.b', 0 ], 5, 'dot object access';
41 test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.b', 0 ], 5, 'deep dot object access';
42 test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.c', 0 ], undef, 'dot hash does not shortcut';
43 test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.c', 0 ], '', 'dot object shortcuts to empty string';
44
45 test {}, { a => [ { b => 2 } ], 'a.b' => 5 },  [ 'a.b', 0, 0 ], 2, 'array dot access';
46 test {}, { a => [ { b => { c => 5 } } ] },  [ 'a.b.c', 0, 0 ], 5, 'array deep dot access';
47 test {}, { a => [ { b => 2 } ] }, [ 'a.b', 1, 0 ], 2, 'array dot access ignores array';
48 test { 'a.b' => 3 }, { a => [ { b => 2 } ] }, , [ 'a.b', 0, 0 ], 2, 'array dot access ignores template';
49
50 test {}, { a => [ $o ] },  [ 'a.b', 0, 0 ], 5, 'array dot object access';
51 test {}, { a => [ { b => $o } ] }, [ 'a.b.b', 0, 0 ], 5, 'array deep dot object access';
52 test {}, { a => [ { b => $o } ] },  [ 'a.c', 0, 0 ], undef, 'array dot hash does not shortcut';
53 test {}, { a => [ { b => $o } ] },  [ 'a.b.c', 0, 0 ], '', 'array dot object shortcuts to empty string';
54
55 done_testing();