1# -*- mode: perl; -*-
2#
3# Verify that objectify() is able to convert a "foreign" object into what we
4# want, when what we want is Math::BigInt or subclass thereof.
5
6use strict;
7use warnings;
8
9package main;
10
11use Test::More tests => 10;
12
13use Math::BigInt;
14
15###############################################################################
16
17for my $class ('Math::BigInt', 'Math::BigInt::Subclass') {
18
19    # This object defines what we want.
20
21    my $int = $class -> new(10);
22
23    # Create various objects that should work with the object above after
24    # objectify() has done its thing.
25
26    my $int_percent1 = My::Percent::Int1 -> new(100);
27    is($int * $int_percent1, 10,
28       qq|\$class -> new(10);|
29       . q| $int_percent1 = My::Percent::Int1 -> new(100);|
30       . q| $int * $int_percent1|);
31
32    my $int_percent2 = My::Percent::Int2 -> new(100);
33    is($int * $int_percent2, 10,
34       qq|\$class -> new(10);|
35       . q| $int_percent2 = My::Percent::Int2 -> new(100);|
36       . q| $int * $int_percent2|);
37
38    my $int_percent3 = My::Percent::Int3 -> new(100);
39    is($int * $int_percent3, 10,
40       qq|\$class -> new(10);|
41       . q| $int_percent3 = My::Percent::Int3 -> new(100);|
42       . q| $int * $int_percent3|);
43
44    my $int_percent4 = My::Percent::Int4 -> new(100);
45    is($int * $int_percent4, 10,
46       qq|\$class -> new(10);|
47       . q| $int_percent4 = My::Percent::Int4 -> new(100);|
48       . q| $int * $int_percent4|);
49
50    my $int_percent5 = My::Percent::Int5 -> new(100);
51    is($int * $int_percent5, 10,
52       qq|\$class -> new(10);|
53       . q| $int_percent5 = My::Percent::Int5 -> new(100);|
54       . q| $int * $int_percent5|);
55}
56
57###############################################################################
58# Class supports as_int(), which returns a Math::BigInt.
59
60package My::Percent::Int1;
61
62sub new {
63    my $class = shift;
64    my $num = shift;
65    return bless \$num, $class;
66}
67
68sub as_int {
69    my $self = shift;
70    return Math::BigInt -> new($$self / 100);
71}
72
73###############################################################################
74# Class supports as_int(), which returns a scalar.
75
76package My::Percent::Int2;
77
78sub new {
79    my $class = shift;
80    my $num = shift;
81    return bless \$num, $class;
82}
83
84sub as_int {
85    my $self = shift;
86    return $$self / 100;
87}
88
89###############################################################################
90# Class does not support as_int(), but supports as_number(), which returns a
91# Math::BigInt.
92
93package My::Percent::Int3;
94
95sub new {
96    my $class = shift;
97    my $num = shift;
98    return bless \$num, $class;
99}
100
101sub as_number {
102    my $self = shift;
103    return Math::BigInt -> new($$self / 100);
104}
105
106###############################################################################
107# Class does  not support as_int(),  but supports as_number(), which  returns a
108# scalar.
109
110package My::Percent::Int4;
111
112sub new {
113    my $class = shift;
114    my $num = shift;
115    return bless \$num, $class;
116}
117
118sub as_number {
119    my $self = shift;
120    return $$self / 100;
121}
122
123###############################################################################
124# Class supports neither as_int() or as_number().
125
126package My::Percent::Int5;
127
128use overload '""' => sub { $_[0] -> as_string(); };
129
130sub new {
131    my $class = shift;
132    my $num = shift;
133    return bless \$num, $class;
134}
135
136sub as_string {
137    my $self = shift;
138    return $$self / 100;
139}
140
141###############################################################################
142
143package Math::BigInt::Subclass;
144
145use base 'Math::BigInt';
146