# Your tasks ...
#
-# You are going to make our Person and Employee classes capable of
-# outputting an XML document describing the object.
+# First, we will create a set of three new classes to make use of the
+# augment method modifier. The class hierarchy will look like this:
#
-# The document will contain a tag and value for each attribute.
+# Document
+# |
+# Report
+# |
+# TPSReport
#
-# You will use method modifiers and roles to achieve this.
+# The Document class should have two read-only attributes: "title" and
+# "author".
#
-# Start by creating a new role, OutputsXML.
+# The Report class should have one read-only attributes: "summary".
#
-# This role should require an "as_xml" method in the classes which
-# consume it.
+# Finally, the TPSReport class should have three read-only attributes:
+# "t", "p", and "s".
#
-# This role should also use an around modifier on the as_xml method in
-# order to make sure the document is well-formed XML.
+# The goal is to produce a report that looks this:
#
-# This document will look something like this:
+# $title
#
-# <?xml version="1.0" encoding="UTF-8"?>
-# <Person>
-# <first_name>Joe</first_name>
-# <last_name>Smith</last_name>
-# </Person>
+# $summary
#
-# Use the role to create the xml declaration (the first line) and the
-# container tags (<person> or <employee>)
+# t: $t
+# p: $p
+# s: $s
#
-# The classes should return a list strings. Each string should be a
-# tagged value for an attribute. For consistency, return the
-# attributes in sorted order.
+# Written by $author
#
-# ( '<first_name>Joe</first_name>', '<last_name>Smith</last_name>' )
+# This report will be a string returned by the Document->output
+# method.
#
-# If an attribute is empty, just output an empty tag (<foo></foo>).
+# Don't worry too much about how many newlines separate each item (as
+# long as it's at least one). The test does a little massaging to make
+# this more forgiving.
#
-# Use an augment modifier in the Person and Employee classes to allow
-# Employee to return just its own attributes.
+# Use augment method modifiers in Report and TPSReport to "inject" the
+# relevant content, while Document will output the $title and $author.
use strict;
use warnings;
use MooseClass::Tests;
-use Person;
-use Employee;
+use TPSReport;
MooseClass::Tests::tests04();
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
- no_droppings('OutputsXML');
+ has_meta('Document');
+ has_meta('Report');
+ has_meta('TPSReport');
- does_role( 'Person', 'OutputsXML' );
+ no_droppings('Document');
+ no_droppings('Report');
+ no_droppings('TPSReport');
+
+ has_ro_attr( 'Document', $_ ) for qw( title author );
+ has_ro_attr( 'Report', 'summary' );
+ has_ro_attr( 'TPSReport', $_ ) for qw( t p s );
+
+ has_method( 'Document', 'output' );
+ has_augmented_method( 'Report', 'output' );
+ has_augmented_method( 'TPSReport', 'output' );
}
- ok( scalar OutputsXML->meta->get_around_method_modifiers('as_xml'),
- 'OutputsXML has an around modifier for as_xml' );
+ my $tps = TPSReport->new(
+ title => 'That TPS Report',
+ author => 'Peter Gibbons (for Bill Lumberg)',
+ summary => 'I celebrate his whole collection!',
+ t => 'PC Load Letter',
+ p => 'Swingline',
+ s => 'flair!',
+ );
- isa_ok( Employee->meta->get_method('as_xml'),
- 'Moose::Meta::Method::Augmented', 'as_xml is augmented in Employee' );
+ my $output = $tps->output;
+ $output =~ s/\n\n+/\n/g;
- person04();
- employee04();
+ is( $output, <<'EOF', 'output returns expected report' );
+That TPS Report
+I celebrate his whole collection!
+t: PC Load Letter
+p: Swingline
+s: flair!
+Written by Peter Gibbons (for Bill Lumberg)
+EOF
}
sub tests06 {
isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
}
+sub has_augmented_method {
+ my $class = shift;
+ my $name = shift;
+
+ my $articled = A($name);
+ ok( $class->meta->has_method($name), "$class has $articled method" );
+
+ my $meth = $class->meta->get_method($name);
+ isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
+}
+
sub no_droppings {
my $class = shift;
'salary is calculated from salary_level, and salary passed to constructor is ignored' );
}
-
-sub person04 {
- my $person = Person->new(
- first_name => 'Bilbo',
- last_name => 'Baggins',
- );
-
- my $xml = <<'EOF';
-<?xml version="1.0" encoding="UTF-8"?>
-<Person>
-<first_name>Bilbo</first_name>
-<last_name>Baggins</last_name>
-<title></title>
-</Person>
-EOF
-
- is( $person->as_xml, $xml, 'Person outputs expected XML' );
-}
-
-sub employee04 {
- my $employee = Employee->new(
- first_name => 'Jimmy',
- last_name => 'Foo',
- ssn => '123-99-4567',
- salary_level => 3,
- );
-
- my $xml = <<'EOF';
-<?xml version="1.0" encoding="UTF-8"?>
-<Employee>
-<first_name>Jimmy</first_name>
-<last_name>Foo</last_name>
-<title>Worker</title>
-<salary>30000</salary>
-<salary_level>3</salary_level>
-<ssn>123-99-4567</ssn>
-</Employee>
-EOF
-
- is( $employee->as_xml, $xml, 'Employee outputs expected XML' );
-}
-
sub person06 {
my $person = Person->new(
first_name => 'Bilbo',