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 bool => sub { ${$_[0]} },
26 SQLATest::SillyBool::Subclass;
28 our @ISA = 'SQLATest::SillyBool';
36 # *DELIBERATELY* unspecified
38 '0+' => sub { ${$_[0]} },
42 SQLATest::SillyInt::Subclass;
44 our @ISA = 'SQLATest::SillyInt';
56 SQLATest::SillierInt::Subclass;
59 '0+' => sub { ${$_[0]} },
60 '+' => sub { ${$_[0]} + $_[1] },
63 our @ISA = 'SQLATest::SillierInt';
72 '0+' => sub { ${$_[0]} },
76 SQLATest::AnalInt::Subclass;
79 '0+' => sub { ${$_[0]} },
82 our @ISA = 'SQLATest::AnalInt';
87 SQLATest::ReasonableInt;
89 # make it match JSON::PP::Boolean
91 '0+' => sub { ${$_[0]} },
92 '++' => sub { $_[0] = ${$_[0]} + 1 },
93 '--' => sub { $_[0] = ${$_[0]} - 1 },
98 SQLATest::ReasonableInt::Subclass;
100 our @ISA = 'SQLATest::ReasonableInt';
105 SQLATest::ReasonableString;
107 # somewhat like DateTime
110 '""' => sub { "${$_[0]}" },
111 '-' => sub { ${$_[0]} - $_[1] },
112 '+' => sub { ${$_[0]} + $_[1] },
116 SQLATest::ReasonableString::Subclass;
118 our @ISA = 'SQLATest::ReasonableString';
122 { class => 'SQLATest::SillyBool', can_math => 0, should_str => 1 },
123 { class => 'SQLATest::SillyBool::Subclass', can_math => 0, should_str => 1 },
124 { class => 'SQLATest::SillyInt', can_math => 0, should_str => 1 },
125 { class => 'SQLATest::SillyInt::Subclass', can_math => 0, should_str => 1 },
126 { class => 'SQLATest::SillierInt', can_math => 0, should_str => 0 },
127 { class => 'SQLATest::SillierInt::Subclass',can_math => 1, should_str => (OVERLOAD_FALLBACK_INHERITS ? 0 : 1) },
128 { class => 'SQLATest::AnalInt', can_math => 0, should_str => 0 },
129 { class => 'SQLATest::AnalInt::Subclass', can_math => 0, should_str => (OVERLOAD_FALLBACK_INHERITS ? 0 : 1) },
130 { class => 'SQLATest::ReasonableInt', can_math => 1, should_str => 1 },
131 { class => 'SQLATest::ReasonableInt::Subclass', can_math => 1, should_str => 1 },
132 { class => 'SQLATest::ReasonableString', can_math => 1, should_str => 1 },
133 { class => 'SQLATest::ReasonableString::Subclass',can_math => 1, should_str => 1 },
136 my $num = bless( \do { my $foo = 42 }, $case->{class} );
138 my $can_str = eval { "$num" eq 42 } || 0;
141 !($can_str xor $case->{should_str}),
142 "should_str setting for $case->{class} matches perl behavior",
143 ) || diag explain { %$case, can_str => $can_str };
145 my $can_math = eval { ($num + 1) == 43 } ? 1 : 0;
148 !($can_math xor $case->{can_math}),
149 "can_math setting for $case->{class} matches perl behavior",
150 ) || diag explain { %$case, actual_can_math => $can_math };
152 my $can_cmp = eval { my $dum = ($num eq "nope"); 1 } || 0;
158 ok $num, 'bool ctx works';
160 if (STRINGIFIER_CAN_RETURN_IVS and $can_cmp) {
164 "stringification detected on $case->{class}",
165 ) || diag explain $case;
168 # is_deeply does not do nummify/stringify cmps properly
169 # but we can always compare the ice
171 ( nfreeze( is_plain_value $num ) eq nfreeze( \$num ) ),
172 "stringification without cmp capability detected on $case->{class}"
173 ) || diag explain $case;
177 refaddr( ${is_plain_value($num)} ),
179 "Same reference (blessed object) returned",
183 is( is_plain_value($num), undef, "non-stringifiable $case->{class} object detected" )
184 || diag explain $case;
187 if ($case->{can_math}) {
194 my $num = bless( \do { my $foo = 23 }, 'SQLATest::ReasonableInt' );
195 cmp_ok(++$num, '==', 24, 'test overloaded object compares correctly');
196 cmp_ok(--$num, 'eq', 23, 'test overloaded object compares correctly');
200 'fallback stringification detected'
202 cmp_ok(--$num, 'eq', 22, 'test overloaded object compares correctly');
203 cmp_ok(++$num, '==', 23, 'test overloaded object compares correctly');
204 } 'overload testing lives';
208 is_plain_value { -value => [] },
217 'nonvalues correctly recognized'
221 for (undef, { -value => undef }) {
225 'NULL -value recognized'
230 is_literal_value { -ident => 'foo' },
232 '-ident recognized as literal'
236 is_literal_value \[ 'sql', 'bind1', [ {} => 'bind2' ] ],
237 [ 'sql', 'bind1', [ {} => 'bind2' ] ],
238 'literal correctly unpacked'
242 for ([], {}, \'', undef) {
244 is_literal_value { -ident => $_ },
246 'illegal -ident does not trip up detection'