1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 2385;
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# Load the library.
27
28eval "require $LIB";
29die $@ if $@;
30
31###############################################################################
32
33can_ok($LIB, '_as_hex');
34
35my @data;
36
37# Small numbers.
38
39for (my $x = 0; $x <= 255 ; ++ $x) {
40    push @data, [ $x, sprintf("0x%x", $x) ];
41}
42
43# Add data in data file.
44
45(my $datafile = $0) =~ s/\.t/.dat/;
46open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
47while (<DATAFILE>) {
48    s/\s+\z//;
49    next if /^#/ || ! /\S/;
50    push @data, [ split /:/ ];
51}
52close DATAFILE or die "$datafile: can't close file after reading: $!";
53
54# List context.
55
56for (my $i = 0 ; $i <= $#data ; ++ $i) {
57    my ($in0, $out0) = @{ $data[$i] };
58
59    my ($x, @got);
60
61    my $test = qq|\$x = $LIB->_new("$in0"); |
62             . qq|\@got = $LIB->_as_hex(\$x)|;
63
64    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
65
66    eval $test;
67    is($@, "", "'$test' gives emtpy \$\@");
68
69    subtest "_as_hex() in list context: $test", sub {
70        plan tests => 3,
71
72        cmp_ok(scalar @got, '==', 1,
73               "'$test' gives one output arg");
74
75        is(ref($got[0]), "",
76           "'$test' output arg is a scalar");
77
78        is($got[0], $out0,
79           "'$test' output arg has the right value");
80    };
81}
82
83# Scalar context.
84
85for (my $i = 0 ; $i <= $#data ; ++ $i) {
86    my ($in0, $out0) = @{ $data[$i] };
87
88    my ($x, $got);
89
90    my $test = qq|\$x = $LIB->_new("$in0"); |
91             . qq|\$got = $LIB->_as_hex(\$x)|;
92
93    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
94
95    eval $test;
96    is($@, "", "'$test' gives emtpy \$\@");
97
98    subtest "_as_hex() in scalar context: $test", sub {
99        plan tests => 2,
100
101        is(ref($got), "",
102           "'$test' output arg is a scalar");
103
104        is($got, $out0,
105           "'$test' output arg has the right value");
106    };
107}
108