### ----------------------------------------------------
### -- AttrX::Mooish
### -- Licenses: Artistic-2.0
### -- Authors: Vadim Belman
### -- File: t/040-errors.custom.rakutest
### ----------------------------------------------------
use Test;
use AttrX::Mooish;
use AttrX::Mooish::X;

use lib $?FILE.IO.parent(1).add("lib"); use CustomHOW;

plan 4;
throws-like
    q<my class Foo1 { has $.bar is rw is mooish(:filter); }; Foo1.new.bar = 123; >,
    X::Method::NotFound,
    message => "No such method 'filter-bar' for invocant of type 'Foo1'",
    "missing filter method"
    ;

throws-like
    q<my class Foo1 { has $.bar is rw is mooish(:trigger); }; Foo1.new.bar = 123; >,
    X::Method::NotFound,
    message => "No such method 'trigger-bar' for invocant of type 'Foo1'",
    "missing trigger method"
    ;

subtest "Base Errors" => {
    plan 3;
    my $inst;

    throws-like q{my class Foo1 { has $.bar is mooish(:lazy(pi)); }},
            AttrX::Mooish::X::TypeCheck::MooishOption, "bad option value";

    my class Foo2 {
        has $.bar is mooish(:lazy);
    }

    throws-like { $inst = Foo2.new; my $v = $inst.bar; },
        X::Method::NotFound,
        message => q<No such method 'build-bar' for invocant of type 'Foo2'>,
        "missing builder";

    my class Foo4 {
        has Str $.bar is rw is mooish(:lazy) where *.starts-with("a");

        method build-bar { "default value" }
    }

    throws-like { $inst = Foo4.new; my $v = $inst.bar },
        AttrX::Mooish::X::StoreValue,
        exception => X::TypeCheck,
        message => /<.wb> "X::TypeCheck::Assignment" <.wb> .* \s '$!bar' <.wb>/,
        "value from builder don't conform 'where' constraint";
}

subtest "Nils", {
    plan 1;

    my $inst;

    my class Foo {
        has %.foo is mooish(:lazy);

        method build-foo { }
    }

    $inst = Foo.new;

    throws-like { $inst.foo },
                AttrX::Mooish::X::StoreValue,
                exception => X::Hash::Store::OddNumber,
                message => /<.wb> "X::Hash::Store::OddNumber" <.wb> .* \s '%!foo' <.wb>/,
                "hash build returns Nil";
}

done-testing;
# vim: ft=raku


