use
5.008001;
BEGIN {
$ENV
{PERL_BSON_BACKEND} =
""
}
BEGIN {
$INC
{
"BSON/XS.pm"
} =
undef
}
binmode
( Test::More->builder->
$_
,
":utf8"
)
for
qw/output failure_output todo_output/
;
use
BSON
qw/encode decode/
;
my
(
$bson
,
$expect
,
$hash
);
my
$bindata
=
"\1\2\3\4\5"
;
is( bson_bytes(),
''
,
"empty bson_bytes() is ''"
);
is( BSON::Bytes->new->data,
''
,
"empty BSON::Bytes constructor is ''"
);
is( bson_bytes(
$bindata
, 2)->subtype, 2,
"bson_bytes(\$data, \$subtype) works"
);
is( bson_bytes(
$bindata
),
$bindata
,
"BSON::Bytes string overload"
);
$bson
=
$expect
= encode( {
A
=> bson_bytes(
$bindata
) } );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Bytes'
,
"BSON::Bytes->BSON::Bytes"
);
is(
"$hash->{A}"
,
$bindata
,
"value correct"
);
$bson
= encode( {
A
=> \
$bindata
} );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Bytes'
,
"scalarref->BSON::Bytes"
);
is(
"$hash->{A}"
,
$bindata
,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
$hash
= encode( {
A
=> BSON::Binary->new(
$bindata
) } );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Bytes'
,
"BSON::Binary->BSON::Bytes"
);
is(
"$hash->{A}"
,
$bindata
,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
SKIP: {
skip(
"MongoDB::BSON::Binary not installed"
, 2 )
unless
$INC
{
'MongoDB/BSON/Binary.pm'
};
$bson
= encode( {
A
=> MongoDB::BSON::Binary->new(
data
=>
$bindata
) } );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Bytes'
,
"MongoDB::BSON::Binary->BSON::Bytes"
);
is(
"$hash->{A}"
,
$bindata
,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
}
my
$test_data
=
"\1\2\3\4\0\1\2\3\4"
;
my
$b64_data
= encode_base64(
$test_data
,
""
);
is( to_myjson({
a
=>bson_bytes(
$test_data
)}),
qq[{"a":"$b64_data"}]
,
'json: bson_bytes(<data>)'
);
is( to_extjson({
a
=>bson_bytes(
$test_data
)}),
qq[{"a":{"\$binary":"$b64_data","\$type":"00"}}]
,
'extjson: bson_bytes(<data>)'
);
is( to_extjson({
a
=>bson_bytes(
$test_data
,128)}),
qq[{"a":{"\$binary":"$b64_data","\$type":"80"}}]
,
'extjson: bson_bytes(<data>,128)'
);
done_testing;