Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

reset state between Loader parse calls so we can re-use the loader

  • Loading branch information...
commit 812ffd5f8a25029c5a576d7562bfa577ec8716d6 1 parent 5f7b6be
@perigrin authored
View
3  lib/XML/Toolkit/Loader.pm
@@ -22,6 +22,9 @@ has parser => (
handles => [qw(parse_uri parse_file parse_string)]
);
+before [qw(parse_uri parse_file parse_string)] =>
+ sub { shift->filter->reset_state };
+
sub render {
my ($self) = @_;
$self->render_object( $self->root_object );
View
31 lib/XML/Toolkit/Loader/Filter.pm
@@ -6,15 +6,18 @@ extends qw(XML::Filter::Moose);
with qw(XML::Toolkit::Builder::ClassRegistry);
has objects => (
- isa => 'ArrayRef',
- is => 'ro',
- auto_deref => 1,
- lazy_build => 1,
- traits => ['Array'],
- handles => {
- 'pop_object' => ['pop'],
- 'add_object' => ['push'],
- 'objects_count' => ['count'],
+ isa => 'ArrayRef',
+ is => 'ro',
+ traits => ['Array'],
+ lazy => 1,
+ builder => '_build_objects',
+ clearer => 'reset_state',
+ handles => {
+ pop_object => ['pop'],
+ add_object => ['push'],
+ objects_count => ['count'],
+ current_object => [ 'get', -1 ],
+ root_object => [ 'get', 0 ],
}
);
@@ -30,16 +33,6 @@ sub parent_object {
return $self->objects->[-1];
}
-sub current_object {
- my ($self) = @_;
- $self->objects->[-1];
-}
-
-sub root_object {
- my ($self) = @_;
- $self->objects->[0];
-}
-
sub at_root_object { $_[0]->current_object == $_[0]->root_object }
sub load_class {
View
50 t/06.reuse_loader.t
@@ -0,0 +1,50 @@
+#!/usr/bin/perl -w
+use strict;
+use Test::More;
+use Test::Exception;
+use Test::XML;
+
+use aliased 'XML::Toolkit::Config::Container' => 'XMLTK::App';
+
+use aliased 'XML::Toolkit::Generator';
+
+my $xml = <<'END_XML';
+<?xml version="1.0"?>
+<note>
+ <to>Tove</to>
+ <from>Jani</from>
+ <heading>Reminder</heading>
+ <body>Don't forget me this weekend!</body>
+</note>
+END_XML
+
+my $xml2 = <<'END_XML';
+<note>
+ <to>Jani</to>
+ <from>Tove</from>
+ <heading>Re: Reminder</heading>
+ <body>Bite me.</body>
+</note>
+END_XML
+
+my $app = XMLTK::App->new( xmlns => { '' => 'MyTest' } );
+my $builder = $app->builder;
+$builder->parse_string($xml);
+eval $builder->render();
+if ($@) {
+ diag "Couldn't EVAL code $@";
+ done_testing;
+ exit;
+}
+
+ok( my $loader = XMLTK::App->new( xmlns => { '' => 'MyTest' } )->loader,
+ 'Build XML::Toolkit::Loader' );
+lives_ok { $loader->parse_string($xml) } 'loader parsed';
+ok( my $root = $loader->root_object, 'extract root object' );
+
+lives_ok { $loader->parse_string($xml2), 'parse new xml' } 'loader parsed again';
+ok( my $root2 = $loader->root_object, 'extract second root object' );
+
+ok($root != $root2, 'root and root2 are different');
+
+done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.