Add what does moose stand for section back to docs
[gitmo/Moose.git] / t / metaclasses / create_anon_with_required_attr.t
CommitLineData
310ba883 1#!/usr/bin/perl
2
6b48e9a3 3# this functionality may be pushing toward parametric roles/classes
4# it's off in a corner and may not be that important
5
310ba883 6use strict;
7use warnings;
8
a28e50e4 9use Test::More;
b10dde3a 10use Test::Fatal;
310ba883 11
12{
13 package HasFoo;
14 use Moose::Role;
15 has 'foo' => (
16 is => 'ro',
17 isa => 'Str',
18 required => 1,
19 );
20
21}
22
23{
24 package My::Metaclass;
25 use Moose;
26 extends 'Moose::Meta::Class';
27 with 'HasFoo';
28}
29
30package main;
31
32my $anon;
b10dde3a 33is( exception {
310ba883 34 $anon = My::Metaclass->create_anon_class( foo => 'this' );
b10dde3a 35}, undef, 'create anon class with required attr' );
310ba883 36isa_ok( $anon, 'My::Metaclass' );
37cmp_ok( $anon->foo, 'eq', 'this', 'foo is this' );
b10dde3a 38isnt( exception {
6b48e9a3 39 $anon = My::Metaclass->create_anon_class();
b10dde3a 40}, undef, 'failed to create anon class without required attr' );
6b48e9a3 41
42my $meta;
b10dde3a 43is( exception {
6b48e9a3 44 $meta
45 = My::Metaclass->initialize( 'Class::Name1' => ( foo => 'that' ) );
b10dde3a 46}, undef, 'initialize a class with required attr' );
6b48e9a3 47isa_ok( $meta, 'My::Metaclass' );
48cmp_ok( $meta->foo, 'eq', 'that', 'foo is that' );
49cmp_ok( $meta->name, 'eq', 'Class::Name1', 'for the correct class' );
b10dde3a 50isnt( exception {
6b48e9a3 51 $meta
52 = My::Metaclass->initialize( 'Class::Name2' );
b10dde3a 53}, undef, 'failed to initialize a class without required attr' );
6b48e9a3 54
b10dde3a 55is( exception {
6b48e9a3 56 eval qq{
57 package Class::Name3;
58 use metaclass 'My::Metaclass' => (
59 foo => 'another',
60 );
61 use Moose;
62 };
63 die $@ if $@;
b10dde3a 64}, undef, 'use metaclass with required attr' );
6b48e9a3 65$meta = Class::Name3->meta;
66isa_ok( $meta, 'My::Metaclass' );
67cmp_ok( $meta->foo, 'eq', 'another', 'foo is another' );
68cmp_ok( $meta->name, 'eq', 'Class::Name3', 'for the correct class' );
b10dde3a 69isnt( exception {
6b48e9a3 70 eval qq{
71 package Class::Name4;
72 use metaclass 'My::Metaclass';
73 use Moose;
74 };
75 die $@ if $@;
b10dde3a 76}, undef, 'failed to use metaclass without required attr' );
6b48e9a3 77
78
79# how do we pass a required attribute to -traits?
b10dde3a 80isnt( exception {
6b48e9a3 81 eval qq{
82 package Class::Name5;
83 use Moose -traits => 'HasFoo';
84 };
85 die $@ if $@;
b10dde3a 86}, undef, 'failed to use trait without required attr' );
310ba883 87
a28e50e4 88done_testing;