Skip to content

Commit

Permalink
Added some tests to ensure that failures work as expected
Browse files Browse the repository at this point in the history
Also moved the ok() docs first and started filling them out.
  • Loading branch information
theory committed Aug 30, 2009
1 parent 2b89620 commit 3445a4b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
26 changes: 16 additions & 10 deletions lib/Test/XPath.pm
Expand Up @@ -209,6 +209,22 @@ L<XML::LibXML::Parser options|XML::LibXML::Parser/"PARSER OPTIONS">, such as
=back
=head3 ok
$xp->ok( '//foo/bar', 'Should have bar element under foo element' );
$xp->ok('/contains(//title, "Welcome")', 'Title should contain "Welcome"');
Test that an XPath expression evaluated against the XML document returns a
true value. If the XPath expression finds no nodes, the result will be false.
If it finds a value, the value must be a true value (in the Perl sense).
$xp->ok('//assets/story', sub {
my $i;
for my $story (@_) {
$story->is('[@id]/text()', ++$i, "ID should be $i in story" );
}
}, 'Should have story elements' );
=head3 is
$xp->is('/html/head/title', 'Welcome');
Expand All @@ -229,16 +245,6 @@ L<XML::LibXML::Parser options|XML::LibXML::Parser/"PARSER OPTIONS">, such as
$xp->cmp_ok()
=head3 ok
$xp->ok( '//foo/bar', 'Should have bar element under foo element' );
$xp->ok( '//assets/story', sub {
my $i;
for my $story (@_) {
$story->is('[@id]/text()', ++$i, "ID should be $i in story" );
}
}, 'Should have story elements' );
=head1 Support
This module is stored in an open GitHub repository,
Expand Down
48 changes: 44 additions & 4 deletions t/simple.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w

use strict;
use Test::Builder::Tester tests => 15;
use Test::Builder::Tester tests => 21;
use Test::More;

BEGIN { use_ok 'Test::XPath' or die; }
Expand Down Expand Up @@ -44,19 +44,59 @@ $xp->like( '/html/head/title', qr{^Hel{2}o$}, 'like should work');
$xp->unlike( '/html/head/title', qr{^Bye$}, 'unlike should work');
$xp->cmp_ok('/html/head/title', 'eq', 'Hello', 'cmp_ok should work');

# Make them fail.
test_out('not ok 1 - is should work');
test_out('not ok 2 - isnt should work');
test_out('not ok 3 - like should work');
test_out('not ok 4 - unlike should work');
test_out('not ok 5 - cmp_ok should work');
$xp->is( '/html/head/title', 'Bye', 'is should work');
$xp->isnt( '/html/head/title', 'Hello', 'isnt should work');
$xp->like( '/html/head/title', qr{^Bye$}, 'like should work');
$xp->unlike( '/html/head/title', qr{^Hel{2}o$}, 'unlike should work');
$xp->cmp_ok('/html/head/title', 'ne', 'Hello', 'cmp_ok should work');
test_test(
skip_err => 1,
title => 'Failures in the simple methods should work',
);

# Try multiples.
$xp->is('/html/body/p', 'firstpost', 'Should work for multiples');

# Try an attribute.
$xp->is('/html/body/p/@class', 'foo', 'Should get attribute value');
$xp->ok('/html/body/p[@class="foo"]', 'Should find by attribute value');

# Try a function.
$xp->is('count(/html/body/p)', 2, 'Should work for functions');

# Try a boolean function.
$xp->ok('boolean(1)', 'Boolean should work');
# Try boolean function.
$xp->ok('boolean(1)', 'boolean(1) should be true');
$xp->ok('true()', 'true() should be true');

# Try a false boolean.
test_out('not ok 1 - false boolean');
$xp->ok('false()', 'false boolean');
test_test( skip_err => 1 );
test_test(
skip_err => 1,
title => 'Boolean true returned by XPath should be true in ok()',
);

# Try a comparison function.
$xp->ok('contains(//title, "Hell")', 'Title should contain "hell"');

# Try a false comparison.
test_out('not ok 1 - heck');
$xp->ok('contains(//title, "Heck")', 'heck');
test_test(
skip_err => 1,
title => 'Boolean false returned by XPath should be false in ok()',
);

# Try a non-existent node.
test_out('not ok 1');
$xp->ok('/foo/baz');
test_test(
skip_err => 1,
title => 'Nonexistent node should be false in ok()',
);

0 comments on commit 3445a4b

Please sign in to comment.