Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 47 lines (35 sloc) 1.152 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
use v6;

use Test;

plan 9;

=begin pod

Basic traits tests, see L<S14/Traits>.

=end pod

# L<S14/Traits>
# Basic definition
my $was_in_any_sub = 0;
my $was_in_class_sub = 0;
role cool {
  has $.is_cool = 42;

  multi sub trait_auxiliary:<is>(cool $trait, Any $container:) { #OK not used
    $was_in_any_sub++;
    $container does cool;
  }

  multi sub trait_auxiliary:<is>(cool $trait, Class $container:) { #OK not used
    $was_in_class_sub++;
    $container does cool;
  }
}
ok(::cool.HOW, "role definition worked");

eval_lives_ok 'my $a is cool; 1', 'mixing in our role into a scalar via "is" worked';
is $was_in_any_sub, 1, 'trait_auxiliary:is was called on container';
is EVAL('my $a is cool; $a.is_cool'), 42, 'our var "inherited" an attribute';

my $b;
class B is cool {}
ok(::B.HOW, 'mixing in our role into a class via "is" worked');
is $was_in_class_sub, 1, 'trait_auxiliary:is was called on class';
$b = B.new;
ok($b, 'creating an instance worked');
is($b.is_cool, 42, 'our class "inherited" an attribute');

eval_dies_ok(' %!P = 1; 1',
        'calling a trait outside of a class should be a syntax error');

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