XML-Twig
view release on metacpan or search on metacpan
t/test_errors.t view on Meta::CPAN
}
}
{ my $t= XML::Twig->new->parse( '<doc/>');
eval { XML::Twig->parse( twig_handlers => { q{foo[@a="$sd"]} => sub { } }, "<foo/>"); };
matches( $@, "^wrong handler condition", 'perl syntax in attribute value');
}
{ my $t= XML::Twig->new->parse( '<doc><field/></doc>');
eval { $t->root->set_field( '*[2]'); };
matches( $@, "can't create a field name from", 'set_field');
}
{ my $t= XML::Twig->new( twig_handlers => { erase => sub { $_->parent->erase } });
eval { $t->parse( '<doc><p><erase>toto</erase></p></doc>'); };
matches( $@, "trying to erase an element before it has been completely parsed", 'erase current element');
}
{ my $t= XML::Twig->new->parse( '<doc><erase><e1/><e2/></erase></doc>');
my $e= $t->first_elt( 'erase')->cut;
eval { $e->erase };
matches( $@, "can only erase an element with no parent if it has a single child", 'erase cut element');
$e->paste( $t->root);
eval { $e->paste( first_child => $t->root); };
matches( $@, "cannot paste an element that belongs to a tree", 'paste uncut element');
$e->cut;
eval { $e->paste( $t->root => 'first_child' ); };
matches( $@, "wrong argument order in paste, should be", 'paste uncut element');
eval { $e->paste( first_child => {} ); };
matches( $@, "wrong target type in paste: 'HASH', should be XML::Twig::Elt", 'paste with wrong ref');
eval { $e->paste( 'first_child' ); };
matches( $@, "missing target in paste", 'paste with no target');
eval { $e->paste( 'first_child', 1 ); };
matches( $@, 'wrong target type in paste \(not a reference\)', 'paste with no ref');
eval { $e->paste( 'first_child', bless( {}, 'foo') ); };
matches( $@, "wrong target type in paste: 'foo'", 'paste with wrong object type');
eval { $e->paste( wrong => $t->root ); };
matches( $@, "tried to paste in wrong position 'wrong'", 'paste in wrong position');
eval { $e->paste( before => $t->root); };
matches( $@, "cannot paste before root", 'paste before root');
eval { $e->paste( after => $t->root); };
matches( $@, "cannot paste after root", 'paste after root');
eval { $e->paste_before( $t->root); };
matches( $@, "cannot paste before root", 'paste before root');
eval { $e->paste_after( $t->root); };
matches( $@, "cannot paste after root", 'paste after root');
}
{ my $t= XML::Twig->new->parse( '<doc><p>text1</p><p>text2</p></doc>');
my $p1= $t->root->first_child( 'p');
my $p2= $t->root->first_child( 'p[2]');
eval { $p1->merge_text( 'toto'); } ;
matches( $@, "invalid merge: can only merge 2 elements", 'merge elt and string');
eval { $p1->merge_text( $p2); } ;
matches( $@, "invalid merge: can only merge 2 text elements", 'merge non text elts');
$p1->first_child->merge_text( $p2->first_child);
is( $t->sprint, '<doc><p>text1text2</p><p></p></doc>', 'merge_text');
my $p3= XML::Twig::Elt->new( '#CDATA' => 'foo');
eval { $p1->first_child->merge_text( $p3); };
matches( $@, "invalid merge: can only merge 2 text elements", 'merge cdata and pcdata elts');
}
{ my $t= XML::Twig->new;
$t->save_global_state;
eval { $t->set_pretty_print( 'foo'); };
matches( $@, "invalid pretty print style 'foo'", 'invalid pretty_print style');
eval { $t->set_pretty_print( 987); };
matches( $@, "invalid pretty print style 987", 'invalid pretty_print style');
eval { $t->set_empty_tag_style( 'foo'); };
matches( $@, "invalid empty tag style 'foo'", 'invalid empty_tag style');
eval { $t->set_empty_tag_style( '987'); };
matches( $@, "invalid empty tag style 987", 'invalid empty_tag style');
eval { $t->set_quote( 'foo'); };
matches( $@, "invalid quote 'foo'", 'invalid quote style');
eval { $t->set_output_filter( 'foo'); };
matches( $@, "invalid output filter 'foo'", 'invalid output filter style');
eval { $t->set_output_text_filter( 'foo'); };
matches( $@, "invalid output text filter 'foo'", 'invalid output text filter style');
}
{ my $t= XML::Twig->new->parse( '<doc/>');
my @methods= qw( depth in_element within_element context current_line current_column current_byte
recognized_string original_string xpcroak xpcarp xml_escape base current_element
element_index position_in_context
);
my $method;
foreach $method ( @methods)
{ eval "\$t->$method";
matches( $@, "calling $method after parsing is finished", $method);
}
$SIG{__WARN__}= $init_warn;
}
{ my $t= XML::Twig->new->parse( '<doc><elt/></doc>');
my $elt= $t->root->first_child( 'elt')->cut;
foreach my $pos ( qw( before after))
{ eval { $elt->paste( $pos => $t->root); };
matches( $@, "cannot paste $pos root", "paste( $pos => root)");
}
}
{ my $t= XML::Twig->new->parse( '<doc><a><f1>f1</f1><f2>f2</f2></a></doc>');
eval { $t->root->simplify( group_tags => { a => 'f1' }); };
matches( $@, "error in grouped tag a", "grouped tag error f1");
eval { $t->root->simplify( group_tags => { a => 'f2' }); };
matches( $@, "error in grouped tag a", "grouped tag error f2");
eval { $t->root->simplify( group_tags => { a => 'f3' }); };
matches( $@, "error in grouped tag a", "grouped tag error f3");
}
{ eval { XML::Twig::Elt->parse( '<e>foo</e>')->subs_text( "foo", '&elt( 0/0)'); };
matches( $@, "(invalid replacement expression |Illegal division by zero)", "invalid replacement expression in subs_text");
}
{ eval { my $t=XML::Twig->new( twig_handlers => { e => sub { $_[0]->parse( "<doc/>") } });
$t->parse( "<d><e/></d>");
};
matches( $@, "cannot reuse a twig that is already parsing", "error re-using a twig during parsing");
}
( run in 1.174 second using v1.01-cache-2.11-cpan-39bf76dae61 )