]>
Commit | Line | Data |
---|---|---|
67e78ff2 | 1 | # This might look like shell script, but it's actually -*- perl -*- |
2 | use strict; | |
3 | use lib qw(t/ t/m/); | |
4 | ||
5 | ||
6 | use File::Slurp; | |
7 | use Test::More qw(no_plan); | |
8 | ||
9 | use TestUtils; | |
10 | use HTML::TreeBuilder; | |
11 | use HTML::Element::Library; | |
12 | ||
13 | use SimpleClass; | |
14 | ||
15 | my $root = 't/html/table-alt'; | |
16 | my $o = SimpleClass->new; | |
17 | my $tree = HTML::TreeBuilder->new_from_file("$root.html"); | |
18 | ||
19 | ||
20 | $tree->table | |
21 | ( | |
22 | # tell seamstress where to find the table, via the method call | |
23 | # ->look_down('id', $gi_table). Seamstress detaches the table from the | |
24 | # HTML tree automatically if no table rows can be built | |
25 | ||
26 | gi_table => 'load_data', | |
27 | ||
28 | # tell seamstress where to find the tr. This is a bit useless as | |
29 | # the <tr> usually can be found as the first child of the parent | |
30 | ||
31 | gi_tr => ['iterate1', 'iterate2'], | |
32 | ||
33 | # the model data to be pushed into the table | |
34 | ||
35 | table_data => $o->load_data, | |
36 | ||
37 | # the way to take the model data and obtain one row | |
38 | # if the table data were a hashref, we would do: | |
39 | # my $key = (keys %$data)[0]; my $val = $data->{$key}; delete $data->{$key} | |
40 | ||
41 | tr_data => sub { my ($self, $data) = @_; | |
42 | shift(@{$data}) ; | |
43 | }, | |
44 | ||
45 | # the way to take a row of data and fill the <td> tags | |
46 | ||
47 | td_data => sub { my ($tr_node, $tr_data) = @_; | |
48 | $tr_node->content_handler($_ => $tr_data->{$_}) | |
49 | for qw(name age weight) } | |
50 | ||
51 | ); | |
52 | ||
53 | my $generated_html = ptree($tree, "$root.gen"); | |
54 | ||
55 | is ($generated_html, File::Slurp::read_file("$root.exp"), | |
56 | "HTML for non-alternating table"); |