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::BigFloat or subclass thereof.
5
6use strict;
7use warnings;
8
9package main;
10
11use Test::More tests => 6;
12
13use Math::BigFloat;
14
15###############################################################################
16
17for my $class ('Math::BigFloat', 'Math::BigFloat::Subclass') {
18
19    # This object defines what we want.
20
21    my $float = $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 $float_percent1 = My::Percent::Float1 -> new(100);
27    is($float * $float_percent1, 10,
28       qq|\$float = $class -> new(10);|
29       . q| $float_percent1 = My::Percent::Float1 -> new(100);|
30       . q| $float * $float_percent1;|);
31
32    my $float_percent2 = My::Percent::Float2 -> new(100);
33    is($float * $float_percent2, 10,
34       qq|\$float = $class -> new(10);|
35       . q| $float_percent2 = My::Percent::Float2 -> new(100);|
36       . q| $float * $float_percent2;|);
37
38    my $float_percent3 = My::Percent::Float3 -> new(100);
39    is($float * $float_percent3, 10,
40       qq|\$float = $class -> new(10);|
41       . q| $float_percent3 = My::Percent::Float3 -> new(100);|
42       . q| $float * $float_percent3;|);
43}
44
45###############################################################################
46# Class supports as_float(), which returns a Math::BigFloat.
47
48package My::Percent::Float1;
49
50sub new {
51    my $class = shift;
52    my $num = shift;
53    return bless \$num, $class;
54}
55
56sub as_float {
57    my $self = shift;
58    return Math::BigFloat -> new($$self / 100);
59}
60
61###############################################################################
62# Class supports as_float(), which returns a scalar.
63
64package My::Percent::Float2;
65
66sub new {
67    my $class = shift;
68    my $num = shift;
69    return bless \$num, $class;
70}
71
72sub as_float {
73    my $self = shift;
74    return $$self / 100;
75}
76
77###############################################################################
78# Class does not support as_float().
79
80package My::Percent::Float3;
81
82use overload '""' => sub { $_[0] -> as_string(); };
83
84sub new {
85    my $class = shift;
86    my $num = shift;
87    return bless \$num, $class;
88}
89
90sub as_string {
91    my $self = shift;
92    return $$self / 100;
93}
94
95###############################################################################
96
97package Math::BigFloat::Subclass;
98
99use base 'Math::BigFloat';
100