1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 4001;
7
8###############################################################################
9# Read and load configuration file and backend library.
10
11use Config::Tiny ();
12
13my $config_file = 'xt/author/lib.ini';
14my $config = Config::Tiny -> read('xt/author/lib.ini')
15  or die Config::Tiny -> errstr();
16
17# Read the library to test.
18
19our $LIB = $config->{_}->{lib};
20
21die "No library defined in file '$config_file'"
22  unless defined $LIB;
23die "Invalid library name '$LIB' in file '$config_file'"
24  unless $LIB =~ /^[A-Za-z]\w*(::\w+)*\z/;
25
26# Read the reference type(s) the library uses.
27
28our $REF = $config->{_}->{ref};
29
30die "No reference type defined in file '$config_file'"
31  unless defined $REF;
32die "Invalid reference type '$REF' in file '$config_file'"
33  unless $REF =~ /^[A-Za-z]\w*(::\w+)*\z/;
34
35# Load the library.
36
37eval "require $LIB";
38die $@ if $@;
39
40###############################################################################
41
42can_ok($LIB, '_from_bytes');
43
44my @data;
45
46# Add data in data file.
47
48(my $datafile = $0) =~ s/\.t/.dat/;
49open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
50while (<DATAFILE>) {
51    s/\s+\z//;
52    next if /^#/ || ! /\S/;
53    push @data, [ split /:/ ];
54}
55close DATAFILE or die "$datafile: can't close file after reading: $!";
56
57# List context.
58
59for (my $i = 0 ; $i <= $#data ; ++ $i) {
60    my ($in0, $out0) = @{ $data[$i] };
61
62    my ($x, @got);
63
64    my $test = qq|\@got = $LIB->_from_bytes("$in0");|;
65
66    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
67
68    eval $test;
69    is($@, "", "'$test' gives emtpy \$\@");
70
71    subtest "_from_bytes() in list context: $test", sub {
72        plan tests => 4,
73
74        cmp_ok(scalar @got, '==', 1,
75               "'$test' gives one output arg");
76
77        is(ref($got[0]), $REF,
78           "'$test' output arg is a $REF");
79
80        is($LIB->_check($got[0]), 0,
81           "'$test' output is valid");
82
83        is($LIB->_str($got[0]), $out0,
84           "'$test' output arg has the right value");
85    };
86}
87
88# Scalar context.
89
90for (my $i = 0 ; $i <= $#data ; ++ $i) {
91    my ($in0, $out0) = @{ $data[$i] };
92
93    my ($x, $got);
94
95    my $test = qq|\$got = $LIB->_from_bytes("$in0");|;
96
97    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
98
99    eval $test;
100    is($@, "", "'$test' gives emtpy \$\@");
101
102    subtest "_from_bytes() in scalar context: $test", sub {
103        plan tests => 3,
104
105        is(ref($got), $REF,
106           "'$test' output arg is a $REF");
107
108        is($LIB->_check($got), 0,
109           "'$test' output is valid");
110
111        is($LIB->_str($got), $out0,
112           "'$test' output arg has the right value");
113    };
114}
115