6 use Scalar::Util 'refaddr';
7 use Storable 'nfreeze';
9 use SQL::Abstract qw(is_plain_value is_literal_value);
16 # *DELIBERATELY* unspecified
18 '0+' => sub { ${$_[0]} },
22 SQLATest::SillyInt::Subclass;
24 our @ISA = 'SQLATest::SillyInt';
36 SQLATest::SillierInt::Subclass;
39 '0+' => sub { ${$_[0]} },
40 '+' => sub { ${$_[0]} + $_[1] },
43 our @ISA = 'SQLATest::SillierInt';
48 SQLATest::ReasonableInt;
50 # make it match JSON::PP::Boolean
52 '0+' => sub { ${$_[0]} },
53 '++' => sub { $_[0] = ${$_[0]} + 1 },
54 '--' => sub { $_[0] = ${$_[0]} - 1 },
60 { class => 'SQLATest::SillyInt', can_math => 0, should_str => 1 },
61 { class => 'SQLATest::SillyInt::Subclass', can_math => 0, should_str => 1 },
62 { class => 'SQLATest::SillierInt', can_math => 0, should_str => 0 },
63 { class => 'SQLATest::SillierInt::Subclass',
64 ($] < 5.017) # fallback is inheriting starting p5 50853fa9 (run up to 5.17.0)
65 ? ( can_math => 1, should_str => 1 )
66 : ( can_math => 1, should_str => 0 )
68 { class => 'SQLATest::ReasonableInt', can_math => 1, should_str => 1 },
71 my $num = bless( \do { my $foo = 42 }, $case->{class} );
73 my $can_str = eval { "$num" eq 42 } || 0;
76 !($can_str xor $case->{should_str}),
77 "should_str setting for $case->{class} matches perl behavior",
78 ) || diag explain { %$case, can_str => $can_str };
80 my $can_math = eval { ($num + 1) == 43 } ? 1 : 0;
83 !($can_math xor $case->{can_math}),
84 "can_math setting for $case->{class} matches perl behavior",
85 ) || diag explain { %$case, actual_can_math => $can_math };
87 my $can_cmp = eval { my $dum = $num eq "nope"; 1 } || 0;
93 ok $num, 'bool ctx works';
99 "stringification detected on $case->{class}",
100 ) || diag explain $case;
103 # is_deeply does not do nummify/stringify cmps properly
104 # but we can always compare the ice
106 ( nfreeze( is_plain_value $num ) eq nfreeze( [ $num ] ) ),
107 "stringification without cmp capability detected on $case->{class}"
108 ) || diag explain $case;
112 refaddr(is_plain_value($num)->[0]),
114 "Same reference (blessed object) returned",
118 is( is_plain_value($num), undef, "non-stringifiable $case->{class} object detected" )
119 || diag explain $case;
122 if ($case->{can_math}) {
129 my $num = bless( \do { my $foo = 23 }, 'SQLATest::ReasonableInt' );
130 cmp_ok(++$num, '==', 24, 'test overloaded object compares correctly');
131 cmp_ok(--$num, 'eq', 23, 'test overloaded object compares correctly');
135 'fallback stringification detected'
137 cmp_ok(--$num, 'eq', 22, 'test overloaded object compares correctly');
138 cmp_ok(++$num, '==', 23, 'test overloaded object compares correctly');
139 } 'overload testing lives';
143 is_plain_value { -value => [] },
152 'nonvalues correctly recognized'
156 for (undef, { -value => undef }) {
160 'NULL -value recognized'
165 is_literal_value { -ident => 'foo' },
167 '-ident recognized as literal'
171 is_literal_value \[ 'sql', 'bind1', [ {} => 'bind2' ] ],
172 [ 'sql', 'bind1', [ {} => 'bind2' ] ],
173 'literal correctly unpacked'
177 for ([], {}, \'', undef) {
179 is_literal_value { -ident => $_ },
181 'illegal -ident does not trip up detection'