Permalink
Browse files

Make CSS patterns match relative to the current node.

This allows them to match from inside the code ref passed to `ok()`.
  • Loading branch information...
1 parent 84d3139 commit b9dbcaa89ee5b3d80fb0190e020e0a0d0fd250e8 @theory committed Jun 29, 2011
Showing with 33 additions and 20 deletions.
  1. +1 −1 Build.PL
  2. +3 −0 Changes
  3. +15 −2 lib/Test/XPath.pm
  4. +14 −17 t/css_selector.t
View
@@ -18,7 +18,7 @@ Module::Build->new(
recommends => {
'Test::Pod' => '1.20',
'Test::Pod::Coverage' => '1.06',
- 'HTML::Selector::XPath' => '0.03',
+ 'HTML::Selector::XPath' => '0.06',
},
meta_merge => {
resources => {
View
@@ -6,6 +6,9 @@ Revision history for Perl extension Test::XPath.
spot.
- Added the `find_value()` method to provide easy access to values in
the document relative to the current node. Suggested by Moritz Onken.
+ - XPaths generated via the `css_selector` filter are now properly
+ created relative to th current node. That means you can use CSS
+ expressions in the recursive block passed to `ok()`.
0.13 2010-06-01T18:39:28
- Added the `filter` option, which allows custom filtering of XPath
View
@@ -27,7 +27,20 @@ sub new {
eval 'require HTML::Selector::XPath';
die 'Please install HTML::Selector::XPath to use CSS selectors'
if $@;
- sub { HTML::Selector::XPath::selector_to_xpath(shift) }
+ sub {
+ my $xpath = do {
+ my $xp = HTML::Selector::XPath->new(shift)->to_xpath(root => '//');
+ if (eval { $_->isa(__PACKAGE__) } && $_->node ne $doc->documentElement) {
+ # Make it relative to the current node.
+ $xp =~ s{^///[*]}{.};
+ } else {
+ # Start from the top.
+ $xp =~ s{^///[*]}{};
+ }
+ $xp;
+ };
+ return $xpath;
+ }
} else {
die "Unknown filter: $f\n";
}
@@ -42,7 +55,7 @@ sub ok {
my ($self, $xpath, $code, $desc) = @_;
my $xpc = $self->{xpc};
my $Test = Test::Builder->new;
- $xpath = $self->{filter}->($xpath);
+ $xpath = $self->{filter}->($xpath, $self);
# Code and desc can be reversed, to support PerlX::MethodCallWithBlock.
($code, $desc) = ($desc, $code) if ref $desc eq 'CODE';
View
@@ -35,22 +35,19 @@ $xp->ok('> html > head > foo', 'whatever');
test_test('ok fail works');
# Try a recursive call.
-SKIP: {
- skip 'http://rt.cpan.org/Ticket/Display.html?id=50131' => 1;
-
- test_out( 'ok 1 - p');
- test_out( 'ok 2 - em');
- test_out( 'ok 3 - b');
- test_out( 'ok 4 - em');
- test_out( 'ok 5 - b');
-
- $xp->ok( 'html > body > p', sub {
- shift->ok('> em', sub {
- $_->ok('> b', 'b');
- }, 'em');
- }, 'p');
- test_test('recursive ok should work');
-}
+test_out( 'ok 1 - p');
+test_out( 'ok 2 - em');
+test_out( 'ok 3 - b');
+test_out( 'ok 4 - em');
+test_out( 'ok 5 - b');
+
+$xp->ok( '> html > body > p', sub {
+ shift->ok('> em', sub {
+ $_->ok('> b', 'b');
+ }, 'em');
+}, 'p');
+test_test('recursive ok should work');
+
# Try is, like, and cmp_ok.
$xp->is( ' > html > head > title', 'Hello', 'is should work');
@@ -96,6 +93,6 @@ test_test('not_ok works');
# Try failed ok.
test_out('not ok 1 - whatever');
-test_err(qq{# Failed test 'whatever'\n# at $file line 100.});
+test_err(qq{# Failed test 'whatever'\n# at $file line 97.});
$xp->not_ok(' > html > head > title', 'whatever');
test_test('not_ok fail works');

0 comments on commit b9dbcaa

Please sign in to comment.