File: encode_tests.t

package info (click to toggle)
libxml-smart-perl 1.78-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 784 kB
  • sloc: perl: 3,644; makefile: 2
file content (140 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (3)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use strict                ;
use warnings              ;

use Test::More            ;

use XML::Smart            ;

use open ':encoding(utf8)';

our $directory = 'data_for_tests';

opendir ( my $_data_dir, $directory) or die $!;

while (my $file = readdir( $_data_dir ) ) {

    next if( $file eq '.'    ) ;
    next if( $file eq '..'   ) ;
    next if( $file =~ '.svn' ) ;
    next if( $file =~ /^bug/ ) ;

    subtest "Testing inclusion of data into xml $file" => sub {
    	_test_data_from( $file ) ;
    	done_testing()           ;
    };

    subtest "Testing embed of data into xml $file"     => sub {
	_test_embed_from( $file ) ;
	done_testing()           ;
    }
}

done_testing() ;


####################################################################################################
##                                      Helper Functions                                          ##
####################################################################################################


sub _test_embed_from { 

    my $infile = shift ;
    
    $infile    = $directory . "/" . $infile    ;

    open( my $_infile, $infile ) or die ( $! ) ;
    my @in_data = <$_infile>            ;
    my $in_data = join( "", @in_data )  ;
    close( $_infile )                          ;

    my $xml_input = '
<subject_list>
        <subject>' . $in_data . '
</subject>
</subject_list>
';

    my $xml_obj = new XML::Smart( $xml_input ) ;

    my $data = $xml_obj->data(
	'decode'    => 1 ,
	'noheader'  => 1 ,
	) ;

    $data =~ s/\n//gs;
    $data =~ s/\s+/ /gs;

    $xml_input =~ s/\n//gs;
    $xml_input =~ s/\s+/ /gs;
    
    cmp_ok( $data, 'eq', $xml_input ) ;

}


sub _test_data_from { 


    my $infile = shift ;
    
    $infile    = $directory . "/" . $infile    ;

    open( my $_infile, $infile ) or die ( $! ) ;
    my @in_data = <$_infile>            ;
    my $in_data = join( "", @in_data )  ;
    close( $_infile )                          ;


    my @utf8_array = ( $in_data, "GOOD DATA" ) ;
    my $utf8       = \@utf8_array              ;

    my $response_xml                        ;
    my $xml_obj         = XML::Smart->new() ;


    eval{
	$xml_obj->{ response }{ data } = $utf8 ;
    };
    if( $@ ){
	fail( "Hash Creation Error $@" ) ;
    }
    eval{
	$response_xml = $xml_obj->data()       ;
    };
    if( $@ ){
	fail( "XML Creation Error $@" )  ;
    }


    my $post_xml_data    = _get_data_from_xml( $response_xml )                    ;
    my $post_xml_in_data = $post_xml_data->{ response }{ data }->[0]->{ CONTENT } ;

    cmp_ok( $post_xml_in_data, 'eq', $in_data ) ;

    return 1 ;

}

sub _get_data_from_xml {
    
    my $xml = shift ;
    
    
    my $data_obj  ;
    
    eval {
        $data_obj = XML::Smart->new( $xml );
    };
    
    if( $@ ) {
        $xml =~ s/[^[:print:]]+//g         ;
        $data_obj = XML::Smart->new( $xml );
    }
    
    my $data_hash = $data_obj->tree();
    
    return $data_hash;
    
}