File: regressions.t

package info (click to toggle)
libdata-dpath-perl 0.60-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: perl: 3,737; makefile: 2
file content (71 lines) | stat: -rwxr-xr-x 2,564 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Deep;
use Data::DPath 'dpath';
use Data::Dumper;

# local $Data::DPath::DEBUG = 1;

use feature 'say';

my $data = {
    aList => [qw/aa bb cc dd ee ff gg hh ii jj/],
    aHash => {
        apple  => 'pie',
        banana => 'split',
        potato => [qw(baked chips fries fish&chips mashed)],
    },
};

my $strange_data = {
    aList => [qw/aa bb cc dd ee ff gg hh ii jj/],
    aHash => {
        apple0 => "pie",
        apple1 => "apple pie",
        apple2 => "apple\npie",
        banana => 'split',
        potato => [qw(baked chips fries fish&chips mashed)],
    },
};

my $res = [ dpath('//*[ value =~ /i/ ]')->match($data) ];
my $expected = [ qw/split pie ii chips fries fish&chips/ ];
unlike ($data->{aHash}, qr/i/, "RT-68882 - aHash does not match the regex");
cmp_bag($res, $expected, "RT-68882 - elements with letter 'i' but not aHash");
# diag "res      = ".Dumper($res);
# diag "expected = ".Dumper($expected);

local $Data::DPath::USE_SAFE;
$res = [ dpath('//*[ value =~ /i/ ]')->match($data) ];
unlike ($data->{aHash}, qr/i/, "RT-68882 - aHash does not match the regex - again without Safe.pm");
cmp_bag($res, $expected, "RT-68882 - elements with letter 'i' but not aHash - again without Safe.pm");

# To clarify confusion I once had here:
#  $data is not found with '//*' because
#  the '*' always gets a *sub* element and
#  therefore can never be the root element.
$res = [ dpath('//*[ Scalar::Util::reftype(value) eq "HASH" ]')->match($data) ];
$expected = [ $data->{aHash} ];
cmp_bag($res, $expected, "RT-68882 related - value filter function still works for hash");

$res = [ dpath('//*[ Scalar::Util::reftype(value) eq "ARRAY" ]')->match($data) ];
$expected = [ $data->{aList}, $data->{aHash}{potato} ];
cmp_bag($res, $expected, "RT-68882 related - value filter function still works for array");

# github #24 - filter expressions with newlines
$res = [ dpath('/aHash/apple0[ value eq "pie" ]')->match($strange_data) ];
$expected = [ $strange_data->{aHash}{apple0} ];
cmp_bag($res, $expected, "github 24 - filter expressions with newlines");
$res = [ dpath('/aHash/apple1[ value eq "apple pie" ]')->match($strange_data) ];
$expected = [ $strange_data->{aHash}{apple1} ];
cmp_bag($res, $expected, "github 24 - filter expressions with newlines");
$res = [ dpath('/aHash/apple2[ value eq "apple
pie" ]')->match($strange_data) ];
$expected = [ $strange_data->{aHash}{apple2} ];
cmp_bag($res, $expected, "github 24 - filter expressions with newlines");

done_testing;