File: 03-config.t

package info (click to toggle)
libmethod-signatures-simple-perl 1.07-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: perl: 141; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,344 bytes parent folder | download | duplicates (4)
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

use strict;
use warnings;

use Test::More tests => 3;

# testing that we can install several different keywords into the same scope
{
    package Monster;

    use Method::Signatures::Simple;
    use Method::Signatures::Simple name => 'action', invocant => '$monster';
    use Method::Signatures::Simple method_keyword => 'constructor', invocant => '$species';
    use Method::Signatures::Simple function_keyword => 'function';

    constructor spawn {
        bless {@_}, $species;
    }

    action speak (@words) {
        return join ' ', $monster->{name}, $monster->{voices}, @words;
    }

    action attack ($me: $you) {
        $you->take_damage($me->{strength});
    }

    method take_damage ($hits) {
        $self->{hitpoints} = calculate_damage($self->{hitpoints}, $hits);
        if($self->{hitpoints} <= 0) {
            $self->{is_dead} = 1;
        }
    }

    function calculate_damage ($hitpoints, $damage) {
        return $hitpoints - $damage;
    }
}

package main;
my $hellhound = Monster->spawn( name => "Hellhound", voices => "barks", strength => 22, hitpoints => 100 );
is $hellhound->speak(qw(arf arf)), 'Hellhound barks arf arf';

my $human = Monster->spawn( name => 'human', voices => 'whispers', strength => 4, hitpoints => 16 );
$hellhound->attack($human);
is $human->{is_dead}, 1;
is $human->{hitpoints}, -6;