-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- xml helper now uses named arguments - added tests for xml helper
- Loading branch information
Showing
4 changed files
with
189 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#!/usr/bin/perl | ||
package t::unit::GPH::XMLHelper; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Test2::V0 -target => 'GPH::XMLHelper'; | ||
use Test2::Tools::Spec; | ||
use Data::Dumper; | ||
|
||
local $SIG{__WARN__} = sub {}; | ||
|
||
describe "class `$CLASS`" => sub { | ||
tests 'it can be instantiated' => sub { | ||
can_ok($CLASS, 'new'); | ||
}; | ||
|
||
tests "mandatory element options" => sub { | ||
my $object = $CLASS->new(); | ||
|
||
ok(dies {$object->buildElement((attributes => {}))}, 'died with missing name') or note($@); | ||
ok(lives {$object->buildElement((name => 'foo'))}, 'lives with mandatory options') or note($@); | ||
}; | ||
|
||
tests 'object validation' => sub { | ||
my ($object, $exception, $warnings); | ||
$exception = dies { | ||
$warnings = warns { | ||
$object = $CLASS->new(); | ||
}; | ||
}; | ||
|
||
is($exception, undef, 'no exception thrown'); | ||
is($warnings, 0, 'no warnings generated'); | ||
is( | ||
$object, | ||
object { | ||
field dom => object { | ||
prop blessed => 'XML::LibXML::Document'; | ||
}; | ||
}, | ||
'object as expected', | ||
Dumper($object) | ||
); | ||
|
||
}; | ||
}; | ||
|
||
describe 'test element' => sub { | ||
my (%args, $element, $expected_xml); | ||
|
||
case 'name value attributes' => sub { | ||
%args = ( | ||
name => 'foo', | ||
value => 'bar', | ||
attributes => { | ||
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' | ||
} | ||
); | ||
$expected_xml = '<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">bar</foo>'; | ||
}; | ||
|
||
case 'name' => sub { | ||
%args = ( | ||
name => 'foo', | ||
); | ||
$expected_xml = '<foo/>'; | ||
}; | ||
|
||
case 'name value' => sub { | ||
%args = ( | ||
name => 'foo', | ||
value => 'bar', | ||
); | ||
$expected_xml = '<foo>bar</foo>'; | ||
}; | ||
|
||
tests 'element generation' => sub { | ||
my ($object, $exception, $warnings); | ||
$exception = dies { | ||
$warnings = warns { | ||
$object = $CLASS->new(); | ||
$element = $object->buildElement(%args); | ||
}; | ||
}; | ||
|
||
is($exception, undef, 'no exception thrown'); | ||
is($warnings, 0, 'no warnings generated'); | ||
|
||
is( | ||
$element->toString(), | ||
$expected_xml, | ||
'element as expected', | ||
Dumper($element) | ||
); | ||
}; | ||
}; | ||
|
||
describe 'test paren element' => sub { | ||
my ($object, $dom, $element, $exception, $warnings); | ||
|
||
$exception = dies { | ||
$warnings = warns { | ||
$object = $CLASS->new(); | ||
$element = $object->buildElement(( | ||
name => 'foo', | ||
attributes => { | ||
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' | ||
} | ||
)); | ||
|
||
$object->buildElement(( | ||
name => 'bar', | ||
parent => $element, | ||
value => 'baz', | ||
)); | ||
|
||
$dom = $object->getDom(); | ||
$dom->setDocumentElement($element); | ||
}; | ||
}; | ||
|
||
is($exception, undef, 'no exception thrown'); | ||
is($warnings, 0, 'no warnings generated'); | ||
|
||
is( | ||
$dom->toString(), | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><bar>baz</bar></foo> | ||
', | ||
'parent child element as expected', | ||
Dumper($dom->toString()) | ||
) | ||
}; | ||
|
||
done_testing(); | ||
|