added hash_filter as option when calling to_hash [0.12]
[MARC-Fast] / t / 004_marc-hash_filter.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use blib;
5
6 use Test::More tests => 9;
7 use Data::Dump qw/dump/;
8
9 BEGIN {
10         use_ok( 'MARC::Fast' );
11         use_ok( 'Encode' );
12 }
13
14 my $debug = shift @ARGV;
15
16 my $marc_file = 't/utf8.marc';
17
18 ok(my $marc = MARC::Fast->new(
19         marcdb => $marc_file,
20         hash_filter => sub {
21                 my ($l, $tag) = @_;
22                 $l = Encode::decode( 'utf-8', $l );
23                 $l =~ s/knjiga/briga/;
24                 return $l;
25         },
26 ), "new");
27
28 cmp_ok($marc->count, '==', 1, 'count' );
29
30 ok(my $rec = $marc->fetch(1), "fetch 1");
31 diag dump $rec if $debug;
32
33 ok(my $hash = $marc->to_hash(1), "to_hash 1");
34 diag dump $hash if $debug;
35
36 cmp_ok( $hash->{260}->[0]->{'b'}, 'eq', "\x{160}kolska briga,", 'hash_filter from new' );
37
38 ok($hash = $marc->to_hash(1, hash_filter => sub {
39                 my ($l, $tag) = @_;
40                 $l = Encode::decode( 'utf-8', $l );
41                 $l =~ s/knjiga/zabava/;
42                 return $l;
43 }), "to_hash 1 with hash_filter");
44 diag dump $hash if $debug;
45
46 cmp_ok( $hash->{260}->[0]->{'b'}, 'eq', "\x{160}kolska zabava,", 'hash_filter from to_hash' );