Skip to content

Commit

Permalink
Some initial tests for 6model parametric ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Jan 13, 2015
1 parent 9d6ba70 commit 98b770c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions t/nqp/87-parametric-6model.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#! nqp

plan(10);

# Counter to make sure parameterization interning works right.
my int $num_parameterizations := 0;

# A coercion type HOW, as far as needed to test parameterization.
class SimpleCoerceHOW {
method new_type() {
my $type := nqp::newtype(self.new(), 'Uninstantiable');
nqp::setparameterizer($type, -> $type, $params {
# Re-use same HOW.
$num_parameterizations++;
nqp::newtype($type.HOW, 'Uninstantiable');
});
$type
}

method parameterize($coercion_types, $to, $from) {
nqp::parameterizetype($coercion_types, [$to, $from]);
}

sub check_instantiated($coercion_type) {
nqp::die('Cannot perform this operation on an uninstantiated coercion type')
if nqp::isnull(nqp::typeparameterized($coercion_type));
}

method to_type($coercion_type) {
check_instantiated($coercion_type);
nqp::typeparameterat($coercion_type, 0)
}

method from_type($coercion_type) {
check_instantiated($coercion_type);
# Could be nqp::typeparameterat($coercion_type, 1), but want to test other op.
nqp::atpos(nqp::typeparameters($coercion_type), 1)
}
}

my $coercion_types := SimpleCoerceHOW.new_type();
ok(1, 'Can create a parameterized type');

my $failed := 0;
try {
nqp::setparameterizer($coercion_types, -> $type, $params { });
CATCH { $failed := 1; }
}
ok($failed, 'Cannot set up parametricity twice');

class A { }
class B { }
class C { }

ok($num_parameterizations == 0, 'Sanity: did not yet run parameterization closure');
my $ct1 := $coercion_types.HOW.parameterize($coercion_types, A, B);
ok($num_parameterizations == 1, 'Produced one parameterization');

ok(nqp::eqaddr($ct1.HOW, $coercion_types.HOW), 'TestCoerceHOW is shared');
ok(!nqp::eqaddr($ct1.WHAT, $coercion_types.WHAT), '.WHAT is not shared');

ok(nqp::eqaddr(nqp::typeparameterized($ct1), $coercion_types), 'Can get back to parametric type');

ok(nqp::eqaddr($ct1.HOW.to_type($ct1), A), 'Can get type parameter (1)');
ok(nqp::eqaddr($ct1.HOW.from_type($ct1), B), 'Can get type parameter (2)');

$failed := 0;
try {
$coercion_types.HOW.to_type($coercion_types);
CATCH { $failed := 1 }
}
ok($failed, 'Cannot get type parameters on the unparameterized coercion type');

0 comments on commit 98b770c

Please sign in to comment.