class-mop says 'The compute_all_applicable_attributes method has been deprecated.'
[gitmo/Mouse.git] / t / 000-recipes / 002_schwartz_tutorial.t
CommitLineData
51212d90 1#!/usr/bin/perl
2
3# This adapted from the tutorial here:
4# http://www.stonehenge.com/merlyn/LinuxMag/col94.html
5# The Moose is Flying (part 1)'
6# Using Mouse, instead
7
8# use feature ':5.10';
9
10
11use strict;
12use warnings;
13
14# functions to capture the output of the tutorial
15our $DUMMY_STDOUT = "";
16sub dprint { $DUMMY_STDOUT .= join "", @_ };
17sub stdout { my $stdout = $DUMMY_STDOUT; $DUMMY_STDOUT = ""; return $stdout }
18sub say { ::dprint $_, "\n" for @_ }
19
20######################################################################
21# This is the tutorial, as posted by Heikki Lehvaslaiho in Mouse's RT
22# ticket #42992, except with print and say modified to use the above.
23
24package Animal;
25use Mouse::Role;
26has 'name' => (is => 'rw');
27sub speak {
28 my $self = shift;
29 ::dprint $self->name, " goes ", $self->sound, "\n";
30}
31requires 'sound';
32has 'color' => (is => 'rw', default => sub { shift->default_color });
33requires 'default_color';
34no Mouse::Role;
351;
36
37## Cow.pm:
38package Cow;
39use Mouse;
40with 'Animal';
41sub default_color { 'spotted' }
42sub sound { 'moooooo' }
43no Mouse;
441;
45## Horse.pm:
46package Horse;
47use Mouse;
48with 'Animal';
49sub default_color { 'brown' }
50sub sound { 'neigh' }
51no Mouse;
521;
53## Sheep.pm:
54package Sheep;
55use Mouse;
56with 'Animal';
57sub default_color { 'black' }
58sub sound { 'baaaah' }
59no Mouse;
601;
61
62package MouseA;
63use Mouse;
64with 'Animal';
65sub default_color { 'white' }
66sub sound { 'squeak' }
67after 'speak' => sub {
68 ::dprint "[but you can barely hear it!]\n";
69};
70before 'speak' => sub {
71 ::dprint "[Ahem]\n";
72};
73no Mouse;
741;
75
76
77
78package Racer;
79use Mouse::Role;
80has $_ => (is => 'rw', default => 0)
81 foreach qw(wins places shows losses);
82sub won { my $self = shift; $self->wins($self->wins + 1) }
83sub placed { my $self = shift; $self->places($self->places + 1) }
84sub showed { my $self = shift; $self->shows($self->shows + 1) }
85sub lost { my $self = shift; $self->losses($self->losses + 1) }
86sub standings {
87 my $self = shift;
88 join ", ", map { $self->$_ . " $_" } qw(wins places shows losses);
89}
90no Mouse::Role;
911;
92
93
94
95# To create the race horse, we just mix a horse with a racer:
96
97package RaceHorse;
98use Mouse;
99extends 'Horse';
100with 'Racer';
101no Mouse;
1021;
103
104
105######################################################################
106# Now the tests
107package main;
108use Test::More tests => 5;
109
110#use Horse;
111my $talking = Horse->new(name => 'Mr. Ed');
112say $talking->name; # prints Mr. Ed
113is stdout, "Mr. Ed\n";
114$talking->color("grey"); # sets the color
115$talking->speak; # says "Mr. Ed goes neigh"
116
117TODO: { local $TODO = "this currently fails because of a bug in Class::Method::Modifiers (see RT #42992)";
118is stdout, <<EXPECTED;
119Mr. Ed goes neigh
120EXPECTED
121
122
123#use Sheep;
124my $baab = Sheep->new(color => 'white', name => 'Baab');
125$baab->speak; # prints "Baab goes baaaah"
126is stdout, <<EXPECTED;
127Baab goes baaaah
128EXPECTED
129 }
130
131#use MouseA
132my $mickey = MouseA->new(name => 'Mickey');
133$mickey->speak;
134is stdout, <<EXPECTED;
135[Ahem]
136Mickey goes squeak
137[but you can barely hear it!]
138EXPECTED
139
140#use RaceHorse;
141my $s = RaceHorse->new(name => 'Seattle Slew');
142$s->won; $s->won; $s->won; $s->placed; $s->lost; # run some races
143::dprint $s->standings, "\n"; # 3 wins, 1 places, 0 shows, 1 losses
144is stdout, <<EXPECTED;
1453 wins, 1 places, 0 shows, 1 losses
146EXPECTED
147