tckect #42992: Method modifiers affect all classes in the whole inheritance tree
[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;
49a56bba 13use Test::More;
14
15BEGIN {
16 plan skip_all =>
17 "This test requires Class::Method::Modifiers or Class::Method::Modifiers::Fast"
18 unless eval {
19 require Class::Method::Modifiers::Fast;
20 } or eval {
21 require Class::Method::Modifiers;
22 };
23}
51212d90 24
25# functions to capture the output of the tutorial
26our $DUMMY_STDOUT = "";
27sub dprint { $DUMMY_STDOUT .= join "", @_ };
28sub stdout { my $stdout = $DUMMY_STDOUT; $DUMMY_STDOUT = ""; return $stdout }
29sub say { ::dprint $_, "\n" for @_ }
30
31######################################################################
32# This is the tutorial, as posted by Heikki Lehvaslaiho in Mouse's RT
33# ticket #42992, except with print and say modified to use the above.
34
35package Animal;
36use Mouse::Role;
37has 'name' => (is => 'rw');
38sub speak {
39 my $self = shift;
40 ::dprint $self->name, " goes ", $self->sound, "\n";
41}
42requires 'sound';
43has 'color' => (is => 'rw', default => sub { shift->default_color });
44requires 'default_color';
45no Mouse::Role;
461;
47
48## Cow.pm:
49package Cow;
50use Mouse;
51with 'Animal';
52sub default_color { 'spotted' }
53sub sound { 'moooooo' }
54no Mouse;
551;
56## Horse.pm:
57package Horse;
58use Mouse;
59with 'Animal';
60sub default_color { 'brown' }
61sub sound { 'neigh' }
62no Mouse;
631;
64## Sheep.pm:
65package Sheep;
66use Mouse;
67with 'Animal';
68sub default_color { 'black' }
69sub sound { 'baaaah' }
70no Mouse;
711;
72
73package MouseA;
74use Mouse;
75with 'Animal';
76sub default_color { 'white' }
77sub sound { 'squeak' }
78after 'speak' => sub {
79 ::dprint "[but you can barely hear it!]\n";
80};
81before 'speak' => sub {
82 ::dprint "[Ahem]\n";
83};
84no Mouse;
851;
86
87
88
89package Racer;
90use Mouse::Role;
91has $_ => (is => 'rw', default => 0)
92 foreach qw(wins places shows losses);
93sub won { my $self = shift; $self->wins($self->wins + 1) }
94sub placed { my $self = shift; $self->places($self->places + 1) }
95sub showed { my $self = shift; $self->shows($self->shows + 1) }
96sub lost { my $self = shift; $self->losses($self->losses + 1) }
97sub standings {
98 my $self = shift;
99 join ", ", map { $self->$_ . " $_" } qw(wins places shows losses);
100}
101no Mouse::Role;
1021;
103
104
105
106# To create the race horse, we just mix a horse with a racer:
107
108package RaceHorse;
109use Mouse;
110extends 'Horse';
111with 'Racer';
112no Mouse;
1131;
114
115
116######################################################################
117# Now the tests
118package main;
49a56bba 119plan tests => 5;
51212d90 120
121#use Horse;
122my $talking = Horse->new(name => 'Mr. Ed');
123say $talking->name; # prints Mr. Ed
124is stdout, "Mr. Ed\n";
125$talking->color("grey"); # sets the color
126$talking->speak; # says "Mr. Ed goes neigh"
127
51212d90 128is stdout, <<EXPECTED;
129Mr. Ed goes neigh
130EXPECTED
131
132
133#use Sheep;
134my $baab = Sheep->new(color => 'white', name => 'Baab');
135$baab->speak; # prints "Baab goes baaaah"
136is stdout, <<EXPECTED;
137Baab goes baaaah
138EXPECTED
51212d90 139
140#use MouseA
141my $mickey = MouseA->new(name => 'Mickey');
142$mickey->speak;
143is stdout, <<EXPECTED;
144[Ahem]
145Mickey goes squeak
146[but you can barely hear it!]
147EXPECTED
148
149#use RaceHorse;
150my $s = RaceHorse->new(name => 'Seattle Slew');
151$s->won; $s->won; $s->won; $s->placed; $s->lost; # run some races
152::dprint $s->standings, "\n"; # 3 wins, 1 places, 0 shows, 1 losses
153is stdout, <<EXPECTED;
1543 wins, 1 places, 0 shows, 1 losses
155EXPECTED
156