1 # 2 initial tests, and 6 per component in the loop below
2 # (do not forget to update the number of components in test 3 as well)
3 use Test::More tests => 2 + 6 * 24;
11 my $libdir = 'test_trash';
12 unshift(@INC, $libdir);
14 my $appclass = 'TestComponents';
16 { type => 'Controller', prefix => 'C', name => 'Bar' },
17 { type => 'Controller', prefix => 'C', name => 'Foo::Bar' },
18 { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Bar' },
19 { type => 'Controller', prefix => 'C', name => 'Foo::Foo::Foo::Bar' },
20 { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Bar::Foo' },
21 { type => 'Controller', prefix => 'Controller', name => 'Bar::Bar::Foo' },
22 { type => 'Controller', prefix => 'Controller', name => 'Bar::Foo' },
23 { type => 'Controller', prefix => 'Controller', name => 'Foo' },
24 { type => 'Model', prefix => 'M', name => 'Bar' },
25 { type => 'Model', prefix => 'M', name => 'Foo::Bar' },
26 { type => 'Model', prefix => 'M', name => 'Foo::Foo::Bar' },
27 { type => 'Model', prefix => 'M', name => 'Foo::Foo::Foo::Bar' },
28 { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Bar::Foo' },
29 { type => 'Model', prefix => 'Model', name => 'Bar::Bar::Foo' },
30 { type => 'Model', prefix => 'Model', name => 'Bar::Foo' },
31 { type => 'Model', prefix => 'Model', name => 'Foo' },
32 { type => 'View', prefix => 'V', name => 'Bar' },
33 { type => 'View', prefix => 'V', name => 'Foo::Bar' },
34 { type => 'View', prefix => 'V', name => 'Foo::Foo::Bar' },
35 { type => 'View', prefix => 'V', name => 'Foo::Foo::Foo::Bar' },
36 { type => 'View', prefix => 'View', name => 'Bar::Bar::Bar::Foo' },
37 { type => 'View', prefix => 'View', name => 'Bar::Bar::Foo' },
38 { type => 'View', prefix => 'View', name => 'Bar::Foo' },
39 { type => 'View', prefix => 'View', name => 'Foo' },
42 sub make_component_file {
43 my ($type, $prefix, $name) = @_;
45 my $compbase = "Catalyst::${type}";
46 my $fullname = "${appclass}::${prefix}::${name}";
47 my @namedirs = split(/::/, $name);
48 my $name_final = pop(@namedirs);
49 my @dir_list = ($libdir, $appclass, $prefix, @namedirs);
50 my $dir_ux = join(q{/}, @dir_list);
51 my $dir = File::Spec->catdir(@dir_list);
52 my $file = File::Spec->catfile($dir, $name_final . '.pm');
54 mkpath($dir_ux); # mkpath wants unix '/' seperators :p
55 open(my $fh, '>', $file) or die "Could not open file $file for writing: $!";
60 my \$self = shift->NEXT::COMPONENT(\@_);
62 *{\__PACKAGE__ . "::whoami"} = sub { return \__PACKAGE__; };
72 foreach my $component (@components) {
73 make_component_file($component->{type},
78 eval "package $appclass; use Catalyst; __PACKAGE__->setup";
80 can_ok( $appclass, 'components');
82 my $complist = $appclass->components;
84 # the +1 below is for the app class itself
85 is(scalar keys %$complist, 24+1, "Correct number of components loaded");
87 foreach (keys %$complist) {
89 # Skip the component which happens to be the app itself
90 next if $_ eq $appclass;
92 my $instance = $appclass->component($_);
93 isa_ok($instance, $_);
94 can_ok($instance, 'whoami');
95 is($instance->whoami, $_);
97 if($_ =~ /^${appclass}::(?:V|View)::(.*)/) {
99 isa_ok($instance, 'Catalyst::View');
100 can_ok($appclass->view($moniker), 'whoami');
101 is($appclass->view($moniker)->whoami, $_);
103 elsif($_ =~ /^${appclass}::(?:M|Model)::(.*)/) {
105 isa_ok($instance, 'Catalyst::Model');
106 can_ok($appclass->model($moniker), 'whoami');
107 is($appclass->model($moniker)->whoami, $_);
109 elsif($_ =~ /^${appclass}::(?:C|Controller)::(.*)/) {
111 isa_ok($instance, 'Catalyst::Controller');
112 can_ok($appclass->controller($moniker), 'whoami');
113 is($appclass->controller($moniker)->whoami, $_);
116 die "Something is wrong with this test, this should"
117 . " have been unreachable";