accumulator_factory.t 586 B

1234567891011121314151617181920212223242526272829303132
  1. #!perl -T
  2. use utf8;
  3. use 5.006;
  4. use strict;
  5. use warnings;
  6. use Test::More;
  7. plan tests => 2;
  8. use Sidef;
  9. my $code = <<'EOT';
  10. func Accumulator(sum) {
  11. func(num) { sum += num };
  12. }
  13. EOT
  14. my $sidef = Sidef->new(name => 'accumulator_factory');
  15. my $acc = $sidef->execute_code($code);
  16. my $x = $acc->call(Sidef::Types::Number::Number->new(1));
  17. my $r1 = $x->call(Sidef::Types::Number::Number->new(5));
  18. $acc->call(Sidef::Types::Number::Number->new(42)); # this should not reset the accumulator
  19. my $r2 = $x->call(Sidef::Types::Number::Number->new(4));
  20. is("$r1", "6");
  21. is("$r2", "10");