From 3445a4bf061bab572f76613def9100c2cf66cfb8 Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Sun, 30 Aug 2009 15:46:21 -0700 Subject: [PATCH] Added some tests to ensure that failures work as expected Also moved the ok() docs first and started filling them out. --- lib/Test/XPath.pm | 26 +++++++++++++++---------- t/simple.t | 48 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/lib/Test/XPath.pm b/lib/Test/XPath.pm index 6b096ff..cb0e213 100644 --- a/lib/Test/XPath.pm +++ b/lib/Test/XPath.pm @@ -209,6 +209,22 @@ L, 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'); @@ -229,16 +245,6 @@ L, 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, diff --git a/t/simple.t b/t/simple.t index 9b96dda..b692533 100644 --- a/t/simple.t +++ b/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; } @@ -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()', +);