Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e267414689
Fetching contributors…

Cannot retrieve contributors at this time

file 82 lines (65 sloc) 1.924 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 76 77 78 79 80 81 82
use v6;
use Test;

# L<S03/Item assignment precedence>

plan 12;

# Tests for binding instance and class attributes
# note that only attributes themselves ($!foo) can be bound,
# not accessors ($.foo)

{
    my $var = 42;
    class Klass1 { has $.x; method bind { $!x := $var } }

    my $obj1 = Klass1.new;
    lives_ok { $obj1.bind() }, 'attribute binding lives';

    #?pugs 3 todo 'bug'
    is $obj1.x, 42, 'binding $!x instance attribute (1)';
    $var = 23;
    is $obj1.x, 23, 'binding $!x instance attribute (2)';
}

{
    my $var = 42;
    class Klass2 {
        has $x;
        method bind { $x := $var }
        method get_x { $x }
        method set_x ($new_x) { $x = $new_x }
    }

    my $obj2 = Klass2.new;
    $obj2.bind();

    #?pugs 3 todo 'bug'
    is $obj2.get_x, 42, 'binding $x instance attribute (1)';
    $var = 23;
    is $obj2.get_x, 23, 'binding $x instance attribute (2)';
    $obj2.set_x(19);
    is $var, 19, 'binding $x instance attribute (3)';
}

# Public class attributes
#?rakudo skip 'class attributes'
{
    my $var = 42;
    class Klass3 { our $.x; method bind { $!x := $var } }

    try { Klass3.bind() };

    #?pugs 3 todo 'bug'
    is try { Klass3.x }, 42, "binding public class attribute (1)";
    $var = 23;
    is try { Klass3.x }, 23, "binding public class attribute (2)";
    try { Klass3.x = 19 };
    is $var, 19, "binding public class attribute (3)";
}

# Private class attributes
#?rakudo skip 'class attributes'
{
    my $var = 42;
    class Klass4 {
        our $x;
        method bind { $x := $var }
        method get_x { $x }
        method set_x ($new_x) { $x = $new_x }
    }

    try { Klass4.bind() };

    is Klass4.get_x, 42, "binding private class attribute (1)";
    $var = 23;
    is Klass4.get_x, 23, "binding private class attribute (2)";
    Klass4.set_x(19);
    is $var, 19, "binding private class attribute (3)";
}


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