Skip to content

Commit

Permalink
[gsoc_spectest] t/packages -> t/spec/S10-packages/*.t and t/spec/pack…
Browse files Browse the repository at this point in the history
…ages/*.pm. Modified a bunch of tests.

packages used for testing are stored in t/spec/packages to avoid a hyphen in the name since hyphens are not legal package/variable names (without quoting them explicitly each time).


git-svn-id: http://svn.pugscode.org/pugs@21050 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information
Auzon authored and Auzon committed Jun 26, 2008
1 parent 3daa935 commit be85ac8
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 0 deletions.
4 changes: 4 additions & 0 deletions S10-packages/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
t/spec/S10-packages/README

The packages these tests use are stored in t/spec/packages to avoid the use of
a hyphen in the package name, which is not allowed without quoting.
32 changes: 32 additions & 0 deletions S10-packages/export.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use v6;

use Test;

plan 7;

# (Automatic s:g/::/$PATH_SEPARATOR_OF_CUR_OS/)++
use t::spec::packages::Export_PackB;

ok t::spec::packages::Export_PackB::does_export_work(),
"'is export' works correctly even when not exporting to Main (1)";

# t::spec::packages::Export_PackA::exported_foo should not have been exported into
# our namespace.
dies_ok { exported_foo() },
"'is export' works correctly even when not exporting to Main (2)";

{
use t::spec::packages::Export_PackC;
lives_ok { foo_packc() }, "lexical export works";
}
dies_ok { foo_packc() }, "lexical export is indeed lexical";


sub moose {
use t::spec::packages::Export_PackD;
is(this_gets_exported_lexically(), 'moose!', "lexical import survives pad regeneration")
}

moose();
moose();
moose();
11 changes: 11 additions & 0 deletions S10-packages/import.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use v6;

use Test;
plan 1;

if $?PUGS_BACKEND ne "BACKEND_PUGS" {
skip_rest "PIL2JS and PIL-Run do not support eval() yet.";
exit;
}

is(eval("use t::spec::packages::Import 'foo'; 123;"), 123, "import doesn't get called if it doesn't exist");
53 changes: 53 additions & 0 deletions S10-packages/require_and_use.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use v6;

use Test;

plan 18;
force_todo 1,2,4,5,7,9,11;

if $?PUGS_BACKEND ne "BACKEND_PUGS" {
skip_rest "PIL2JS and PIL-Run do not support eval() yet.";
exit;
}

my @tests = (
"t::spec::packages::RequireAndUse1", { $^a == 42 },
"t::spec::packages::RequireAndUse2", { $^a != 23 },
"t::spec::packages::RequireAndUse3", { $^a != 23 },
);

for @tests -> $mod, $expected_ret {

my @strings = (
"use $mod",
"require '{ $mod.split("::").join("/") ~ ".pm" }'",
);

for @strings -> $str {
diag $str;
my $retval = try { eval $str };

ok defined($retval) && $retval != -1 && $expected_ret($retval),
"require or use's return value was correct ({$str})";
# XXX: Keys of %*INC not yet fully decided (module name? module object?),
# IIRC.
ok defined(%*INC{$mod}) && %*INC{$mod} != -1 && $expected_ret(%*INC{$mod}),
"\%*INC was updated correctly ({$str})";
}
}

our $loaded = 0;
our $imported = 0;

eval q{use t::spec::packages::LoadCounter; 1} orelse die "error loading package: $!";
is($loaded, 1, "use loads a module");
is($imported, 1, "use calls &import");

eval q{use t::spec::packages::LoadCounter; 1} orelse die "error loading package: $!";
is($loaded, 1, "a second use doesn't load the module again");
is($imported, 2, "a second use does call &import again");

eval q{no t::spec::packages::LoadCounter; 1} orelse die "error no'ing package: $!";
is($loaded, 1, "&no doesn't load the module again");
is($imported, 1, "&no calls &unimport");

86 changes: 86 additions & 0 deletions S10-packages/scope.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use v6;

use Test;

# test that packages work. Note that the correspondance between type
# objects as obtained via the ::() syntax and packages is only hinted
# at in L<S10/Packages/or use the sigil-like>
plan 23;

# 4 different ways to be imported
# L<S10/Packages/A bare>
{
package Test1 {
sub ns { "Test1" }
sub pkg { $?PACKAGE }
sub test1_export is export { "export yourself!" }
}
package Test2 {
sub ns { "Test2" }
sub pkg { $?PACKAGE }
our $scalar = 42;
}
package Test3 {
sub pkg { $?PACKAGE }
}
}

use t::spec::packages::Test;

# test that all the functions are in the right place

# sanity test
# L<S10/Packages/package for Perl 6 code>
is($?PACKAGE, "Main", 'The Main $?PACKAGE was not broken by any declarations');

# block level
is(Test1::ns, "Test1", "block-level package declarations");
cmp_ok(Test1::pkg, &infix:<===>, ::Test1::, 'block-level $?PACKAGE var');
dies_ok { test1_export() }, "export was not imported implicitly";

# declared packages
is(Test2::ns, "Test2", "declared package");
cmp_ok(Test2::pkg, &infix:<===>, ::Test2::, 'declared package $?PACKAGE');

# string eval'ed packages
is(Test3::pkg, ::Test3::, 'eval\'ed package $?PACKAGE');
cmp_ok(Test3::pkg, &infix:<===>, ::Test3::, 'eval\'ed package type object');

# this one came from t/packages/Test.pm
is(t::spec::packages::Test::ns, "t::packages::Test", "loaded package");
cmp_ok(t::spec::packages::Test::pkg, &infix:<===>, ::t::packages::Test::, 'loaded package $?PACKAGE object');
my $x;
lives_ok { $x = test_export() }, "export was imported successfully";
is($x, "party island", "exported OK");

# exports
dies_ok { ns() }, "no ns() leaked";

# now the lexical / file level packages...
my $pkg;
dies_ok { $pkg = Our::Package::pkg },
"Can't see `our' packages out of scope", :todo<feature>;
lives_ok { $pkg = t::spec::packages::Test::get_our_pkg() },
"Package in scope can see `our' package declarations";
is($pkg, Our::Package, 'correct $?PACKAGE');
ok(!($pkg === ::Our::Package),
'not the same as global type object', :todo<feature>);

# oh no, how do we get to that object, then?
# perhaps %t::spec::packages::Test::<Our::Package> ?

dies_ok { $pkg = t::spec::packages::Test::cant_see_pkg() },
"can't see package declared out of scope", :todo<feature>;
lives_ok { $pkg = t::spec::packages::Test::my_pkg() },
"can see package declared in same scope";
is($pkg, ::My::Package::, 'correct $?PACKAGE');
ok($pkg !=== ::*My::Package::, 'not the same as global type object');

# Check temporization of variables in external packages
{
{
ok(eval('temp $Test2::scalar; 1'), "parse for temp package vars", :todo<bug>);
$Test2::scalar++;
}
is($Test2::scalar, 42, 'temporization of external package variables', :todo<bug>);
}
9 changes: 9 additions & 0 deletions S11-modules/use_perl_6.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use v6;

# L<S11/Versioning/which is short for>

use Perl-6;
use Test;

plan 1;
ok "'use Perl-6' works (and means the same as 'use v6')";
7 changes: 7 additions & 0 deletions packages/Export_PackA.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use v6;

module t::spec::packages::Export_PackA {
sub exported_foo () is export {
42;
}
}
9 changes: 9 additions & 0 deletions packages/Export_PackB.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use v6;

module t::spec::packages::Export_PackB {
use t::spec::packages::Export_PackA;

sub does_export_work () {
try { exported_foo() } == 42;
}
}
7 changes: 7 additions & 0 deletions packages/Export_PackC.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use v6;

module t::spec::packages::Export_PackC {
sub foo_packc () is export {
1;
}
}
7 changes: 7 additions & 0 deletions packages/Export_PackD.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use v6;

module t::spec::packages::Export_PackD {
sub this_gets_exported_lexically () is export {
'moose!'
}
}
4 changes: 4 additions & 0 deletions packages/Import.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use v6;

module t::spec::packages::Import;
# note the absence of a sub import() { }
7 changes: 7 additions & 0 deletions packages/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
t/spec/packages/README

This directory exists to hold files tested in S10-packages so that there isn't
a hyphen in the filename of the .pm file. This way, we can just
use t::spec::packages::Whatever;
instead of needing to quote the package name all the time, or add the proper
directory to the Perl 6 equivalent of @INC.
11 changes: 11 additions & 0 deletions packages/RequireAndUse1.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use v6;

# require and use return the last statement evaluated in the .pm, in this case
# 42. See thread "What do use and require evaluate to?" on p6l started by Ingo
# Blechschmidt, L<"http://www.nntp.perl.org/group/perl.perl6.language/22258">.

module t::spec::packages::RequireAndUse1 {
23;
}

42;
9 changes: 9 additions & 0 deletions packages/RequireAndUse2.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use v6;

# module Foo {...} returns the Foo module object,
# see thread "What do use and require evaluate to?" on p6l started by Ingo
# Blechschmidt, L<"http://www.nntp.perl.org/group/perl.perl6.language/22258">.

module t::spec::packages::RequireAndUse2 {
23;
}
10 changes: 10 additions & 0 deletions packages/RequireAndUse3.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use v6;

# "module Foo; ..." is equivalent to "module Foo {...}", so it returns the Foo
# module object, see thread "What do use and require evaluate to?" on p6l
# started by Ingo Blechschmidt,
# L<"http://www.nntp.perl.org/group/perl.perl6.language/22258">.

module t::spec::packages::RequireAndUse3;

23;
41 changes: 41 additions & 0 deletions packages/Test.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# The semicolon form of "package" would be illegal in the
# middle of a Perl 6 file.
# At the top, it would mean the rest of the file was Perl 5 code.
# So we use "package" with a block:

package t::spec::packages::Test {

sub ns { "t::spec::packages::Test" }

sub pkg { $?PACKAGE }

sub test_export is export { "party island" }

sub get_our_pkg {
Our::Package::pkg();
}

our package Our::Package {

sub pkg { $?PACKAGE }

}

sub cant_see_pkg {
return My::Package::pkg();
}

{
sub my_pkg {
return My::Package::pkg();
}

my package My::Package {
sub pkg { $?PACKAGE }
}

}

sub dummy_sub_with_params($arg1, $arg2) is export { "[$arg1] [$arg2]" }

}

0 comments on commit be85ac8

Please sign in to comment.