1#!/usr/bin/perl -T 2 3# This script tests both the NodeList interface and the Perl overload 4# interface of both NodeList classes. 5 6# We also check to see that node lists are updated whenever the document 7# is modified. 8 9use strict; use warnings; 10 11use Test::More tests => 030; 12 13 14# -------------------------# 15# Tests 1-2: load the modules 16 17BEGIN { use_ok 'HTML::DOM::NodeList'; } 18BEGIN { use_ok 'HTML::DOM::NodeList::Magic'; } 19 20# -------------------------# 21# Tests 3-5: constructors 22 23my (@plain,@magic); 24 25# plain node list (linked to an array) 26my $plain = new HTML::DOM::NodeList \@plain; 27isa_ok $plain, 'HTML::DOM::NodeList'; 28# magic node list (caches the list returned by a coderef) 29my $magic = new HTML::DOM::NodeList::Magic sub { @magic }; 30isa_ok $magic, 'HTML::DOM::NodeList::Magic'; 31# Oh look, we have p5.10 roles! 32ok DOES $magic 'HTML::DOM::NodeList', 'magic node list does NodeList'; 33 34@plain = qw'Te hypermachoi strategoi ta niketeria'; 35@magic = qw'Hos lytrotheisa ton deinon eucharisteria'; 36 37# -------------------------# 38# Tests 6-11: access contents 39 40is +(item $plain 2), 'strategoi', 'item'; 41is +(item $magic 3), 'deinon', 'item (magic)'; 42is $$plain[2], 'strategoi', 'overloading'; 43is $$magic[3], 'deinon', 'overloading (magic)'; 44is $plain->length, 5, 'length'; 45is $magic->length, 5, 'length (magic)'; 46 47# -------------------------# 48# Tests 12-19: access contents after modification 49 50splice @plain, 1,1; 51splice @magic, 2,1; 52 53is +(item $magic 3), 'deinon', 'item when magic list is stale'; 54is $magic->length, 5, 'length when magic list is stale'; 55 56$magic->_you_are_stale; 57 58is +(item $plain 2), 'ta', 'item after modification'; 59is +(item $magic 3), 'eucharisteria', 'item after modification (magic)'; 60is $$plain[2], 'ta', 'overloading after modification'; 61is $$magic[3], 'eucharisteria', 'overloading after modification (magic)'; 62is $plain->length, 4, 'length after modification'; 63is $magic->length, 4, 'length after modification (magic)'; 64 65# -------------------------# 66# Test 20: magic node list garbage collection 67 68no warnings 'once'; 69*HTML::DOM::NodeList::Magic::DESTROY = sub { $::bye_bye++ }; 70bless $magic, ref $magic; 71undef $magic; 72ok $::bye_bye, 'make sure the dustbin man does his job'; 73 74# -------------------------# 75# Test 21: second arg to magic node list’s constructor 76 77require HTML::DOM; 78my $doc = new HTML::DOM; $doc->open; 79$magic = new HTML::DOM::NodeList::Magic sub { $doc->childNodes }, $doc; 80$magic->length; # call the sub and populate it 81$doc->appendChild($doc->createElement('br')); 82is $magic->length, 2, 'second arg to magic node list constructor automatically registers the node list with the document'; 83 84# -------------------------# 85# Tests 22-4: make sure doc-modification methods update node lists 86# This did not work for document->write before 0.027. 87 88$doc = new HTML::DOM; 89my $divs = $doc->getElementsByTagName('div'); 90()=@$divs; # force the node list to update itself 91$doc->write("<div><div><div></div></div></div>"); 92$doc->close; 93is @$divs, 3, "node lists are updated by document->write"; 94$doc->elem_handler(script => sub { 95 is @$divs, 2, "node lists are updated before an elem_handler is called"; 96}); 97$doc->write( 98 "<div><div><script></script><div></div><div></div></div></div>" 99); 100is @$divs, 4, 'node lists are updated when doc->write finishes'; 101 102# ~~~ Add tests for node-manipulation methods. 103