Skip to content

Commit

Permalink
fixed a few small namespace handling bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 19, 2012
1 parent b3a8efc commit 619e9c7
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 72 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -10,6 +10,8 @@
- Improved documentation.
- Improved tests.
- Fixed json_has method in Test::Mojo.
- Fixed small class_to_file bug.
- Fixed a few small namespace handling bugs.

3.31 2012-08-15
- Added accept_charset, accept_encoding, content_encoding, origin and
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Loader.pm
Expand Up @@ -42,7 +42,7 @@ sub search {
# Check all directories
my (@modules, %found);
for my $directory (@INC) {
next unless -d (my $path = catdir $directory, split(/::/, $namespace));
next unless -d (my $path = catdir $directory, split(/::|'/, $namespace));

# List "*.pm" files in directory
opendir(my $dir, $path);
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -60,7 +60,7 @@ sub camelize {

sub class_to_file {
my $class = shift;
$class =~ s/:://g;
$class =~ s/::|'//g;
$class =~ s/([A-Z])([A-Z]*)/$1.lc($2)/ge;
return decamelize($class);
}
Expand Down Expand Up @@ -457,6 +457,7 @@ Convert a class name to a file.
Convert class name to path.
Foo::Bar -> Foo/Bar.pm
FooBar -> FooBar.pm
=head2 C<decamelize>
Expand Down
46 changes: 23 additions & 23 deletions t/mojo/base.t
Expand Up @@ -5,10 +5,10 @@ use Test::More tests => 415;
use FindBin;
use lib "$FindBin::Bin/lib";

package BaseTest;
package Mojo::BaseTest;
use Mojo::Base -strict;

use base 'BaseTest::Base2';
use base 'Mojo::BaseTest::Base2';

# "When I first heard that Marge was joining the police academy,
# I thought it would be fun and zany, like that movie Spaceballs.
Expand All @@ -17,40 +17,40 @@ use base 'BaseTest::Base2';
__PACKAGE__->attr(heads => 1);
__PACKAGE__->attr('name');

package BaseTestTest;
use Mojo::Base 'BaseTest';
package Mojo::BaseTestTest;
use Mojo::Base 'Mojo::BaseTest';

package BaseTestTestTest;
use Mojo::Base 'BaseTestTest';
package Mojo::BaseTestTestTest;
use Mojo::Base "Mojo'BaseTestTest";

package main;

use Mojo::Base;
use BaseTest::Base1;
use BaseTest::Base2;
use BaseTest::Base3;
use Mojo::BaseTest::Base1;
use Mojo::BaseTest::Base2;
use Mojo::BaseTest::Base3;

# Basic functionality
my @monkeys;
for my $i (1 .. 50) {
$monkeys[$i] = BaseTest->new;
$monkeys[$i] = Mojo::BaseTest->new;
$monkeys[$i]->bananas($i);
is $monkeys[$i]->bananas, $i, 'right attribute value';
}
for my $i (51 .. 100) {
$monkeys[$i] = BaseTestTest->new(bananas => $i);
$monkeys[$i] = Mojo::BaseTestTest->new(bananas => $i);
is $monkeys[$i]->bananas, $i, 'right attribute value';
}

# Instance method
my $monkey = BaseTestTestTest->new;
my $monkey = Mojo::BaseTestTestTest->new;
$monkey->attr('mojo');
$monkey->mojo(23);
is $monkey->mojo, 23, 'monkey has mojo';
ok !BaseTestTest->can('mojo'), 'base class does not have mojo';
ok(BaseTestTest->can('heads'), 'base class has heads');
ok !BaseTest->can('mojo'), 'base class does not have mojo';
ok(BaseTest->can('heads'), 'base class has heads');
ok !Mojo::BaseTestTest->can('mojo'), 'base class does not have mojo';
ok(Mojo::BaseTestTest->can('heads'), 'base class has heads');
ok !Mojo::BaseTest->can('mojo'), 'base class does not have mojo';
ok(Mojo::BaseTest->can('heads'), 'base class has heads');

# Default value defined but false
my $m = $monkeys[1];
Expand All @@ -63,8 +63,8 @@ is $m->coconuts, 5, 'right attribute value';
my $y = 1;
for my $i (101 .. 150) {
$y = !$y;
$monkeys[$i] = BaseTest->new;
isa_ok $monkeys[$i]->name('foobarbaz'), 'BaseTest',
$monkeys[$i] = Mojo::BaseTest->new;
isa_ok $monkeys[$i]->name('foobarbaz'), 'Mojo::BaseTest',
'attribute value has right class';
$monkeys[$i]->heads('3') if $y;
$y
Expand All @@ -74,31 +74,31 @@ for my $i (101 .. 150) {

# Chained attributes and coderef default value support
for my $i (151 .. 200) {
$monkeys[$i] = BaseTest->new;
$monkeys[$i] = Mojo::BaseTest->new;
is $monkeys[$i]->ears, 2, 'right attribute value';
is $monkeys[$i]->ears(6)->ears, 6, 'right chained attribute value';
is $monkeys[$i]->eyes, 2, 'right attribute value';
is $monkeys[$i]->eyes(6)->eyes, 6, 'right chained attribute value';
}

# Tap into chain
$monkey = BaseTest->new;
$monkey = Mojo::BaseTest->new;
is $monkey->tap(sub { $_->name('foo') })->name, 'foo', 'right attribute value';
is $monkey->tap(sub { shift->name('bar')->name })->name, 'bar',
'right attribute value';

# Inherit -base flag
$monkey = BaseTest::Base3->new(evil => 1);
$monkey = Mojo::BaseTest::Base3->new(evil => 1);
is $monkey->evil, 1, 'monkey is evil';
is $monkey->bananas, undef, 'monkey has no bananas';
$monkey->bananas(3);
is $monkey->bananas, 3, 'monkey has 3 bananas';

# Exceptions
eval { BaseTest->attr(foo => []) };
eval { Mojo::BaseTest->attr(foo => []) };
like $@, qr/Default has to be a code reference or constant value/,
'right error';
eval { BaseTest->attr(23) };
eval { Mojo::BaseTest->attr(23) };
like $@, qr/Attribute "23" invalid/, 'right error';

1;
12 changes: 6 additions & 6 deletions t/mojo/home.t
Expand Up @@ -52,16 +52,16 @@ is $home->rel_dir('foo/bar'), catdir(splitdir($FindBin::Bin), 'foo', 'bar'),

# List files
is first(sub {/Base1\.pm$/}, @{$home->list_files('lib')}),
'BaseTest/Base1.pm', 'right result';
'Mojo/BaseTest/Base1.pm', 'right result';
is first(sub {/Base2\.pm$/}, @{$home->list_files('lib')}),
'BaseTest/Base2.pm', 'right result';
'Mojo/BaseTest/Base2.pm', 'right result';
is first(sub {/Base3\.pm$/}, @{$home->list_files('lib')}),
'BaseTest/Base3.pm', 'right result';
'Mojo/BaseTest/Base3.pm', 'right result';

# Slurp files
like $home->slurp_rel_file('lib/BaseTest/Base1.pm'), qr/Base1/,
like $home->slurp_rel_file('lib/Mojo/BaseTest/Base1.pm'), qr/Base1/,
'right content';
like $home->slurp_rel_file('lib/BaseTest/Base2.pm'), qr/Base2/,
like $home->slurp_rel_file('lib/Mojo/BaseTest/Base2.pm'), qr/Base2/,
'right content';
like $home->slurp_rel_file('lib/BaseTest/Base3.pm'), qr/Base3/,
like $home->slurp_rel_file('lib/Mojo/BaseTest/Base3.pm'), qr/Base3/,
'right content';
@@ -1,4 +1,4 @@
package BaseTest::Base1;
package Mojo::BaseTest::Base1;
use Mojo::Base -base;

# "Okay folks, show's over. Nothing to see here, show's... Oh my god!
Expand Down
@@ -1,5 +1,5 @@
package BaseTest::Base2;
use Mojo::Base 'BaseTest::Base1';
package Mojo::BaseTest::Base2;
use Mojo::Base 'Mojo::BaseTest::Base1';

# "Hey, I asked for ketchup! I'm eatin' salad here!"
has [qw(ears eyes)] => sub {2};
Expand Down
@@ -1,5 +1,5 @@
package BaseTest::Base3;
use BaseTest::Base1 -base;
package Mojo::BaseTest::Base3;
use Mojo::BaseTest::Base1 -base;

# "I've done everything the Bible says,
# even the stuff that contradicts the other stuff!"
Expand Down
@@ -1,4 +1,4 @@
package LoaderException;
package Mojo::LoaderException;

use Mojo::Base -base;

Expand Down
@@ -1,21 +1,21 @@
package LoaderException2;
package Mojo::LoaderException2;
use Mojo::Base -strict;

LoaderException2_2::throw_error();
Mojo::LoaderException2_2::throw_error();

1;

package LoaderException2_2;
package Mojo::LoaderException2_2;

use Carp 'croak';

sub throw_error {
eval { LoaderException2_3::throw_error() };
eval { Mojo::LoaderException2_3::throw_error() };
croak $@ if $@;
}

# "Shoplifting is a victimless crime. Like punching someone in the dark."
package LoaderException2_3;
package Mojo::LoaderException2_3;

use Carp 'croak';

Expand Down
@@ -1,4 +1,4 @@
package LoaderTest::A;
package Mojo::LoaderTest::A;

# "I have purchased the Springfield YMCA.
# I plan to tear it down and turn the land into a nature preserve.
Expand Down
@@ -1,4 +1,4 @@
package LoaderTest::B;
package Mojo::LoaderTest::B;

# "The Internet King? I wonder if he could provide faster nudity..."
use Mojo::Base -base;
Expand Down
@@ -1,4 +1,4 @@
package LoaderTest::C;
package Mojo::LoaderTest::C;

# "We started out like Romeo and Juliet, but it ended up in tragedy."
use Mojo::Base -base;
Expand Down
41 changes: 22 additions & 19 deletions t/mojo/loader.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;
# Disable libev
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More tests => 55;
use Test::More tests => 57;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand All @@ -18,7 +18,7 @@ ok !$loader->load('B');
ok !!UNIVERSAL::can(B => 'svref_2object');

# Exception
my $e = $loader->load('LoaderException');
my $e = $loader->load('Mojo::LoaderException');
isa_ok $e, 'Mojo::Exception', 'right object';
like $e->message, qr/Missing right curly/, 'right message';
is $e->lines_before->[0][0], 5, 'right line';
Expand All @@ -37,39 +37,42 @@ like "$e", qr/Missing right curly/, 'right message';

# Complicated exception
$loader = Mojo::Loader->new;
$e = $loader->load('LoaderException2');
$e = $loader->load('Mojo::LoaderException2');
isa_ok $e, 'Mojo::Exception', 'right object';
like $e->message, qr/Exception/, 'right message';
is $e->lines_before->[0][0], 1, 'right line';
is $e->lines_before->[0][1], 'package LoaderException2;', 'right value';
is $e->lines_before->[1][0], 2, 'right line';
is $e->lines_before->[1][1], 'use Mojo::Base -strict;', 'right value';
is $e->lines_before->[2][0], 3, 'right line';
is $e->lines_before->[2][1], '', 'right value';
is $e->lines_before->[0][0], 1, 'right line';
is $e->lines_before->[0][1], 'package Mojo::LoaderException2;', 'right value';
is $e->lines_before->[1][0], 2, 'right line';
is $e->lines_before->[1][1], 'use Mojo::Base -strict;', 'right value';
is $e->lines_before->[2][0], 3, 'right line';
is $e->lines_before->[2][1], '', 'right value';
is $e->line->[0], 4, 'right line';
is $e->line->[1], 'LoaderException2_2::throw_error();', 'right value';
is $e->line->[1], 'Mojo::LoaderException2_2::throw_error();', 'right value';
is $e->lines_after->[0][0], 5, 'right line';
is $e->lines_after->[0][1], '', 'right value';
is $e->lines_after->[1][0], 6, 'right line';
is $e->lines_after->[1][1], '1;', 'right value';
like "$e", qr/Exception/, 'right message';

$loader = Mojo::Loader->new;
my $modules = $loader->search('LoaderTest');
my @modules = sort @$modules;

# Search
is_deeply \@modules, [qw(LoaderTest::A LoaderTest::B LoaderTest::C)],
$loader = Mojo::Loader->new;
my @modules = sort @{$loader->search('Mojo::LoaderTest')};
is_deeply \@modules,
[qw(Mojo::LoaderTest::A Mojo::LoaderTest::B Mojo::LoaderTest::C)],
'found the right modules';
is_deeply [sort @{$loader->search("Mojo'LoaderTest")}],
[qw(Mojo'LoaderTest::A Mojo'LoaderTest::B Mojo'LoaderTest::C)],
'found the right modules';

# Load
ok !$loader->load("Mojo'LoaderTest::A"), 'loaded successfully';
ok !!Mojo::LoaderTest::A->can('new'), 'loaded successfully';
$loader->load($_) for @modules;
ok !!LoaderTest::A->can('new'), 'loaded successfully';
ok !!LoaderTest::B->can('new'), 'loaded successfully';
ok !!LoaderTest::C->can('new'), 'loaded successfully';
ok !!Mojo::LoaderTest::B->can('new'), 'loaded successfully';
ok !!Mojo::LoaderTest::C->can('new'), 'loaded successfully';

# Class does not exist
is $loader->load('LoaderTest'), 1, 'nothing to load';
is $loader->load('Mojo::LoaderTest'), 1, 'nothing to load';

# Invalid class
is $loader->load('Mojolicious/Lite'), 1, 'nothing to load';
Expand Down
18 changes: 10 additions & 8 deletions t/mojo/util.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 146;
use Test::More tests => 148;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
Expand Down Expand Up @@ -31,13 +31,15 @@ is decamelize('FooBB'), 'foo_b_b', 'right decamelized result';
is decamelize('Foo::BB'), 'foo-b_b', 'right decamelized result';

# class_to_file
is class_to_file('Foo::Bar'), 'foo_bar', 'right file';
is class_to_file('FooBar'), 'foo_bar', 'right file';
is class_to_file('FOOBar'), 'foobar', 'right file';
is class_to_file('FOOBAR'), 'foobar', 'right file';
is class_to_file('FOO::Bar'), 'foobar', 'right file';
is class_to_file('FooBAR'), 'foo_bar', 'right file';
is class_to_file('Foo::BAR'), 'foo_bar', 'right file';
is class_to_file('Foo::Bar'), 'foo_bar', 'right file';
is class_to_file('FooBar'), 'foo_bar', 'right file';
is class_to_file('FOOBar'), 'foobar', 'right file';
is class_to_file('FOOBAR'), 'foobar', 'right file';
is class_to_file('FOO::Bar'), 'foobar', 'right file';
is class_to_file('FooBAR'), 'foo_bar', 'right file';
is class_to_file('Foo::BAR'), 'foo_bar', 'right file';
is class_to_file("Foo'BAR"), 'foo_bar', 'right file';
is class_to_file("Foo'Bar::Baz"), 'foo_bar_baz', 'right file';

# class_to_path
is class_to_path('Foo::Bar'), 'Foo/Bar.pm', 'right path';
Expand Down

0 comments on commit 619e9c7

Please sign in to comment.