rework keyword properties; add 'defaults', 'strict'
[p5sagit/Function-Parameters.git] / t / foreign / Method-Signatures-Simple / 03-config.t
CommitLineData
d93106d7 1#!perl
2use strict;
3use warnings FATAL => 'all';
4
5use Test::More tests => 3;
6
7# testing that we can install several different keywords into the same scope
8{
9 package Monster;
10
11 use Function::Parameters;
12 use Function::Parameters {
f7651a6e 13 action => { defaults => 'method', shift => '$monster' },
14 constructor => { defaults => 'method', shift => '$species' },
1a52f2db 15 function => 'function',
d93106d7 16 };
17
18 constructor spawn {
19 bless {@_}, $species;
20 }
21
22 action speak (@words) {
23 return join ' ', $monster->{name}, $monster->{voices}, @words;
24 }
25
26 action attack ($me: $you) {
27 $you->take_damage($me->{strength});
28 }
29
30 method take_damage ($hits) {
31 $self->{hitpoints} = calculate_damage($self->{hitpoints}, $hits);
32 if($self->{hitpoints} <= 0) {
33 $self->{is_dead} = 1;
34 }
35 }
36
37 function calculate_damage ($hitpoints, $damage) {
38 return $hitpoints - $damage;
39 }
40}
41
42package main;
43my $hellhound = Monster->spawn( name => "Hellhound", voices => "barks", strength => 22, hitpoints => 100 );
44is $hellhound->speak(qw(arf arf)), 'Hellhound barks arf arf';
45
46my $human = Monster->spawn( name => 'human', voices => 'whispers', strength => 4, hitpoints => 16 );
47$hellhound->attack($human);
48is $human->{is_dead}, 1;
49is $human->{hitpoints}, -6;
50