use
5.0001;
BEGIN {
$ENV
{PERL_BSON_BACKEND} =
undef
}
BEGIN {
$INC
{
"BSON/XS.pm"
} =
undef
}
use
BSON
qw/encode decode/
;
my
(
$hash
,
$bson
,
$expect
);
my
$bigpos
= Math::BigInt->new(
"2147483648"
);
my
$bigneg
= Math::BigInt->new(
"-2147483649"
);
packed_is(
"l"
, bson_int32(), 0,
"empty bson_int32() is 0"
);
packed_is(
"l"
, BSON::Int32->new, 0,
"empty constructor is 0"
);
eval
{ bson_int32(2**31) };
like( $@,
qr/can't fit/
,
"bson_int32(2**31) fails"
);
eval
{ bson_int32(-2**31-1) };
like( $@,
qr/can't fit/
,
"bson_int32(-2**31-1) fails"
);
eval
{ bson_int32(
$bigpos
) };
like( $@,
qr/can't fit/
,
"bson_int32(big BigInt) fails"
);
eval
{ bson_int32(
$bigneg
) };
like( $@,
qr/can't fit/
,
"bson_int32(-big BigInt) fails"
);
packed_is(
"l"
, bson_int32(314159), 314159,
"overloading correct"
);
subtest
'native'
=>
sub
{
$bson
=
$expect
= encode( {
A
=> 314159 } );
$hash
= decode(
$bson
);
is( sv_type(
$hash
->{A} ),
'IV'
,
"int32->int32"
);
packed_is(
"l"
,
$hash
->{A}, 314159,
"value correct"
);
$bson
= encode( {
A
=> bson_int32(314159) } );
$hash
= decode(
$bson
);
is( sv_type(
$hash
->{A} ),
'IV'
,
"BSON::Int32->int32"
);
packed_is(
"l"
,
$hash
->{A}, 314159,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
$bson
= encode( {
A
=> bson_int32(
"314159"
) } );
$hash
= decode(
$bson
);
is( sv_type(
$hash
->{A} ),
'IV'
,
"BSON::Int32->int32"
);
packed_is(
"l"
,
$hash
->{A}, 314159,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
};
subtest
'wrapped'
=>
sub
{
$bson
=
$expect
= encode( {
A
=> 314159 } );
$hash
= decode(
$bson
,
wrap_numbers
=> 1 );
is(
ref
(
$hash
->{A} ),
'BSON::Int32'
,
"int32->BSON::Int32"
);
packed_is(
"l"
,
$hash
->{A}, 314159,
"value correct"
);
$bson
= encode( {
A
=> bson_int32(314159) } );
$hash
= decode(
$bson
,
wrap_numbers
=> 1 );
is(
ref
(
$hash
->{A} ),
'BSON::Int32'
,
"int32->BSON::Int32"
);
packed_is(
"l"
,
$hash
->{A}, 314159,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
$bson
= encode( {
A
=> bson_int32(
"314159"
) } );
$hash
= decode(
$bson
,
wrap_numbers
=> 1 );
is(
ref
(
$hash
->{A} ),
'BSON::Int32'
,
"int32->BSON::Int32"
);
packed_is(
"l"
,
$hash
->{A}, 314159,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
};
SKIP: {
skip
"JSON::PP has trouble with TO_JSON being false"
, 1
if
ref
JSON::MaybeXS->new eq
'JSON::PP'
;
is( to_myjson({
a
=>bson_int32(0)}),
q[{"a":0}]
,
'bson_int32(0)'
);
}
is( to_myjson({
a
=>bson_int32(42)}),
q[{"a":42}]
,
'bson_int32(42)'
);
SKIP: {
skip
"JSON::PP has trouble with TO_JSON being false"
, 1
if
ref
JSON::MaybeXS->new eq
'JSON::PP'
;
is( to_extjson({
a
=>bson_int32(0)}),
q[{"a":0}]
,
'extjson: bson_int32(0)'
);
}
is( to_extjson({
a
=>bson_int32(42)}),
q[{"a":42}]
,
'extjson: bson_int32(42)'
);
done_testing;