#!/bin/env perl -w
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

use Test::More qw(no_plan);
use Test::Exception;

require 'utils.pm';

BEGIN {use_ok('qpid_proton');}
require_ok('qpid_proton');

my $data;
my $hash;
my $result;

#=============================================================================
# raises an error when getting a hash from a null data object
#=============================================================================
dies_ok(sub {qpid::proton::get_map_from(undef);},
        "Raises an error when getting a hash froma  null data object");


#=============================================================================
# raises an error if the current node is not a map
#=============================================================================
$data = qpid::proton::Data->new;
$data->put_string(random_string(16));
$data->rewind;
$data->next;

dies_ok(sub {qpid::proton::get_map_from($data);},
        "Raises an error if the current node is not a map");


#=============================================================================
# returns a hash from a Data object
#=============================================================================
$data = qpid::proton::Data->new;
$hash = random_hash(rand(128) + 64);
$data->put_map;
$data->enter;
foreach my $key (keys \%{$hash}) {
    $data->put_string($key);
    $data->put_string($hash->{$key});
}
$data->exit;
$data->rewind;
$data->next;

{
    $result = qpid::proton::get_map_from($data);

    ok(defined($result), "Getting a hash returns a value");
    ok(scalar(keys %{$result}) == scalar(keys %{$hash}),
       "Returned the same number of keys");
    is_deeply(\%{$result}, \%{$hash}, "Returned the same hash values");
}
