AI-Prolog

 view release on metacpan or  search on metacpan

t/20term.t  view on Meta::CPAN

#!/usr/bin/perl
# '$Id: 20term.t,v 1.6 2005/08/06 23:28:40 ovid Exp $';
use warnings;
use strict;
use Test::More tests => 92;
#use Test::More 'no_plan';

my $CLASS;
BEGIN
{
    chdir 't' if -d 't';
    unshift @INC => '../lib';
    $CLASS = 'AI::Prolog::Term';
    use_ok($CLASS) or die;
}

# I hate the fact that they're interdependent.  That brings a 
# chicken and egg problem to squashing bugs.
use aliased 'AI::Prolog::Parser';

can_ok $CLASS, 'occurcheck';
is $CLASS->occurcheck, 0, '... and it should return a false value';
$CLASS->occurcheck(1);
is $CLASS->occurcheck, 1, '... but we should be able to set it to a true value';

can_ok $CLASS, 'new';

eval { $CLASS->new(1,2,3,4) };
ok $@, 'Calling new with arguments it does not expect should croak()';
like $@, qr/Unknown arguments to Term->new/,
    '... with an appropriate error message';

# new, unbound term

ok my $term = $CLASS->new, 'Calling it without arguments should succeed';
isa_ok $term, $CLASS, '... and the object it returns';

#diag $term->to_string;
my $term2 = $term->refresh([undef, $term]);
#diag $term2->to_string;
can_ok $term, 'functor';
ok ! defined $term->functor, '... and creating an blank term should not have a functor';

can_ok $term, 'arity';
is $term->arity, 0, '... and the blank term should have an arity (number of args) of 0';

can_ok $term, 'args';
is_deeply $term->args, [], '... and it should have no args';

can_ok $term, 'bound';
ok ! $term->bound, '... nor should it be bound to another term';

can_ok $term, 'varid';
is $term->varid, 1, '... and the first empty term should have the first varid';

can_ok $term, 'deref';
is_deeply $term->deref, $term, '... and since it is not bound, it returns $self';

can_ok $term, 'ref';
ok ! defined $term->ref, '... which means it should not reference anything';

can_ok $term, 'to_string';
is $term->to_string, 'A',
    '... and a simple unbound term will just have a new variable as its string representation';

# new with id

ok $term = $CLASS->new(7), 'We should be able to create a new term by just specifying an id for it';
ok ! defined $term->functor, '... and creating an blank term should not have a functor';
is $term->arity, 0, '... and the blank term should have an arity of 0';
is_deeply $term->args, [], '... and it should have no args';
ok ! $term->bound, '... nor should it be bound to another term';
is $term->varid, 7, '... and it should have the id we specify';
is_deeply $term->deref, $term, '... and since it is not bound, it returns $self';
ok ! defined $term->ref, '... which means it should not reference anything';
is $term->to_string, 'B',
    '... and a simple unbound term will just have a new variable as its string representation';

# new with functor and arity

ok $term = $CLASS->new('steals', 2), 
    'Creating a new terms by specifiying its functor and arity';
is $term->functor, 'steals', '... and it should have the functor we specify';
is $term->arity, 2, '... and the blank term should have an arity of 0';
is_deeply $term->args, [], '... and it should have no args';
is $term->bound, 1, '... but it should be bound to a value!';
is $term->varid, 0, '... and it should have a false varid';
is_deeply $term->deref, $term, '... and since it is not bound, it returns $self';
ok ! defined $term->ref, '... which means it should not reference anything';
is $term->to_string, 'steals()',
    '... bound term with no args should show the functor and empty parens';



( run in 1.360 second using v1.01-cache-2.11-cpan-39bf76dae61 )