Rename t/000-recipes to t/000_recipes
[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
51212d90 15# functions to capture the output of the tutorial
16our $DUMMY_STDOUT = "";
17sub dprint { $DUMMY_STDOUT .= join "", @_ };
18sub stdout { my $stdout = $DUMMY_STDOUT; $DUMMY_STDOUT = ""; return $stdout }
19sub say { ::dprint $_, "\n" for @_ }
20
21######################################################################
22# This is the tutorial, as posted by Heikki Lehvaslaiho in Mouse's RT
23# ticket #42992, except with print and say modified to use the above.
24
25package Animal;
26use Mouse::Role;
27has 'name' => (is => 'rw');
28sub speak {
29 my $self = shift;
30 ::dprint $self->name, " goes ", $self->sound, "\n";
31}
32requires 'sound';
33has 'color' => (is => 'rw', default => sub { shift->default_color });
34requires 'default_color';
35no Mouse::Role;
361;
37
38## Cow.pm:
39package Cow;
40use Mouse;
41with 'Animal';
42sub default_color { 'spotted' }
43sub sound { 'moooooo' }
44no Mouse;
451;
46## Horse.pm:
47package Horse;
48use Mouse;
49with 'Animal';
50sub default_color { 'brown' }
51sub sound { 'neigh' }
52no Mouse;
531;
54## Sheep.pm:
55package Sheep;
56use Mouse;
57with 'Animal';
58sub default_color { 'black' }
59sub sound { 'baaaah' }
60no Mouse;
611;
62
63package MouseA;
64use Mouse;
65with 'Animal';
66sub default_color { 'white' }
67sub sound { 'squeak' }
68after 'speak' => sub {
69 ::dprint "[but you can barely hear it!]\n";
70};
71before 'speak' => sub {
72 ::dprint "[Ahem]\n";
73};
74no Mouse;
751;
76
77
78
79package Racer;
80use Mouse::Role;
81has $_ => (is => 'rw', default => 0)
82 foreach qw(wins places shows losses);
83sub won { my $self = shift; $self->wins($self->wins + 1) }
84sub placed { my $self = shift; $self->places($self->places + 1) }
85sub showed { my $self = shift; $self->shows($self->shows + 1) }
86sub lost { my $self = shift; $self->losses($self->losses + 1) }
87sub standings {
88 my $self = shift;
89 join ", ", map { $self->$_ . " $_" } qw(wins places shows losses);
90}
91no Mouse::Role;
921;
93
94
95
96# To create the race horse, we just mix a horse with a racer:
97
98package RaceHorse;
99use Mouse;
100extends 'Horse';
101with 'Racer';
102no Mouse;
1031;
104
105
106######################################################################
107# Now the tests
108package main;
49a56bba 109plan tests => 5;
51212d90 110
111#use Horse;
112my $talking = Horse->new(name => 'Mr. Ed');
113say $talking->name; # prints Mr. Ed
114is stdout, "Mr. Ed\n";
115$talking->color("grey"); # sets the color
116$talking->speak; # says "Mr. Ed goes neigh"
117
51212d90 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
51212d90 129
130#use MouseA
131my $mickey = MouseA->new(name => 'Mickey');
132$mickey->speak;
133is stdout, <<EXPECTED;
134[Ahem]
135Mickey goes squeak
136[but you can barely hear it!]
137EXPECTED
138
139#use RaceHorse;
140my $s = RaceHorse->new(name => 'Seattle Slew');
141$s->won; $s->won; $s->won; $s->placed; $s->lost; # run some races
142::dprint $s->standings, "\n"; # 3 wins, 1 places, 0 shows, 1 losses
143is stdout, <<EXPECTED;
1443 wins, 1 places, 0 shows, 1 losses
145EXPECTED
146