The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Gen::Test::Rinci::FuncResult - Generate function to test a function

VERSION

version 0.01

SYNOPSIS

 use Gen::Test::Rinci::FuncResult qw(gen_test_func);
 use Test::More;

 sub divide {
     my %args = @_;
     my ($a, $b) = ($args{a}, $args{b});
     return [500, "undefined"] if $a == 0 && $b == 0;
     [200, "OK", $a/$b];
 }

 gen_test_func(name => 'test_divide', func => \&divide);

 test_divide(args=>{a=>6, b=>3}, result=>2);
 test_divide(args=>{a=>6, b=>0}, dies=>1);
 test_divide(args=>{a=>0, b=>0}, status=>500);
 done_testing;

DESCRIPTION

FAQ

SEE ALSO

Rinci

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

FUNCTIONS

None are exported by default, but they are exportable.

gen_test_func(%args) -> [status, msg, result, meta]

This function (A) will generate a function (B).

A accepts, among others, the name or the reference to the function that you want to test (T) and the name of the generated function, B.

B will run T once with some specified arguments, catch exception, and test its result. The result is expected to be an enveloped result (see the documentation of Rinci::function for more details about enveloped result).

B will accept the following arguments:

  • name (str)

      Name of the test. Will default to "T (ARGS...)" to show the name of the target
      function and the arguments that it is called with.
  • args (hash or array)

      Argument to feed to function T.
  • dies (bool, default => 0)

      Whether function T should die when run. If set to 1, further tests will not be
      done except the test that function dies.
  • status (int, default => 200)

      Will test the result's status code.
  • result (any)

      If specified, will test the actual result of the function.
  • run (code)

      Instead of running function T with C<args>, will execute this code instead.
  • posttest (code)

      Run this code for additional tests.

Todo:

  • Handle function with result_naked => 1.

Arguments ('*' denotes required arguments):

  • func* => code

    Target function to test.

  • install => bool (default: 1)

    Whether to install the function.

  • name* => str

    Name of the test function to generate (B).

    Can be fully qualified, e.g. Pkg::SubPkg::funcname or unqualified funcname (where the package will be taken from the package argument). Relevant for when installing the function.

  • package => any

    Perl package to put this function in.

    Relevant only when installing the function.

Return value:

Returns an enveloped result (an array). First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (result) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.