9 use_ok qw(SL::Template::Simple);
 
  11 my $t = SL::Template::Simple->new(form => {});
 
  14   my ($form, $template_array, $query, $result, $text) = @_;
 
  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;
 
  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';
 
  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';
 
  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';
 
  37 { package LXOTestDummy; sub b { 5 } }
 
  38 my $o = bless [], 'LXOTestDummy';
 
  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';
 
  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';
 
  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';