1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 20205;
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
42my $scalar_util_ok = eval { require Scalar::Util; };
43Scalar::Util -> import('refaddr') if $scalar_util_ok;
44
45diag "Skipping some tests since Scalar::Util is not installed."
46  unless $scalar_util_ok;
47
48can_ok($LIB, '_lcm');
49
50my @data;
51
52# Add data in data file.
53
54(my $datafile = $0) =~ s/\.t/.dat/;
55open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
56while (<DATAFILE>) {
57    s/\s+\z//;
58    next if /^#/ || ! /\S/;
59    push @data, [ split /:/ ];
60}
61close DATAFILE or die "$datafile: can't close file after reading: $!";
62
63# List context.
64
65for (my $i = 0 ; $i <= $#data ; ++ $i) {
66    my ($in0, $in1, $out0) = @{ $data[$i] };
67
68    my ($x, $y, @got);
69
70    my $test = qq|\$x = $LIB->_new("$in0"); |
71             . qq|\$y = $LIB->_new("$in1"); |
72             . qq|\@got = $LIB->_lcm(\$x, \$y);|;
73
74    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
75
76    eval $test;
77    is($@, "", "'$test' gives emtpy \$\@");
78
79    subtest "_lcm() in list context: $test", sub {
80        plan tests => 9;
81
82        cmp_ok(scalar @got, "==", 1,
83               "'$test' gives one output arg");
84
85        is(ref($got[0]), $REF,
86           "'$test' output arg is a $REF");
87
88        is($LIB->_check($got[0]), 0,
89           "'$test' output is valid");
90
91        is($LIB->_str($got[0]), $out0,
92           "'$test' output arg has the right value");
93
94      SKIP: {
95            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
96
97            isnt(refaddr($got[0]), refaddr($y),
98                 "'$test' output arg is not the second input arg");
99        }
100
101        is(ref($x), $REF,
102           "'$test' first input arg is still a $REF");
103
104        my $strx = $LIB->_str($x);
105        ok($strx eq $out0 || $strx eq $in0,
106           "'$test' first input arg has the right value")
107          or diag("       got: $strx\n", "  expected: ",
108                  $out0 eq $in0 ? $out0 : "$out0 or $in0");
109
110        is(ref($y), $REF,
111           "'$test' second input arg is still a $REF");
112
113        is($LIB->_str($y), $in1,
114           "'$test' second input arg is unmodified");
115    };
116}
117
118# Scalar context.
119
120for (my $i = 0 ; $i <= $#data ; ++ $i) {
121    my ($in0, $in1, $out0) = @{ $data[$i] };
122
123    my ($got);
124    my ($x, $y);
125    my ($xo, $yo);
126
127    my $test = qq|\$x = $LIB->_new("$in0"); |
128             . qq|\$y = $LIB->_new("$in1"); |
129             . qq|\$got = $LIB->_lcm(\$x, \$y);|;
130
131    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
132
133    eval $test;
134    is($@, "", "'$test' gives emtpy \$\@");
135
136    subtest "_lcm() in scalar context: $test", sub {
137        plan tests => 8;
138
139        is(ref($got), $REF,
140           "'$test' output arg is a $REF");
141
142        is($LIB->_check($got), 0,
143           "'$test' output is valid");
144
145        is($LIB->_str($got), $out0,
146           "'$test' output arg has the right value");
147
148      SKIP: {
149            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
150
151            isnt(refaddr($got), refaddr($y),
152                 "'$test' output arg is not the second input arg");
153        }
154
155        is(ref($x), $REF,
156           "'$test' first input arg is still a $REF");
157
158        my $strx = $LIB->_str($x);
159        ok($strx eq $out0 || $strx eq $in0,
160           "'$test' first input arg has the right value")
161          or diag("       got: $strx\n", "  expected: ",
162                  $out0 eq $in0 ? $out0 : "$out0 or $in0");
163
164        is(ref($y), $REF,
165           "'$test' second input arg is still a $REF");
166
167        is($LIB->_str($y), $in1,
168           "'$test' second input arg is unmodified");
169    };
170}
171