6 use Scalar::Util 'refaddr';
7 use Storable 'nfreeze';
9 use SQL::Abstract qw(is_plain_value is_literal_value);
11 # fallback setting is inheriting starting p5 50853fa9 (run up to 5.17.0)
12 use constant OVERLOAD_FALLBACK_INHERITS => ( ($] < 5.017) ? 0 : 1 );
13 use constant STRINGIFIER_CAN_RETURN_IVS => ( ($] < 5.008) ? 0 : 1 );
20 # *DELIBERATELY* unspecified
22 '0+' => sub { ${$_[0]} },
26 SQLATest::SillyInt::Subclass;
28 our @ISA = 'SQLATest::SillyInt';
40 SQLATest::SillierInt::Subclass;
43 '0+' => sub { ${$_[0]} },
44 '+' => sub { ${$_[0]} + $_[1] },
47 our @ISA = 'SQLATest::SillierInt';
56 '0+' => sub { ${$_[0]} },
60 SQLATest::AnalInt::Subclass;
63 '0+' => sub { ${$_[0]} },
66 our @ISA = 'SQLATest::AnalInt';
71 SQLATest::ReasonableInt;
73 # make it match JSON::PP::Boolean
75 '0+' => sub { ${$_[0]} },
76 '++' => sub { $_[0] = ${$_[0]} + 1 },
77 '--' => sub { $_[0] = ${$_[0]} - 1 },
82 SQLATest::ReasonableInt::Subclass;
84 our @ISA = 'SQLATest::ReasonableInt';
89 SQLATest::ReasonableString;
91 # somewhat like DateTime
94 '""' => sub { "${$_[0]}" },
95 '-' => sub { ${$_[0]} - $_[1] },
96 '+' => sub { ${$_[0]} + $_[1] },
100 SQLATest::ReasonableString::Subclass;
102 our @ISA = 'SQLATest::ReasonableString';
106 { class => 'SQLATest::SillyInt', can_math => 0, should_str => 1 },
107 { class => 'SQLATest::SillyInt::Subclass', can_math => 0, should_str => 1 },
108 { class => 'SQLATest::SillierInt', can_math => 0, should_str => 0 },
109 { class => 'SQLATest::SillierInt::Subclass',can_math => 1, should_str => (OVERLOAD_FALLBACK_INHERITS ? 0 : 1) },
110 { class => 'SQLATest::AnalInt', can_math => 0, should_str => 0 },
111 { class => 'SQLATest::AnalInt::Subclass', can_math => 0, should_str => (OVERLOAD_FALLBACK_INHERITS ? 0 : 1) },
112 { class => 'SQLATest::ReasonableInt', can_math => 1, should_str => 1 },
113 { class => 'SQLATest::ReasonableInt::Subclass', can_math => 1, should_str => 1 },
114 { class => 'SQLATest::ReasonableString', can_math => 1, should_str => 1 },
115 { class => 'SQLATest::ReasonableString::Subclass',can_math => 1, should_str => 1 },
118 my $num = bless( \do { my $foo = 42 }, $case->{class} );
120 my $can_str = eval { "$num" eq 42 } || 0;
123 !($can_str xor $case->{should_str}),
124 "should_str setting for $case->{class} matches perl behavior",
125 ) || diag explain { %$case, can_str => $can_str };
127 my $can_math = eval { ($num + 1) == 43 } ? 1 : 0;
130 !($can_math xor $case->{can_math}),
131 "can_math setting for $case->{class} matches perl behavior",
132 ) || diag explain { %$case, actual_can_math => $can_math };
134 my $can_cmp = eval { my $dum = ($num eq "nope"); 1 } || 0;
140 ok $num, 'bool ctx works';
142 if (STRINGIFIER_CAN_RETURN_IVS and $can_cmp) {
146 "stringification detected on $case->{class}",
147 ) || diag explain $case;
150 # is_deeply does not do nummify/stringify cmps properly
151 # but we can always compare the ice
153 ( nfreeze( is_plain_value $num ) eq nfreeze( \$num ) ),
154 "stringification without cmp capability detected on $case->{class}"
155 ) || diag explain $case;
159 refaddr( ${is_plain_value($num)} ),
161 "Same reference (blessed object) returned",
165 is( is_plain_value($num), undef, "non-stringifiable $case->{class} object detected" )
166 || diag explain $case;
169 if ($case->{can_math}) {
176 my $num = bless( \do { my $foo = 23 }, 'SQLATest::ReasonableInt' );
177 cmp_ok(++$num, '==', 24, 'test overloaded object compares correctly');
178 cmp_ok(--$num, 'eq', 23, 'test overloaded object compares correctly');
182 'fallback stringification detected'
184 cmp_ok(--$num, 'eq', 22, 'test overloaded object compares correctly');
185 cmp_ok(++$num, '==', 23, 'test overloaded object compares correctly');
186 } 'overload testing lives';
190 is_plain_value { -value => [] },
199 'nonvalues correctly recognized'
203 for (undef, { -value => undef }) {
207 'NULL -value recognized'
212 is_literal_value { -ident => 'foo' },
214 '-ident recognized as literal'
218 is_literal_value \[ 'sql', 'bind1', [ {} => 'bind2' ] ],
219 [ 'sql', 'bind1', [ {} => 'bind2' ] ],
220 'literal correctly unpacked'
224 for ([], {}, \'', undef) {
226 is_literal_value { -ident => $_ },
228 'illegal -ident does not trip up detection'