From c337c4041b557cec7e74654ccd503016ee3a4348 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Fri, 26 Dec 2014 18:16:58 +0200 Subject: [PATCH] Refactor tests (second pass) --- t/Arsenal.pl | 4 - t/Arsenal.pm | 70 -------------- t/SelectData.pm | 22 ----- t/SimpleClass.pm | 29 ------ t/content_handler.t | 15 --- t/crunch.t | 6 -- t/data/3dig.dat | 5 - t/data/4dig.dat | 10 -- t/data/table2.pm | 37 -------- t/defmap.t | 6 -- t/dual_iter.t | 25 ----- t/fillinform.t | 5 - t/hashmap.t | 10 -- t/highlander.t | 21 ----- t/highlander2.t | 34 ------- t/html/position.html | 14 +++ t/html/prune.exp | 4 + t/html/prune.html | 13 +++ t/iter.t | 7 -- t/iter2.t | 31 ------- t/m/SelectData.pm | 22 ----- t/m/SimpleClass.pm | 29 ------ t/misc.t | 208 ++++++++++++++++++++++++++++++++++++++++++ t/newchild.t | 10 -- t/passover.t | 6 -- t/position.t | 25 ----- t/prune.t | 30 ------ t/replace_content.t | 16 ---- t/set_child_content.t | 15 --- t/sibdex.t | 15 --- t/siblings.t | 19 ---- t/table-alt.t | 22 ----- t/table.t | 22 ----- t/table2-table_ld.t | 43 --------- t/table2-tr_ld.t | 54 ----------- t/table2.t | 34 ------- t/tables.t | 179 ++++++++++++++++++++++++++++++++++++ t/unroll_select.t | 17 ---- t/wrap_content.t | 18 ---- 39 files changed, 418 insertions(+), 734 deletions(-) delete mode 100644 t/Arsenal.pl delete mode 100644 t/Arsenal.pm delete mode 100644 t/SelectData.pm delete mode 100644 t/SimpleClass.pm delete mode 100644 t/content_handler.t delete mode 100644 t/crunch.t delete mode 100644 t/data/3dig.dat delete mode 100644 t/data/4dig.dat delete mode 100644 t/data/table2.pm delete mode 100644 t/defmap.t delete mode 100644 t/dual_iter.t delete mode 100644 t/fillinform.t delete mode 100644 t/hashmap.t delete mode 100644 t/highlander.t delete mode 100644 t/highlander2.t create mode 100644 t/html/position.html create mode 100644 t/html/prune.exp create mode 100644 t/html/prune.html delete mode 100644 t/iter.t delete mode 100644 t/iter2.t delete mode 100644 t/m/SelectData.pm delete mode 100644 t/m/SimpleClass.pm create mode 100644 t/misc.t delete mode 100644 t/newchild.t delete mode 100644 t/passover.t delete mode 100644 t/position.t delete mode 100644 t/prune.t delete mode 100644 t/replace_content.t delete mode 100644 t/set_child_content.t delete mode 100644 t/sibdex.t delete mode 100644 t/siblings.t delete mode 100644 t/table-alt.t delete mode 100644 t/table.t delete mode 100644 t/table2-table_ld.t delete mode 100644 t/table2-tr_ld.t delete mode 100644 t/table2.t create mode 100644 t/tables.t delete mode 100644 t/unroll_select.t delete mode 100644 t/wrap_content.t diff --git a/t/Arsenal.pl b/t/Arsenal.pl deleted file mode 100644 index eb18749..0000000 --- a/t/Arsenal.pl +++ /dev/null @@ -1,4 +0,0 @@ -use lib '.'; -use Arsenal; - -Arsenal->new->load_data; diff --git a/t/Arsenal.pm b/t/Arsenal.pm deleted file mode 100644 index 62a45fe..0000000 --- a/t/Arsenal.pm +++ /dev/null @@ -1,70 +0,0 @@ -package Arsenal; - -use strict; -use Data::Dumper; - -my %player; - -sub new { - my $this = shift; - bless {}, ref($this) || $this; -} - -my $number = qr!\d{1,2}!; -my $name = qr!\w+(?:\s\w{2,}){1,2}!; -my $position = qr!\w!; -my $height = qr!\d[-]\d{1,2}!; -my $weight = qr!\d{2,3}!; -my $birthday = qr!\w{3}\s\d{1,2},\s\d{4}!; -my $birthplace= qr!\w+(?:\s\w+)*(?:,\s\S+)*!; - -sub load_data { - my @data; - - while () { - last if /__END__/; - - @player{qw(number name pos height weight birthday birthplace)} - = - m! - ($number)\s+ - ($name)\s+ - ($position)\s+ - ($height)\s+ - ($weight)\s+ - ($birthday)\s+ - ($birthplace) - !x; - - warn $_; - warn Dumper \%player; - } -} - -1; -__DATA__ -24 Manuel Almunia G 6-3 190 May 19, 1977 Pamplona, Spain - 10 Dennis Bergkamp F 6-0 172 May 10, 1969 Amsterdam, Netherlands - 23 Sol Campbell D 6-2 201 Sep 18, 1974 Newham, England - 22 Gael Clichy D 5-11 159 Jul 26, 1985 Clichy, France - 3 Ashley Cole D 5-7 148 Dec 20, 1980 Stepney, England - 18 Pascal Cygan D 6-4 192 Apr 19, 1974 Lens, France - 27 Emmanuel Eboue D 5-10 159 Jun 4, 1983 Abidjan, Cote d'Ivoire - 15 Francesc Fabregas Soler M 5-7 152 Apr 4, 1987 Arenys del Mar, Spain - 16 Mathieu Flamini M 5-10 148 Mar 7, 1984 Marseille, France - 40 Ryan Garry D 6-2 181 Sep 29, 1983 Hornchurch, England - 14 Thierry Henry F 6-2 179 Aug 17, 1977 Paris, France - 13 Aliksandr Hleb M 6-1 154 May 1, 1981 Minsk, USSR - 12 Lauren D 5-11 157 Jan 19, 1977 Londi Kribi, Cameroon - 1 Jens Lehmann G 6-3 192 Nov 10, 1969 Essen, West Germany - 8 Fredrik Ljungberg M 5-9 165 Apr 16, 1977 Halmstads, Sweden - 26 Quincy Owusu Abeyie F 5-11 163 Apr 15, 1986 Amsterdam, Netherlands - 7 Robert Pires M 6-1 163 Oct 29, 1973 Reims, France - 21 Mart Poom G 6-4 187 Feb 3, 1972 Tallinn, USSR - 9 Jose Antonio Reyes F 6-0 181 Sep 1, 1983 Utrera, Spain - 20 Philippe Senderos D 6-3 185 Feb 14, 1985 Geneva, Switzerland - 19 Gilberto Silva M 6-3 172 Oct 7, 1976 Belo Horizonte, Brazil - 17 Alexandre Song M 6-0 168 Apr 9, 1987 Cameroon - 28 Kolo Toure D 6-0 168 Mar 19, 1981 Sokoura Bouake, Cote d'Ivoire - 11 Robin van Persie F 6-0 157 Aug 6, 1983 Rotterdam, Netherlands -__END__ diff --git a/t/SelectData.pm b/t/SelectData.pm deleted file mode 100644 index 65ce175..0000000 --- a/t/SelectData.pm +++ /dev/null @@ -1,22 +0,0 @@ -package SelectData; - - - -my @clan_data = ( - { clan_name => 'janglers', clan_id => 12, selected => 1 }, - { clan_name => 'thugknights', clan_id => 14 }, - { clan_name => 'cavaliers' , clan_id => 13 } - ); - - -sub new { - my $this = shift; - bless {}, ref($this) || $this; -} - -sub load_data { - \@clan_data -} - - -1; diff --git a/t/SimpleClass.pm b/t/SimpleClass.pm deleted file mode 100644 index d0f4a75..0000000 --- a/t/SimpleClass.pm +++ /dev/null @@ -1,29 +0,0 @@ -package SimpleClass; - - -my @name = qw(bob bill brian babette bobo bix); -my @age = qw(99 12 44 52 12 43); -my @weight = qw(99 52 80 124 120 230); - - -sub new { - my $this = shift; - bless {}, ref($this) || $this; -} - -sub load_data { - my @data; - - for (0 .. 5) { - push @data, { - age => shift @age, - name => shift @name, - weight => shift @weight - } - } - - \@data -} - - -1; diff --git a/t/content_handler.t b/t/content_handler.t deleted file mode 100644 index 5a2b203..0000000 --- a/t/content_handler.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $t1 = HTML::Element->new_from_lol( - ['html', - ['head', - [ 'title', 'I like stuff!' ]], - ['body', {id => 'corpus'}, {'lang', 'en-JP'}, - 'stuff', - ['p', 'um, p < 4!', {'class' => 'par123'}], - ['div', {foo => 'bar'}, '123'], # at 0.1.2 - ['div', {jack => 'olantern'}, '456']]]); # at 0.1.2 - -$t1->content_handler(corpus => 'all gone!'); -isxml $t1, \'I like stuff!all gone!', 'content_handler'; diff --git a/t/crunch.t b/t/crunch.t deleted file mode 100644 index 241cc3f..0000000 --- a/t/crunch.t +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/crunch.html'; -$tree->crunch(look_down => [ class => 'imageElement' ], leave => 1); -isxml $tree, 't/html/crunch.exp', 'crunch'; diff --git a/t/data/3dig.dat b/t/data/3dig.dat deleted file mode 100644 index 439cfca..0000000 --- a/t/data/3dig.dat +++ /dev/null @@ -1,5 +0,0 @@ -437,MS-DOS,United States,0,1,1,1,1 -708,Arabic (ASMO 708),0,1,0,0,1 -709,Arabic (ASMO 449+ BCON V4),0,1,0,0,1 -710,Arabic (Transparent Arabic),0,1,0,0,1 -720,Arabic (Transparent ASMO),0,1,0,0,1 \ No newline at end of file diff --git a/t/data/4dig.dat b/t/data/4dig.dat deleted file mode 100644 index 030affd..0000000 --- a/t/data/4dig.dat +++ /dev/null @@ -1,10 +0,0 @@ -1200,Unicode (BMP of ISO 10646),0,0,1,1,2 -1250,Windows 3.1 Eastern European,1,0,1,1,1 -1251,Windows 3.1 Cyrillic,1,0,1,1,1 -1252,Windows 3.1 US (ANSI),1,0,1,1,1 -1253,Windows 3.1 Greek,1,0,1,1,1 -1254,Windows 3.1 Turkish,1,0,1,1,1 -1255,Hebrew,1,0,0,0,1 -1256,Arabic,1,0,0,0,1 -1257,Baltic,1,0,0,0,1 -1361,Korean (Johab),1,0,0,3,1 \ No newline at end of file diff --git a/t/data/table2.pm b/t/data/table2.pm deleted file mode 100644 index 7b5eaf5..0000000 --- a/t/data/table2.pm +++ /dev/null @@ -1,37 +0,0 @@ -package data::table2; - -use strict; -use warnings; - -use Cwd; -use Data::Dumper; - -#warn __PACKAGE__ . ' cwd - ' . getcwd() ; - -sub new { - my $this = shift; - bless {}, ref($this) || $this; -} - -sub load_data { - - my @file = qw(4dig 3dig); - - my %data; - - for my $file (@file) { - my $f = "t/data/$file.dat"; - my @data; - open F, $f or die "couldnt open $f: $!"; - while () { - push @data, [ split ',', $_ ] ; - } - $data{$file} = \@data; - } - #warn Dumper \%data; - \%data; - -} - -1; - diff --git a/t/defmap.t b/t/defmap.t deleted file mode 100644 index 880b998..0000000 --- a/t/defmap.t +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/defmap.html'; -$tree->defmap(smap => {pause => 'arsenal rules'}, $ENV{TEST_VERBOSE}); -isxml $tree, 't/html/defmap.exp', 'defmap'; diff --git a/t/dual_iter.t b/t/dual_iter.t deleted file mode 100644 index 2c93fd4..0000000 --- a/t/dual_iter.t +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/dual_iter.html'; - -$tree->iter2( - wrapper_data => [ - ['the pros' => 'never have to worry about service again'], - ['the cons' => 'upfront extra charge on purchase'], - ['our choice' => 'go with the extended service plan'] - ], - wrapper_proc => sub { - my ($container) = @_; - # only keep the last 2 dts and dds - my @content_list = $container->content_list; - $container->splice_content(0, @content_list - 2); - }, - splice => sub { - my ($container, @item_elems) = @_; - $container->unshift_content(@item_elems); - }, - debug => $ENV{TEST_VERBOSE}, -); - -isxml $tree, 't/html/dual_iter.exp', 'dual_iter'; diff --git a/t/fillinform.t b/t/fillinform.t deleted file mode 100644 index 1d88b2a..0000000 --- a/t/fillinform.t +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/fillinform.html'; -isxml \($tree->fillinform({state => 'catatonic'})), 't/html/fillinform.exp', 'fillinform'; diff --git a/t/hashmap.t b/t/hashmap.t deleted file mode 100644 index ed81f06..0000000 --- a/t/hashmap.t +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/hashmap.html'; -$tree->hash_map( - hash => {people_id => 888, phone => '444-4444', email => 'm@xml.com'}, - to_attr => 'sid', - excluding => ['email'] -); -isxml $tree, 't/html/hashmap.exp', 'hash_map'; diff --git a/t/highlander.t b/t/highlander.t deleted file mode 100644 index c224988..0000000 --- a/t/highlander.t +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 3; - -sub test { - my $age = shift; - my $tree = mktree 't/html/highlander.html'; - - $tree->highlander( - age_dialog => [ - under10 => sub { $_[0] < 10 }, - under18 => sub { $_[0] < 18 }, - welcome => sub { 1 } - ], - $age - ); - - local $_; # XML::Parser does not like read-only $_ (RT #101129) - isxml $tree, "t/html/highlander-$age.exp", "highlander for $age"; -} - -test $_ for qw(5 15 50); diff --git a/t/highlander2.t b/t/highlander2.t deleted file mode 100644 index dd8252d..0000000 --- a/t/highlander2.t +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 3; - -sub replace_age { - my ($branch, $age) = @_; - $branch->look_down(id => 'age')->replace_content($age); -} - -sub test { - my $age = shift; - my $tree = mktree 't/html/highlander2.html'; - my $if_then = $tree->look_down(id => 'age_dialog')->highlander2( - cond => [ - under10 => [ - sub { $_[0] < 10 }, - \&replace_age - ], - under18 => [ - sub { $_[0] < 18 }, - \&replace_age - ], - welcome => [ - sub { 1 }, - \&replace_age - ] - ], - cond_arg => [ $age ] - ); - - local $_; # XML::Parser does not like read-only $_ (RT #101129) - isxml ($tree, "t/html/highlander2-$age.exp", "highlander2 for age $age"); -} - -test $_ for qw(5 15 27); diff --git a/t/html/position.html b/t/html/position.html new file mode 100644 index 0000000..a3fc9a1 --- /dev/null +++ b/t/html/position.html @@ -0,0 +1,14 @@ + + + + + + + + + +
a a a a +
a a a a +
+ + diff --git a/t/html/prune.exp b/t/html/prune.exp new file mode 100644 index 0000000..85471a5 --- /dev/null +++ b/t/html/prune.exp @@ -0,0 +1,4 @@ + + +
There was man named Jed
He did not have a head
He lived beneath a sled
Now he's afraid of Fred...
+ diff --git a/t/html/prune.html b/t/html/prune.html new file mode 100644 index 0000000..25b1fae --- /dev/null +++ b/t/html/prune.html @@ -0,0 +1,13 @@ + + + + + +
There was man named Jed
+
He did not have a head
+
He lived beneath a sled
+
Now he's afraid of Fred...
+
+
+ + diff --git a/t/iter.t b/t/iter.t deleted file mode 100644 index 6e86b44..0000000 --- a/t/iter.t +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/iter.html'; -my $li = $tree->look_down(class => 'store_items'); -$tree->iter($li, qw/bread butter vodka/); -isxml $tree, 't/html/iter.exp', 'iter'; diff --git a/t/iter2.t b/t/iter2.t deleted file mode 100644 index 5d48877..0000000 --- a/t/iter2.t +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/iter2.html'; - -$tree->iter2( - # default wrapper_ld ok - wrapper_data => [ - [ Programmer => 'one who likes Perl and Seamstress' ], - [ DBA => 'one who does business as' ], - [ Admin => 'one who plays Tetris all day' ] - ], - wrapper_proc => sub { - my ($container) = @_; - - # only keep the last 2 dts and dds - my @content_list = $container->content_list; - $container->splice_content(0, @content_list - 2); - }, - # default item_ld is k00l - # default item_data is phrEsh - # default item_proc will do w0rk - splice => sub { - my ($container, @item_elems) = @_; - $container->unshift_content(@item_elems); - }, - - debug => $ENV{TEST_VERBOSE}, -); - -isxml $tree, 't/html/iter2.exp', 'iter2'; diff --git a/t/m/SelectData.pm b/t/m/SelectData.pm deleted file mode 100644 index 65ce175..0000000 --- a/t/m/SelectData.pm +++ /dev/null @@ -1,22 +0,0 @@ -package SelectData; - - - -my @clan_data = ( - { clan_name => 'janglers', clan_id => 12, selected => 1 }, - { clan_name => 'thugknights', clan_id => 14 }, - { clan_name => 'cavaliers' , clan_id => 13 } - ); - - -sub new { - my $this = shift; - bless {}, ref($this) || $this; -} - -sub load_data { - \@clan_data -} - - -1; diff --git a/t/m/SimpleClass.pm b/t/m/SimpleClass.pm deleted file mode 100644 index d0f4a75..0000000 --- a/t/m/SimpleClass.pm +++ /dev/null @@ -1,29 +0,0 @@ -package SimpleClass; - - -my @name = qw(bob bill brian babette bobo bix); -my @age = qw(99 12 44 52 12 43); -my @weight = qw(99 52 80 124 120 230); - - -sub new { - my $this = shift; - bless {}, ref($this) || $this; -} - -sub load_data { - my @data; - - for (0 .. 5) { - push @data, { - age => shift @age, - name => shift @name, - weight => shift @weight - } - } - - \@data -} - - -1; diff --git a/t/misc.t b/t/misc.t new file mode 100644 index 0000000..51fb284 --- /dev/null +++ b/t/misc.t @@ -0,0 +1,208 @@ +#!/usr/bin/perl -T +use lib '.'; +use t::lib tests => 26; + +################################################## +# Short tests based on mklol + +sub mklol { + HTML::Element->new_from_lol( + ['html', + ['head', + [ 'title', 'I like stuff!' ]], + ['body', {id => 'corpus'}, {'lang', 'en-JP'}, + 'stuff', + ['p', 'um, p < 4!', {'class' => 'par123'}], + ['div', {foo => 'bar'}, '123'], + ['div', {jack => 'olantern'}, '456']]]); +} + +my $tree_replaced = \'I like stuff!all gone!'; +my $tree; + +$tree = mklol; +$tree->content_handler(corpus => 'all gone!'); +isxml $tree, $tree_replaced, 'content_handler'; + +$tree = mklol; +$tree->set_child_content(id => 'corpus', 'all gone!'); +isxml $tree, $tree_replaced, 'set_child_content'; + +$tree = mklol; +$tree->look_down('_tag' => 'body')->replace_content('all gone!'); +isxml $tree, $tree_replaced, 'replace_content'; + +$tree = mklol; +my $p = $tree->look_down('_tag' => 'body')->look_down(_tag => 'p'); +is $p->sibdex, 1, 'p tag has 1 as its index'; + +$tree = mklol; +my $div = $tree->look_down('_tag' => 'body')->look_down(_tag => 'p'); +my @sibs = $div->siblings; +is $sibs[0], 'stuff', "first sibling is simple text"; +is $sibs[2]->tag, 'div', "3rd tag is a div tag"; +is scalar @sibs, 4, "4 siblings total"; + +$tree = mklol; +my $bold = HTML::Element->new('b', id => 'wrapper'); +my $w = $tree->look_down(_tag => 'p'); +$w->wrap_content($bold); +isxml $w, \'

um, p < 4!

', 'wrap_content'; + +################################################## +# Short tests + +$tree = mktree 't/html/crunch.html'; +$tree->crunch(look_down => [ class => 'imageElement' ], leave => 1); +isxml $tree, 't/html/crunch.exp', 'crunch'; + +$tree = mktree 't/html/defmap.html'; +$tree->defmap(smap => {pause => 'arsenal rules'}, $ENV{TEST_VERBOSE}); +isxml $tree, 't/html/defmap.exp', 'defmap'; + +$tree = mktree 't/html/fillinform.html'; +isxml \($tree->fillinform({state => 'catatonic'})), 't/html/fillinform.exp', 'fillinform'; + +$tree = mktree 't/html/hashmap.html'; +$tree->hash_map( + hash => {people_id => 888, phone => '444-4444', email => 'm@xml.com'}, + to_attr => 'sid', + excluding => ['email'] +); +isxml $tree, 't/html/hashmap.exp', 'hash_map'; + +$tree = mktree 't/html/iter.html'; +my $li = $tree->look_down(class => 'store_items'); +$tree->iter($li, qw/bread butter vodka/); +isxml $tree, 't/html/iter.exp', 'iter'; + +my @list = map { [item => $_] } qw/bread butter beans/; +my $initial_lol = [ note => [ list => [ item => 'sample' ] ] ]; +my ($new_lol) = HTML::Element::newchild($initial_lol, list => @list); +my $expected = [note => [list => [item => 'bread'], [item => 'butter'], [item => 'beans']]]; +is_deeply $new_lol, $expected, 'newchild unrolling'; + +$tree = mktree 't/html/highlander2.html'; +$tree->passover('under18'); +isxml $tree, 't/html/highlander2-passover.exp', 'passover'; + +$tree = mktree 't/html/position.html'; +my $found = $tree->look_down(id => 'findme'); +my $pos = join ' ', $found->position; +is $pos, '-1 1 0 1 2', 'position'; + +$tree = mktree 't/html/prune.html'; +$tree->prune; +isxml $tree, 't/html/prune.exp', 'prune'; + +################################################## +# Longer tests + +$tree = mktree 't/html/dual_iter.html'; + +$tree->iter2( + wrapper_data => [ + ['the pros' => 'never have to worry about service again'], + ['the cons' => 'upfront extra charge on purchase'], + ['our choice' => 'go with the extended service plan'] + ], + wrapper_proc => sub { + my ($container) = @_; + # only keep the last 2 dts and dds + my @content_list = $container->content_list; + $container->splice_content(0, @content_list - 2); + }, + splice => sub { + my ($container, @item_elems) = @_; + $container->unshift_content(@item_elems); + }, + debug => $ENV{TEST_VERBOSE}, +); + +isxml $tree, 't/html/dual_iter.exp', 'dual_iter'; + +### + +for my $age (qw/5 15 50/) { + $tree = mktree 't/html/highlander.html'; + $tree->highlander( + age_dialog => [ + under10 => sub { $_[0] < 10 }, + under18 => sub { $_[0] < 18 }, + welcome => sub { 1 } + ], + $age + ); + isxml $tree, "t/html/highlander-$age.exp", "highlander for $age"; +} + +### + +sub replace_age { + my ($branch, $age) = @_; + $branch->look_down(id => 'age')->replace_content($age); +} + +for my $age (qw/5 15 27/) { + $tree = mktree 't/html/highlander2.html'; + my $if_then = $tree->look_down(id => 'age_dialog')->highlander2( + cond => [ + under10 => [ sub { $_[0] < 10 }, \&replace_age ], + under18 => [ sub { $_[0] < 18 }, \&replace_age ], + welcome => [ sub { 1 }, \&replace_age ] + ], + cond_arg => [ $age ] + ); + + isxml ($tree, "t/html/highlander2-$age.exp", "highlander2 for age $age"); +} + +### + +$tree = mktree 't/html/iter2.html'; + +$tree->iter2( + # default wrapper_ld ok + wrapper_data => [ + [ Programmer => 'one who likes Perl and Seamstress' ], + [ DBA => 'one who does business as' ], + [ Admin => 'one who plays Tetris all day' ] + ], + wrapper_proc => sub { + my ($container) = @_; + + # only keep the last 2 dts and dds + my @content_list = $container->content_list; + $container->splice_content(0, @content_list - 2); + }, + # default item_ld is k00l + # default item_data is phrEsh + # default item_proc will do w0rk + splice => sub { + my ($container, @item_elems) = @_; + $container->unshift_content(@item_elems); + }, + + debug => $ENV{TEST_VERBOSE}, +); + +isxml $tree, 't/html/iter2.exp', 'iter2'; + +### + +my @data = ( + { clan_name => 'janglers', clan_id => 12, selected => 1 }, + { clan_name => 'thugknights', clan_id => 14 }, + { clan_name => 'cavaliers' , clan_id => 13 } +); +$tree = mktree 't/html/unroll_select.html'; + +$tree->unroll_select( + select_label => 'clan_list', + option_value => sub { my $row = shift; $row->{clan_id} }, + option_content => sub { my $row = shift; $row->{clan_name} }, + option_selected => sub { my $row = shift; $row->{selected} }, + data => \@data, + data_iter => sub { my $data = shift; shift @$data }); + +isxml $tree, 't/html/unroll_select.exp', 'unroll_select'; diff --git a/t/newchild.t b/t/newchild.t deleted file mode 100644 index edeff5e..0000000 --- a/t/newchild.t +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/perl -T -use lib '.'; -use t::lib tests => 1; - -my @list = map { [item => $_] } qw/bread butter beans/; -my $initial_lol = [ note => [ list => [ item => 'sample' ] ] ]; -my ($new_lol) = HTML::Element::newchild($initial_lol, list => @list); - -my $expected = [note => [list => [item => 'bread'], [item => 'butter'], [item => 'beans']]]; -Test::More::is_deeply($new_lol, $expected, 'test unrolling'); diff --git a/t/passover.t b/t/passover.t deleted file mode 100644 index 2a0a901..0000000 --- a/t/passover.t +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $tree = mktree 't/html/highlander2.html'; -$tree->passover('under18'); -isxml $tree, 't/html/highlander2-passover.exp', 'passover'; diff --git a/t/position.t b/t/position.t deleted file mode 100644 index 93dbfb5..0000000 --- a/t/position.t +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $html =<<'EOHTML'; - - - - - - - - - -
a a a a -
a a a a -
- - -EOHTML - -my $t1 = HTML::TreeBuilder->new_from_content ($html) ; -my $found = $t1->look_down(id => 'findme'); - -my $pos = join ' ', $found->position; -is $pos, '-1 1 0 1 2'; diff --git a/t/prune.t b/t/prune.t deleted file mode 100644 index 654e656..0000000 --- a/t/prune.t +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/perl -T -use lib '.'; -use t::lib tests => 1; - -my $root = HTML::TreeBuilder->new_from_content(<<'EOHTML'); - - - - - -
There was man named Jed
-
He did not have a head
-
He lived beneath a sled
-
Now he's afraid of Fred...
-
-
- - -EOHTML - -$root->prune; - -my $expected = ' - - -
There was man named Jed
He did not have a head
He lived beneath a sled
Now he's afraid of Fred...
- -'; - -isxml($root, \$expected, 'prune'); diff --git a/t/replace_content.t b/t/replace_content.t deleted file mode 100644 index 4723593..0000000 --- a/t/replace_content.t +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $t1 = HTML::Element->new_from_lol( - ['html', - ['head', - [ 'title', 'I like stuff!' ]], - ['body', {id => 'corpus'}, {'lang', 'en-JP'}, - 'stuff', - ['p', 'um, p < 4!', {'class' => 'par123'}], - ['div', {foo => 'bar'}, '123'], # at 0.1.2 - ['div', {jack => 'olantern'}, '456']]]); # at 0.1.2 - -$t1->look_down('_tag' => 'body')->replace_content('all gone!'); - -isxml $t1, \'I like stuff!all gone!', 'replace_content'; diff --git a/t/set_child_content.t b/t/set_child_content.t deleted file mode 100644 index ce8a18c..0000000 --- a/t/set_child_content.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $t1 = HTML::Element->new_from_lol( - ['html', - ['head', - [ 'title', 'I like stuff!' ]], - ['body', {id => 'corpus'}, {'lang', 'en-JP'}, - 'stuff', - ['p', 'um, p < 4!', {'class' => 'par123'}], - ['div', {foo => 'bar'}, '123'], # at 0.1.2 - ['div', {jack => 'olantern'}, '456']]]); # at 0.1.2 - -$t1->set_child_content(id => 'corpus', 'all gone!'); -isxml $t1, \'I like stuff!all gone!', 'set_child_content'; diff --git a/t/sibdex.t b/t/sibdex.t deleted file mode 100644 index 1181e06..0000000 --- a/t/sibdex.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $t1 = HTML::Element->new_from_lol( - ['html', - ['head', - [ 'title', 'I like stuff!' ]], - ['body', {id => 'corpus'}, {'lang', 'en-JP'}, - 'stuff', - ['p', 'um, p < 4!', {'class' => 'par123'}], - ['div', {foo => 'bar'}, '123'], # at 0.1.2 - ['div', {jack => 'olantern'}, '456']]]); # at 0.1.2 - -my $p = $t1->look_down('_tag' => 'body')->look_down(_tag => 'p'); -is $p->sibdex, 1, 'p tag has 1 as its index'; diff --git a/t/siblings.t b/t/siblings.t deleted file mode 100644 index 50b8c2f..0000000 --- a/t/siblings.t +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 3; - -my $t1 = HTML::Element->new_from_lol( - ['html', - ['head', - [ 'title', 'I like stuff!' ]], - ['body', {id => 'corpus'}, {'lang', 'en-JP'}, - 'stuff', - ['p', 'um, p < 4!', {'class' => 'par123'}], - ['div', {foo => 'bar'}, '123'], # at 0.1.2 - ['div', {jack => 'olantern'}, '456']]]); # at 0.1.2 - -my $div = $t1->look_down('_tag' => 'body')->look_down(_tag => 'p'); -my @sibs = $div->siblings; - -is $sibs[0], 'stuff', "first sibling is simple text"; -is $sibs[2]->tag, 'div', "3rd tag is a div tag"; -is scalar @sibs, 4, "4 siblings total"; diff --git a/t/table-alt.t b/t/table-alt.t deleted file mode 100644 index f759da4..0000000 --- a/t/table-alt.t +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; -use lib 't'; -use SimpleClass; - -my $o = SimpleClass->new; -my $tree = mktree 't/html/table-alt.html'; - -$tree->table( - gi_table => 'load_data', - gi_tr => ['iterate1', 'iterate2'], - table_data => $o->load_data, - tr_data => sub { - my ($self, $data) = @_; - shift @{$data}; - }, - td_data => sub { - my ($tr_node, $tr_data) = @_; - $tr_node->content_handler($_ => $tr_data->{$_}) for qw(name age weight) - }); - -isxml $tree, 't/html/table-alt.exp', 'table (alternating)'; diff --git a/t/table.t b/t/table.t deleted file mode 100644 index fe9fbf4..0000000 --- a/t/table.t +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; -use lib 't'; -use SimpleClass; - -my $o = SimpleClass->new; -my $tree = mktree 't/html/table.html'; - -$tree->table( - gi_table => 'load_data', - gi_tr => 'data_row', - table_data => $o->load_data, - tr_data => sub { - my ($self, $data) = @_; - shift(@{$data}) ; - }, - td_data => sub { - my ($tr_node, $tr_data) = @_; - $tr_node->content_handler($_ => $tr_data->{$_}) for qw(name age weight) - }); - -isxml $tree, 't/html/table.exp', 'table'; diff --git a/t/table2-table_ld.t b/t/table2-table_ld.t deleted file mode 100644 index 1a359da..0000000 --- a/t/table2-table_ld.t +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/perl -# Test the 3 possible look_down calls to table2() -# a = default -# b = supplied array ref -# c = supplied code ref -use t::lib tests => 3; -use lib qw(t/ t/m/); -use data::table2; - -my $o = data::table2->new; - -# a - default table_ld - -my $tree = mktree 't/html/table2.html'; - -my $table = HTML::Element::Library::ref_or_ld( - $tree, - ['_tag' => 'table'] -); - -isxml $table, 't/html/table2-table_ld.exp', 'table2 look_down default'; - -# b - arrayref table_ld - -$table = HTML::Element::Library::ref_or_ld( - $tree, - [frame => 'hsides', rules => 'groups'] - ); - -isxml $table, 't/html/table2-table_ld.exp', 'table2 look_down arrayref'; - -# c - coderef table_ld - -$table = HTML::Element::Library::ref_or_ld( - $tree, - sub { - my ($t) = @_; - my $caption = $t->look_down('_tag' => 'caption'); - $caption->parent; - } -); - -isxml $table, 't/html/table2-table_ld.exp', 'table2 look_down coderef'; diff --git a/t/table2-tr_ld.t b/t/table2-tr_ld.t deleted file mode 100644 index c685205..0000000 --- a/t/table2-tr_ld.t +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 3; - -# a - default table_ld - -my $tree = mktree 't/html/table2.html'; - -my @tr = HTML::Element::Library::ref_or_ld( - $tree, - ['_tag' => 'tr'] -); - -is (scalar @tr, 16, 'table2 tr look_down (default)'); - -# b - arrayref tr_ld - -$tree = mktree 't/html/table2-tr_ld-arrayref.html'; - -my $tr = HTML::Element::Library::ref_or_ld( - $tree, - [class => 'findMe'] -); - -isxml $tr, 't/html/table2-tr_ld-arrayref.exp', 'table2 tr look_down (arrayref)'; - -# c - coderef tr_ld -# removes windows listings before returning @tr - -$tree = mktree 't/html/table2.html'; - -@tr = HTML::Element::Library::ref_or_ld( - $tree, - sub { - my ($t) = @_; - my @tr = $t->look_down('_tag' => 'tr'); - my @keep; - for my $tr (@tr) { - - my @td = $tr->look_down ('_tag' => 'td') ; - my $detached; - for my $td (@td) { - if (grep { $_ =~ /Windows/ } $td->content_list) { - $tr->detach; - ++$detached; - last; - } - } - push @keep, $tr unless $detached; - } - @keep; - } -); - -isxml $tree, 't/html/table2-tr_ld-coderef.exp', 'table2 tr look_down (coderef)'; diff --git a/t/table2.t b/t/table2.t deleted file mode 100644 index 3566c63..0000000 --- a/t/table2.t +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; -use lib 't'; -use data::table2; - -my $root = 't/html/table2'; -my $d = data::table2->load_data; -my $tree = mktree 't/html/table2.html'; - -for my $dataset (keys %$d) { - my %tbody = ('4dig' => 0, '3dig' => 1); - $tree->table2 ( - debug => $ENV{TEST_VERBOSE}, - table_data => $d->{$dataset}, - tr_base_id => $dataset, - tr_ld => sub { - my $t = shift; - my $tbody = ($t->look_down('_tag' => 'tbody'))[$tbody{$dataset}]; - my @tbody_child = $tbody->content_list; - $tbody_child[$_]->detach for (1 .. $#tbody_child) ; - $tbody->content_list; - }, - td_proc => sub { - my ($tr, $data) = @_; - my @td = $tr->look_down('_tag' => 'td'); - for my $i (0..$#td) { - # warn $i; - $td[$i]->splice_content(0, 1, $data->[$i]); - } - } - ); -} - -isxml $tree, 't/html/table2.exp', 'table2'; diff --git a/t/tables.t b/t/tables.t new file mode 100644 index 0000000..dcb6793 --- /dev/null +++ b/t/tables.t @@ -0,0 +1,179 @@ +#!/usr/bin/perl -T +use lib '.'; +use t::lib tests => 9; + +my $tree; +sub data () { + [map { +{name => $_->[0], age => $_->[1], weight => $_->[2]} } ( + [qw/bob 99 99/], + [qw/bill 12 52/], + [qw/brian 44 80/], + [qw/babette 52 124/], + [qw/bobo 12 120/], + [qw/bix 43 230/], + )]; +} + +sub data2 () { + { + '3dig' => [ + ['437', 'MS-DOS', 'United States', '0', '1', '1', '1', '1'], + ['708', 'Arabic (ASMO 708)', '0', '1', '0', '0', '1'], + ['709', 'Arabic (ASMO 449+ BCON V4)', '0', '1', '0', '0', '1'], + ['710', 'Arabic (Transparent Arabic)', '0', '1', '0', '0', '1'], + ['720', 'Arabic (Transparent ASMO)', '0', '1', '0', '0', '1']], + '4dig' => [ + ['1200', 'Unicode (BMP of ISO 10646)', '0', '0', '1', '1', '2'], + ['1250', 'Windows 3.1 Eastern European', '1', '0', '1', '1', '1'], + ['1251', 'Windows 3.1 Cyrillic', '1', '0', '1', '1', '1'], + ['1252', 'Windows 3.1 US (ANSI)', '1', '0', '1', '1', '1'], + ['1253', 'Windows 3.1 Greek', '1', '0', '1', '1', '1'], + ['1254', 'Windows 3.1 Turkish', '1', '0', '1', '1', '1'], + ['1255', 'Hebrew', '1', '0', '0', '0', '1'], + ['1256', 'Arabic', '1', '0', '0', '0', '1'], + ['1257', 'Baltic', '1', '0', '0', '0', '1'], + ['1361', 'Korean (Johab)', '1', '0', '0', '3', '1']] + }; +} + +$tree = mktree 't/html/table.html'; + +$tree->table( + gi_table => 'load_data', + gi_tr => 'data_row', + table_data => data, + tr_data => sub { + my ($self, $data) = @_; + shift(@{$data}) ; + }, + td_data => sub { + my ($tr_node, $tr_data) = @_; + $tr_node->content_handler($_ => $tr_data->{$_}) for qw(name age weight) + }); + +isxml $tree, 't/html/table.exp', 'table'; + +### + +$tree = mktree 't/html/table-alt.html'; + +$tree->table( + gi_table => 'load_data', + gi_tr => ['iterate1', 'iterate2'], + table_data => data, + tr_data => sub { + my ($self, $data) = @_; + shift @{$data}; + }, + td_data => sub { + my ($tr_node, $tr_data) = @_; + $tr_node->content_handler($_ => $tr_data->{$_}) for qw(name age weight) + }); + +isxml $tree, 't/html/table-alt.exp', 'table (alternating)'; + +### + +my $d = data2; +$tree = mktree 't/html/table2.html'; + +for my $dataset (keys %$d) { + my %tbody = ('4dig' => 0, '3dig' => 1); + $tree->table2 ( + debug => $ENV{TEST_VERBOSE}, + table_data => $d->{$dataset}, + tr_base_id => $dataset, + tr_ld => sub { + my $t = shift; + my $tbody = ($t->look_down('_tag' => 'tbody'))[$tbody{$dataset}]; + my @tbody_child = $tbody->content_list; + $tbody_child[$_]->detach for (1 .. $#tbody_child) ; + $tbody->content_list; + }, + td_proc => sub { + my ($tr, $data) = @_; + my @td = $tr->look_down('_tag' => 'td'); + for my $i (0..$#td) { + # warn $i; + $td[$i]->splice_content(0, 1, $data->[$i]); + } + } + ); +} + +isxml $tree, 't/html/table2.exp', 'table2'; + +### + +# a - default table_ld +$tree = mktree 't/html/table2.html'; +my $table = HTML::Element::Library::ref_or_ld( + $tree, + ['_tag' => 'table'] +); +isxml $table, 't/html/table2-table_ld.exp', 'table2 look_down default'; + +### + +# b - arrayref table_ld +$table = HTML::Element::Library::ref_or_ld( + $tree, + [frame => 'hsides', rules => 'groups'] +); +isxml $table, 't/html/table2-table_ld.exp', 'table2 look_down arrayref'; + +# c - coderef table_ld +$table = HTML::Element::Library::ref_or_ld( + $tree, + sub { + my ($t) = @_; + my $caption = $t->look_down('_tag' => 'caption'); + $caption->parent; + } +); +isxml $table, 't/html/table2-table_ld.exp', 'table2 look_down coderef'; + +### + +# a - default table_ld +my @tr = HTML::Element::Library::ref_or_ld( + $tree, + ['_tag' => 'tr'] +); +is (scalar @tr, 16, 'table2 tr look_down (default)'); + +# b - coderef tr_ld +# removes windows listings before returning @tr + +HTML::Element::Library::ref_or_ld( + $tree, + sub { + my ($t) = @_; + my @trs = $t->look_down('_tag' => 'tr'); + my @keep; + for my $tr (@trs) { + + my @td = $tr->look_down ('_tag' => 'td') ; + my $detached; + for my $td (@td) { + if (grep { $_ =~ /Windows/ } $td->content_list) { + $tr->detach; + ++$detached; + last; + } + } + push @keep, $tr unless $detached; + } + @keep; + } +); +isxml $tree, 't/html/table2-tr_ld-coderef.exp', 'table2 tr look_down (coderef)'; + +# c - arrayref tr_ld + +$tree = mktree 't/html/table2-tr_ld-arrayref.html'; +my $tr = HTML::Element::Library::ref_or_ld( + $tree, + [class => 'findMe'] +); +isxml $tr, 't/html/table2-tr_ld-arrayref.exp', 'table2 tr look_down (arrayref)'; diff --git a/t/unroll_select.t b/t/unroll_select.t deleted file mode 100644 index d29870a..0000000 --- a/t/unroll_select.t +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; -use lib 't'; - -use SelectData; - -my $tree = mktree 't/html/unroll_select.html'; - -$tree->unroll_select( - select_label => 'clan_list', - option_value => sub { my $row = shift; $row->{clan_id} }, - option_content => sub { my $row = shift; $row->{clan_name} }, - option_selected => sub { my $row = shift; $row->{selected} }, - data => SelectData->load_data, - data_iter => sub { my $data = shift; shift @$data }); - -isxml ($tree, 't/html/unroll_select.exp', 'unroll_select'); diff --git a/t/wrap_content.t b/t/wrap_content.t deleted file mode 100644 index 7c766bc..0000000 --- a/t/wrap_content.t +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/perl -use t::lib tests => 1; - -my $t1 = HTML::Element->new_from_lol( - ['html', - ['head', - [ 'title', 'I like stuff!' ]], - ['body', {id => 'corpus'}, {'lang', 'en-JP'}, - 'stuff', - ['p', ['span', {id => 'wrapme'}, 'um, p < 4!'], {'class' => 'par123'}], - ['div', {foo => 'bar'}, '123'], # at 0.1.2 - ['div', {jack => 'olantern'}, '456']]]); # at 0.1.2 - -my $bold = HTML::Element->new('b', id => 'wrapper'); - -my $w = $t1->look_down('id' => 'wrapme'); -$w->wrap_content($bold); -isxml $w, \'um, p < 4!', 'wrap_content'; -- 2.39.2