1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 276117;
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, '_root');
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->_root(\$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 "_root() 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        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
105           "'$test' first input arg has the correct value");
106
107        is(ref($y), $REF,
108           "'$test' second input arg is still a $REF");
109
110        is($LIB->_str($y), $in1,
111           "'$test' second input arg is unmodified");
112    };
113}
114
115# Scalar context.
116
117for (my $i = 0 ; $i <= $#data ; ++ $i) {
118    my ($in0, $in1, $out0) = @{ $data[$i] };
119
120    my ($x, $y, $got);
121
122    my $test = qq|\$x = $LIB->_new("$in0"); |
123             . qq|\$y = $LIB->_new("$in1"); |
124             . qq|\$got = $LIB->_root(\$x, \$y);|;
125
126    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
127
128    eval $test;
129    is($@, "", "'$test' gives emtpy \$\@");
130
131    subtest "_root() in scalar context: $test", sub {
132        plan tests => 8;
133
134        is(ref($got), $REF,
135           "'$test' output arg is a $REF");
136
137        is($LIB->_check($got), 0,
138           "'$test' output is valid");
139
140        is($LIB->_str($got), $out0,
141           "'$test' output arg has the right value");
142
143      SKIP: {
144            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
145
146            isnt(refaddr($got), refaddr($y),
147                 "'$test' output arg is not the second input arg");
148        }
149
150        is(ref($x), $REF,
151           "'$test' first input arg is still a $REF");
152
153        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
154           "'$test' first input arg has the correct value");
155
156        is(ref($y), $REF,
157           "'$test' second input arg is still a $REF");
158
159        is($LIB->_str($y), $in1,
160           "'$test' second input arg is unmodified");
161    };
162}
163