App-RecordStream

 view release on metacpan or  search on metacpan

lib/App/RecordStream/Operation/toprettyprint.pm  view on Meta::CPAN


use App::RecordStream::OutputStream;
use App::RecordStream::Record;

sub init {
  my $this = shift;
  my $args = shift;

  my $limit = undef;
  my $key_groups  = App::RecordStream::KeyGroups->new();
  my $do_not_nest = 0;
  my $spec = {
    "1"         => sub { $limit = 1; },
    "one"       => sub { $limit = 1; },
    "n=i"       => \$limit,
    'keys|k=s'  => sub { $key_groups->add_groups($_[1]); },
    'nonested'  => \$do_not_nest,
    'aligned:s' => \(my $aligned),
  };

  $this->parse_options($args, $spec);

  if ( ! $key_groups->has_any_group() ) {
    $key_groups->add_groups('!.!returnrefs');
  }

  $this->{'LIMIT'}         = $limit;
  $this->{'KEY_GROUPS'}    = $key_groups;
  $this->{'NESTED_OUTPUT'} = not $do_not_nest;
  $this->{'ALIGNED'}       = $aligned =~ /^l(eft)?$/i ? 'left' : 'right'
    if defined $aligned;
};

sub accept_record {
  my $this   = shift;
  my $record = shift;

  my $limit = $this->{'LIMIT'};
  if ( defined($limit) ) {

tests/RecordStream/KeySpec.t  view on Meta::CPAN

use App::RecordStream::Record;

BEGIN { use_ok("App::RecordStream::KeySpec"); }

{
  my $rec = App::RecordStream::Record->new("first_key" => "foo", "second_key" => { "bar" => "biz"}, 0 => "zero");
  my $spec = App::RecordStream::KeySpec->new("first_key");
  is(${$spec->guess_key($rec)}, "foo", "Exact key spec match");

  is(${App::RecordStream::KeySpec::find_key($rec,"first_key")}, "foo", "Exact key spec match");
  is(${App::RecordStream::KeySpec::find_key($rec,"does_not_exist")}, undef, "key doesn't exist");
  is(${App::RecordStream::KeySpec::find_key($rec,"second_key/bar")}, "biz", "nested hash");
  is(${App::RecordStream::KeySpec::find_key($rec,"\@first")}, "foo", "Prefix matching");
  is(${App::RecordStream::KeySpec::find_key($rec,"\@cond/ar")}, "biz", "nested substring matching");
  is(${App::RecordStream::KeySpec::find_key($rec,"0")}, "zero", "number only first level");
  is(${App::RecordStream::KeySpec::find_key($rec,'@0')}, "zero", "number only first level, matching");

}

tests/RecordStream/Record.t  view on Meta::CPAN

  is_deeply($rec32, shift @sorted, "rec32 sorted correctly");

  is_deeply($rec11, shift @sorted, "rec11 sorted correctly");
  is_deeply($rec21, shift @sorted, "rec21 sorted correctly");
  is_deeply($rec31, shift @sorted, "rec31 sorted correctly");
}

{
  my $rec = App::RecordStream::Record->new("first_key" => "foo", "second_key" => { "bar" => "biz"}, 0 => "zero");
  is(${$rec->guess_key_from_spec("first_key")}, "foo", "Exact key spec match");
  is(${$rec->guess_key_from_spec("does_not_exist")}, undef, "key doesn't exist");
  is(${$rec->guess_key_from_spec("second_key/bar")}, "biz", "nested hash");
  is(${$rec->guess_key_from_spec("\@first")}, "foo", "Prefix matching");
  is(${$rec->guess_key_from_spec("\@cond/ar")}, "biz", "nested substring matching");
  is(${$rec->guess_key_from_spec("0")}, "zero", "number only first level");
  is(${$rec->guess_key_from_spec('@0')}, "zero", "number only first level, matching");

  ${$rec->guess_key_from_spec("third_key/0")} = 3;
  ok($rec->{"third_key"}->{"0"} == 3, "Auto vivification of hash");

  ${$rec->guess_key_from_spec("fourth_key/#0")} = 3;

tests/RecordStream/Record.t  view on Meta::CPAN


  is(${$rec4->guess_key_from_spec('biz', 0, 1)}, 'zap', 'Do not throw error on existing key');

  #TODO: write key list test
  is_deeply($rec4->get_key_list_for_spec('@b'), ['biz'], "Keylist returns top level key");

  $rec4->{'roo'}->{'foo'} = [{one=>1}, {two=>2}, {three=>3}];

  is_deeply($rec4->get_key_list_for_spec('@r/f'), [qw(roo foo)], "Key list nesting with hashes");
  is_deeply($rec4->get_key_list_for_spec('@r/f/#1/tw'), ['roo', 'foo', '#1', 'two'], "Key list nesting with arrays");
  is_deeply($rec4->get_key_list_for_spec('not_here'), [], "Key list returns empty array on not present");

  my $rec5 = App::RecordStream::Record->new('foo'=>'bar');
  $rec5->get_key_list_for_spec('not_here');
  is_deeply({$rec5->as_hash()}, {foo => 'bar'}, "get_key_list_for_spec doesn't auto-vivify");
  $rec5->get_key_list_for_spec('not_here/zap');
  is_deeply({$rec5->as_hash()}, {foo => 'bar'}, "get_key_list_for_spec doesn't auto-vivify, nested spec");


  my $rec6 = App::RecordStream::Record->new('foo'=>'bar', zoo=>'zap');
  is_deeply($rec6->get_keys_for_group('!oo!s'), [qw(foo zoo)], "Groups from record");

  my $rec7 = App::RecordStream::Record->new('foo'=>'bar', zoo=>'zap', 'coo'=>'cap');
  is_deeply($rec7->get_keys_for_group('!oo!s'), [qw(foo zoo)], "Groups from record, no re-run");
  is_deeply($rec7->get_keys_for_group('!oo!s', 1), [qw(coo foo zoo)], "Groups from record, with re-run");
  is_deeply($rec7->get_group_values('!oo!s', 1), [qw(cap bar zap)], "Groups from record, with re-run");



( run in 0.323 second using v1.01-cache-2.11-cpan-10033ea8487 )