### ----------------------------------------------------
### -- AttrX::Mooish
### -- Licenses: Artistic-2.0
### -- Authors: Vadim Belman
### -- File: t/015-triggers.rakutest
### ----------------------------------------------------
use Test;
use AttrX::Mooish;

#?mooish-custom

plan 1;

subtest "Triggers", {
    plan 15;
    my $inst;
    my class Foo1 {
        has $.bar is rw is mooish(:trigger);
        has $.baz is rw is mooish(:trigger<on_change>);
        has $.foo is rw is mooish(:trigger(method ($value) {
            pass "in-place trigger";
            is $value, "foo value", "valid value passed to in-place";
        }));
        has $.fubar is rw is mooish(:trigger(-> $,$value,*% {
            pass "pointy-block trigger";
            is $value, "fubar value", "valid value passed to pointy block";
        }));

        method trigger-bar ( $value ) {
            pass "trigger for attribute $!bar";
            is $value, "bar value", "valid value passed to trigger";
        }

        method on_change ( $value, :$attribute ) {
            pass "generic trigger on_chage()";
            is $value, "baz value", "valid value passed to on_change";
            is $attribute, <$!baz>, "received attribute name";
        }
    }

    $inst = Foo1.new;
    $inst.bar = "bar value";
    $inst.baz = "baz value";
    $inst.foo = "foo value";
    $inst.fubar = "fubar value";

    my class Foo2 {
        has $.bar is rw is mooish(:lazy, :trigger);

        method build-bar { "build bar" }
        method trigger-bar ( $value ) { is $value, "build bar", "trigger on lazy build" }
    }

    $inst = Foo2.new;
    is $inst.bar, "build bar", "lazily built as expected";

    my class Foo3 {
        has $.bar is rw is mooish(:lazy, :trigger);

        method build-bar { "from builder" }
        method trigger-bar ( $value, *%opt ) {
            if $value ~~ "from builder" {
                ok %opt<builder>, "builder option is set";
            }
            else {
                nok %opt<builder>:exists, "no builder option";
                is %opt<old-value>, 'from builder', "old value is known to trigger";
            }
        }
    }

    $inst = Foo3.new;
    is $inst.bar, "from builder", "built as expected";
    $inst.bar = "not from builder";
}

done-testing;
# vim: ft=raku


