use strict;
use warnings;

use Config::Model;

use Config::Model::Tester::Setup qw/init_test setup_test_dir/;
use Path::Tiny;
use 5.10.0;

use warnings;
use strict;
use Test::More;
use Test::Exception;

$::_use_log4perl_to_warn = 1;

my ($model, $trace, $opts) = init_test('rdhint','rdtrace');

{
    no warnings qw/once/;
    $::RD_HINT  = 1 if $opts->{rdhint};
    $::RD_TRACE = 1 if $opts->{rdtrace};
}

my $wr_dir = setup_test_dir();

$wr_dir->child("debian")->mkpath() ;

# instance to check one dependency at a time
my $root = $model->instance (
    root_class_name => 'Dpkg::Copyright',
    root_dir        => $wr_dir,
    instance_name   => "short-name-test",
)->config_root;

my $lic = $root->fetch_element('License');

foreach my $name (qw/GPL-3+ GPL-2+ Artistic-2.0 BSD-3-clause/,"GPL-3+ with Font exception") {
    ok($lic->fetch_with_id($name),"fetch «$name» license object");
}

# this license index is not a valid short name. But the check is done
# by short name object, not by this hash element
ok($lic->fetch_with_id("GPL-3+ with Font-exception-2.0"), "check license with bad font exception");

print $root->dump_tree if $trace;

my $short = $root->grab('Files:"etc/fonts/*" License short_name');
ok($short, "got short_name object");

foreach my $name ("GPL-3+","GPL-3+ with Font exception",
                  "GPL-2+ or Artistic-2.0, and BSD-3-clause") {
    ok($short->store($name),"store «$name» short name");
}

throws_ok {
    $short->store("GPL-3+ with Font-exception-2.0");
} qr/does not match grammar from model/, "bad short name rejected when storing";

done_testing();
