App-DPath

 view release on metacpan or  search on metacpan

t/app_dpath.t  view on Meta::CPAN

#! /usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;
use Test::More;
use Test::Deep;
use JSON;
use YAML::Syck;
use Config::General;
use Config::INI::Serializer;
use Data::Structure::Util 'unbless';

sub check {
        my ($intype, $outtype, $path, $expected, $just_diag) = @_;

        $path       ||= '//lines//description[ value =~ m(use Data::DPath) ]/../_children//data//name[ value eq \'Hash two\']/../value';
        $expected   ||= [ "2" ];
        my $program   = "$^X -Ilib bin/dpath";
        #my $unblessed = $outtype eq "json" ? "_unblessed" : "";
        my $infile    = "t/testdata.$intype";
        my $cmd       = "$program -i $intype -o $outtype \"$path\" $infile";
        #diag $cmd;
        my $output    = `$cmd`;

        my $result;
        if ($outtype eq "json")
        {
                $result = JSON::from_json(unbless $output);
        }
        elsif ($outtype eq "yaml") {
                $result = YAML::Syck::Load($output);
        }
        elsif ($outtype eq "cfggeneral") {
                my %data = Config::General->new(-String => $output)->getall;
                $result = \%data;
        }
        elsif ($outtype eq "dumper")
        {
                eval "\$result = my $output";
        }
        elsif ($outtype eq "ini") {
                $result = Config::INI::Serializer->new->deserialize($output);
        }
        if ($just_diag) {
                diag Dumper($result);
        } else {
                cmp_deeply $result, $expected, "$intype - dpath - $outtype";
        }
}

check (qw(tap json));
check (qw(yaml json));
check (qw(yaml dumper));
check (qw(json dumper));
# XML <-> data mapping is somewhat artificial, so another path is needed
check (qw(xml dumper), '//description[ value =~ m(use Data::DPath) ]/../_children//data//Hash two/value');
check (qw(ini dumper), '//description[ value =~ m(use Data::DPath) ]/../number', [ "1" ]);
check (qw(ini json),   '//description[ value =~ m(use Data::DPath) ]/../number', [ "1" ]);
check (qw(ini yaml),   '//description[ value =~ m(use Data::DPath) ]/../number', [ "1" ]);
# Config::INI::Serializer hashifies arrays - array indexes become hashkeys
check (qw(ini ini),    '//description[ value =~ m(use Data::DPath) ]/../number', { '0' => "1" });
check (qw(xml ini),    '//description[ value =~ m(use Data::DPath) ]/../number', { '0' => "1" });
check (qw(ini ini),    '/start_time', { '0' => "1236463400.25151" });



( run in 2.347 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )