xref: /openbsd/gnu/usr.bin/perl/cpan/bignum/t/overrides.t (revision 09467b48)
1#!perl
2
3use strict;
4use warnings;
5
6# Test behaviour of hex and oct overrides in detail, and also how the three
7# modules interact.
8
9use Test::More tests => 35;
10
11my $hex_called;
12my $oct_called;
13
14# For testing that existing CORE::GLOBAL overrides are not clobbered
15BEGIN {
16    if ($] > 5.009004) {
17        no warnings 'syntax';
18        *CORE::GLOBAL::hex = sub(_) { ++$hex_called; CORE::hex(@_?$_[0]:$_) };
19        *CORE::GLOBAL::oct = sub(_) { ++$oct_called; CORE::oct(@_?$_[0]:$_) };
20    } else {
21        *CORE::GLOBAL::hex = sub(;$) { ++$hex_called; CORE::hex(@_?$_[0]:$_) };
22        *CORE::GLOBAL::oct = sub(;$) { ++$oct_called; CORE::oct(@_?$_[0]:$_) };
23    }
24}
25
26{
27    use bigint;
28    $_ = "20";
29    is hex, "32", 'bigint hex override without arguments infers $_';
30    is oct, "16", 'bigint oct override without arguments infers $_';
31    @_ = 1..20;
32    is hex(@_), "32", 'bigint hex override provides scalar context';
33    is oct(@_), "16", 'bigint oct override provides scalar context';
34  SKIP:
35    {
36        skip "no lexical hex/oct", 2 unless $] > do { no bigint; 5.009004 };
37        is ref hex(1), 'Math::BigInt',
38          'bigint hex() works when bignum and bigrat are loaded';
39        is ref oct(1), 'Math::BigInt',
40          'bigint oct() works when bignum and bigrat are loaded';
41    }
42}
43{
44    use bignum;
45    $_ = "20";
46    is hex, "32", 'bignum hex override without arguments infers $_';
47    is oct, "16", 'bignum oct override without arguments infers $_';
48    @_ = 1..20;
49    is hex(@_), "32", 'bignum hex override provides scalar context';
50    is oct(@_), "16", 'bignum oct override provides scalar context';
51  SKIP:
52    {
53        skip "no lexical hex/oct", 2 unless $] > 5.009004;
54        is ref hex(1), 'Math::BigInt',
55          'bignum hex() works when bigint and bigrat are loaded';
56        is ref oct(1), 'Math::BigInt',
57          'bignum oct() works when bigint and bigrat are loaded';
58    }
59}
60{
61    use bigrat;
62    $_ = "20";
63    is hex, "32", 'bigrat hex override without arguments infers $_';
64    is oct, "16", 'bigrat oct override without arguments infers $_';
65    @_ = 1..20;
66    is hex(@_), "32", 'bigrat hex override provides scalar context';
67    is oct(@_), "16", 'bigrat oct override provides scalar context';
68  SKIP:
69    {
70        skip "no lexical hex/oct", 2 unless $] > 5.009004;
71        is ref hex(1), 'Math::BigInt',
72          'bigrat hex() works when bignum and bigint are loaded';
73        is ref oct(1), 'Math::BigInt',
74          'bigrat oct() works when bignum and bigint are loaded';
75    }
76}
77
78$hex_called = 0;
79() = hex 0;
80is $hex_called, 1, 'existing hex overrides are called';
81$oct_called = 0;
82() = oct 0;
83is $oct_called, 1, 'existing oct overrides are called';
84
85{
86    package _importer;
87    {
88        use bigint 'hex', 'oct';
89        ::is \&hex, \&bigint::hex, 'exported hex function';
90        ::is \&oct, \&bigint::oct, 'exported oct function';
91    }
92    ::ok ref hex(), 'exported hex function returns ref outside pragma scope';
93    ::ok ref oct(), 'exported oct function returns ref outside pragma scope';
94    ::is oct("20"), "16", 'exported oct function works with "decimal"';
95    # (used to return 20 because it thought it was decimal)
96}
97{
98    package _importer2;
99    use bignum 'hex', 'oct';
100    ::is \&hex, \&bignum::hex, 'bignum exports hex';
101    ::is \&oct, \&bignum::oct, 'bignum exports oct';
102    ::is \&hex, \&bigint::hex, 'bignum exports same hex as bigint';
103    ::is \&oct, \&bigint::oct, 'bignum exports same oct as bigint';
104}
105{
106    package _importer3;
107    use bigrat 'hex', 'oct';
108    ::is \&hex, \&bigrat::hex, 'bigrat exports hex';
109    ::is \&oct, \&bigrat::oct, 'bigrat exports oct';
110    ::is \&hex, \&bigint::hex, 'bigrat exports same hex as bigint';
111    ::is \&oct, \&bigint::oct, 'bigrat exports same oct as bigint';
112}
113is ref(hex 0), "", 'hex export is not global';
114is ref(oct 0), "", 'oct export is not global';
115