1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#!perl -T
use 5.010;
use strict;
use warnings FATAL => 'all';
use Test::More tests => 21;
use Test::Fatal;
use Statistics::R::REXP::Closure;
use Statistics::R::REXP::Language;
use Statistics::R::REXP::Character;
use Statistics::R::REXP::Double;
use Statistics::R::REXP::Integer;
use Statistics::R::REXP::List;
use Statistics::R::REXP::Symbol;
use Statistics::R::REXP::Null;
my $closure = Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Null->new);
my $closure2 = Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Null->new);
is($closure, $closure2, 'closure equality');
is(Statistics::R::REXP::Closure->new($closure2), $closure, 'copy constructor');
## error checking in constructor arguments
like(exception {
Statistics::R::REXP::Closure->new()
}, qr/Attribute \(body\) is required/,
'error-check in no-arg constructor');
like(exception {
Statistics::R::REXP::Closure->new([1, 2, 3])
}, qr/HASH data or a Statistics::R::REXP::Closure/,
'error-check in single-arg constructor');
like(exception {
Statistics::R::REXP::Closure->new(1, 2, 3)
}, qr/odd number of arguments/,
'odd constructor arguments');
like(exception {
Statistics::R::REXP::Closure->new(args => [],
defaults => [undef],
body => Statistics::R::REXP::Null->new)
}, qr/argument names don't match their defaults/,
'odd constructor arguments');
like(exception {
Statistics::R::REXP::Closure->new(body => {foo => 1, bar => 2})
}, qr/Attribute 'body' must be a reference to an instance of Statistics::R::REXP/,
'bad body argument');
like(exception {
Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Integer->new([42]),
environment => 'foo')
}, qr/Attribute 'environment' must be an instance of Environment/,
'bad env enclosure');
my $another_closure = Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Symbol->new('foo'));
isnt($closure, $another_closure, 'closure inequality');
my $args_closure = Statistics::R::REXP::Closure->new(args => ['foo'],
body => Statistics::R::REXP::Symbol->new('foo'));
isnt($args_closure, $another_closure, 'args inequality');
# my $na_heavy_language = Statistics::R::REXP::Language->new(elements => [Statistics::R::REXP::Symbol->new('bla'), ['', undef], '0']);
# my $na_heavy_language2 = Statistics::R::REXP::Language->new(elements => [Statistics::R::REXP::Symbol->new('bla'), [undef, undef], 0]);
# is($na_heavy_language, $na_heavy_language, 'NA-heavy language equality');
# isnt($na_heavy_language, $na_heavy_language2, 'NA-heavy language inequality');
is($closure .'', 'function() NULL', 'closure text representation');
is($args_closure .'', 'function(foo) symbol `foo`', 'closure text representation');
ok(! $closure->is_null, 'is not null');
ok(! $closure->is_vector, 'is not vector');
## attributes
is_deeply($closure->attributes, undef, 'default attributes');
my $closure_attr = Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Null->new,
attributes => { foo => 'bar',
x => [40, 41, 42] });
is_deeply($closure_attr->attributes,
{ foo => 'bar', x => [40, 41, 42] }, 'constructed attributes');
my $closure_attr2 = Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Null->new,
attributes => { foo => 'bar',
x => [40, 41, 42] });
my $another_closure_attr = Statistics::R::REXP::Closure->new(body => Statistics::R::REXP::Null->new,
attributes => { foo => 'bar',
x => [40, 42, 42] });
is($closure_attr, $closure_attr2, 'equality considers attributes');
isnt($closure_attr, $closure, 'inequality considers attributes');
isnt($closure_attr, $another_closure_attr, 'inequality considers attributes deeply');
## attributes must be a hash
like(exception {
Statistics::R::REXP::Closure->new(body => [ Statistics::R::REXP::Symbol->new('foo') ],
attributes => 1)
}, qr/Attribute 'attributes' must be a hash reference/,
'setting non-HASH attributes');
## Perl representation
like(exception {
$closure->to_pl
}, qr/Closures do not have a native Perl representation/,
'Perl representation');
|