### ----------------------------------------------------
### -- Config::BINDish
### -- Licenses: Artistic-2.0
### -- Authors: Vadim Belman <vrurg@lflat.org>
### -- File: t/041-explicit-bug-1.rakutest
### ----------------------------------------------------
use v6.d;
use Test::Async <Config::BINDish::Test Base>;
use Config::BINDish;
use Config::BINDish::Grammar;
use Config::BINDish::Expandable;
use Config::BINDish::AST;
use Config::BINDish::X;
use IP::Addr;

# Test for Expandable-related bugs:
# 1. When $*CFG-VALUE was lost and was causing incorrect syntax errors
# 2. Expandable strings were not considered standalone values as they are not C::B::AST::Value but just containers.

plan 5;

role CustomExt is BINDish-grammar {
    submethod setup-BINDish {
        self.declare-blocks:
            :server => ( :in<.TOP> ),
            :file-pool => ( :in<.TOP>, :named ),
            :fpool-alias<alias> => ( :in<file-pool>,
                                     :type(Str | IO::Path),
                                     :value-only,
                                     :value-sym<string file-path> ),
            ;
        self.declare-options:
            :fpool-path<path> => ( :in<file-pool>, :type(Str) ),
            :base-path => ( :in<server>, :type(Str | IO::Path), value-sym => <file-path> ),
            ;
    }
}

my $cfg = Config::BINDish.new: :strict(:blocks, :options);

lives-ok {
    $cfg.read: string => q:to/CFG/;
            file-pool "static" {
                path "{/server/base-path}/frontend";
                alias "foo.png" {
                    "img/bar.png";
                }
            }

            server {
                // How the server can be reached by a client.
                base-path .;
            }
            CFG
}, "config parsed";

is $cfg.get(:file-pool<static> => "path"), "./frontend", "option is parsed and expanded correctly";

my $alias-blk = $cfg.get( :block(:file-pool<static> => :alias<foo.png> ) );

if is $alias-blk.values.elems, 1, "block standalone value count" {
    my $val0 = $alias-blk.values(:raw)[0];
    does-ok $val0, Config::BINDish::AST::Container, "value node is a container";
    is $val0.payload, "img/bar.png", "value payload";
}
else {
    skip "no values to analyze", 2;
}

done-testing;


