### ----------------------------------------------------
### -- BSON
### -- Licenses: Artistic-2.0
### -- Authors: Marcel Timmerman, Paweł Pabian
### -- File: t/Objectid.rakutest
### ----------------------------------------------------
use v6;
use Test;

use BSON::ObjectId;

#-------------------------------------------------------------------------------
subtest {

  throws-like
    { my BSON::ObjectId $o .= new(:string<67ab4550>); },
    X::BSON, 'Too short oid string',
    :message(/:s String too short or nonhexadecimal/);

  throws-like
    { my BSON::ObjectId $o .= new(:string<Vbghu7988798Vbghu7988798>); },
    X::BSON, 'Nonhexadecimal',
    :message(/:s String too short or nonhexadecimal/);

  my BSON::ObjectId $o .= new(:string<507f191e810c19729de860ea>);
  is $o.oid.elems, 12, '.new(:string)';
  is $o.Str, '507f191e810c19729de860ea', '.Str()';

  throws-like
    { my BSON::ObjectId $o .= new(:bytes(Buf.new(5,7,9...15))); },
    X::BSON, 'Too short/long byte buffer',
    :message(/:s Byte buffer too short\/long/);

  $o .= new(
    :bytes(
      Buf.new(
        0x0b, 0xab, 0xc7, 0x98, 0x87, 0x98, 0xab, 0xcd, 0xe7, 0x98, 0x87, 0x98,
      )
    )
  );
  is $o.oid.elems, 12, '.new(:bytes)';

  $o .= new;
  is $o.oid.elems, 12, '.new()';

}, 'Object id testing';

#-------------------------------------------------------------------------------
subtest {

  my $time = time;
  my BSON::ObjectId $o .= new;

  is $o.oid.elems, 12, 'Length oid ok';
  ok $time ≤ $o.time <= $time + 1, 'Time between this and the next second';

}, 'Object id encoding/decoding';

#-------------------------------------------------------------------------------
# Cleanup
#
done-testing();
exit(0);


