Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 76 lines (59 sloc) 2.8 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use v6;

use Test;

plan 30;

=begin pod

Tests for .^attributes from L<S12/Introspection>.

=end pod

# L<S12/Introspection/"The .^attributes method">

class A {
    has Str $.a = "dnes je horuci a potrebujem pivo";
}
class B is A {
    has Int $!b = 42;
}
class C is B {
    has $.c is rw
}

my @attrs = C.^attributes();
is +@attrs, 3, 'attribute introspection gave correct number of elements';

is @attrs[0].name, '$!c', 'first attribute had correct name';
is @attrs[0].type.gist, '(Mu)', 'first attribute had correct type';
is @attrs[0].has-accessor, True, 'first attribute has an accessor';
ok !@attrs[0].build, 'first attribute has no build value';
ok @attrs[0].rw, 'first attribute is rw';
ok !@attrs[0].readonly, 'first attribute is not readonly';

is @attrs[1].name, '$!b', 'second attribute had correct name';
is @attrs[1].type.gist, '(Int)', 'second attribute had correct type';
is @attrs[1].has-accessor, False, 'second attribute has no accessor';
ok @attrs[1].build ~~ Code, 'second attribute has build block';
is @attrs[1].build().(C, $_), 42,
                              'second attribute build block gives expected value';

is @attrs[2].name, '$!a', 'third attribute had correct name';
is @attrs[2].type.gist, '(Str)', 'third attribute had correct type';
is @attrs[2].has-accessor, True, 'third attribute has an accessor';
ok @attrs[2].build ~~ Code, 'third attribute has build block';
is @attrs[2].build().(C, $_), "dnes je horuci a potrebujem pivo",
                                    'third attribute build block gives expected value';
ok !@attrs[2].rw, 'third attribute is not rw';
ok @attrs[2].readonly, 'third attribute is readonly';

@attrs = C.^attributes(:local);
is +@attrs, 1, 'attribute introspection with :local gave just attribute in base class';
is @attrs[0].name, '$!c', 'get correct attribute with introspection';

#?rakudo skip ':tree not implemented for .^attributes'
{
    @attrs = C.^attributes(:tree);
    is +@attrs, 2, 'attribute introspection with :tree gives right number of elements';
    is @attrs[0].name, '$!c', 'first element is attribute desriptor';
    ok @attrs[1] ~~ Array, 'second element is array';
    is @attrs[1][0].name, '$!b', 'can look into second element array to find next attribute';
    is @attrs[1][1][0].name, '$!a', 'can look deeper to find attribute beyond that';
}

{
    my $x = A.new(a => 'abc');
    my $attr = $x.^attributes(:local).[0];
    isa_ok $attr, Attribute;
    is $attr.get_value($x), 'abc', '.get_value works';
    lives_ok { $attr.set_value($x, 'new') }, 'can set_value';
    is $x.a, 'new', 'set_value worked';

}

# vim: ft=perl6
Something went wrong with that request. Please try again.