use strict;
use HTML::Tiny;
use Test::More tests => 51;
ok my $h = HTML::Tiny->new, 'Create succeeded';
ok my $h_html = HTML::Tiny->new( mode => 'html' ),
'Create succeeded (mode HTML)';
common_checks( $h );
common_checks( $h_html, ' (mode HTML)' );
# Differences between the two output modes.
is $h->closed( 'br' ), '
', 'simple closed OK';
is $h_html->closed( 'br' ), '
', 'simple closed OK (mode HTML)';
is $h->closed( 'input', { type => 'checkbox', checked => [] } ),
'', 'Empty attr OK';
is $h_html->closed( 'input', { type => 'checkbox', checked => [] } ),
'', 'Empty attr OK (mode HTML)';
sub common_checks {
my $h = shift;
my $mode = shift || '';
# No attributes
is $h->open( 'b' ), '', 'simple open OK' . $mode;
is $h->close( 'b' ), '', 'simple close OK' . $mode;
# Tag options
is $h->tag( 'b', '' ), '', 'simple tag OK' . $mode;
is $h->tag( 'b', 'a', 'b' ), 'ab',
'multi tag OK' . $mode;
is $h->tag( 'b', [ 'a', 'b' ] ), 'ab',
'grouped tag OK' . $mode;
is $h->tag( 'p', $h->tag( 'b', 'a', 'b' ) ),
'
a
b
', 'nested multi tag OK' . $mode; is $h->tag( 'p', $h->tag( 'b', [ 'a', 'b' ] ) ), 'ab
', 'nested grouped tag OK' . $mode; # Attributes is $h->open( 'p', { class => 'normal' } ), '', 'simple attr OK' . $mode; is $h->open( 'p', { class => 'normal', style => undef } ), '
', 'skip undef attr OK' . $mode; is $h->tag( 'p', { class => 'small' }, 'a', 'b' ), '
a
b
', 'multi w/ attr OK' . $mode; is $h->tag( 'p', { class => 'small' }, 'a', { class => undef }, 'b' ), 'a
b
', 'change attr OK' . $mode; } # Stringification package T::Obj; sub new { bless {}, shift } sub as_string { 'an object' } package T::Obj2; sub new { bless {}, shift } package main; my $obj = T::Obj->new; is $h->tag( 'p', $obj ), 'an object
', 'stringification OK'; my $obj2 = T::Obj2->new; like $h->tag( 'p', $obj2 ), '/T::Obj2=.+?
/', 'non as_string OK'; # Only hashes allowed eval { $h->closed( { src => 'spork' }, 'Text here' ); }; like $@, '/Attributes\s+must\s+be\s+passed\s+as\s+hash\s+references/', 'error on non-hash OK'; # URL encoding, decoding is $h->url_encode( '