Help language development. Donate to The Perl Foundation

Result cpan:SAMGWISE last updated on 2019-08-28

t/30-adapter-routines.t6
#! /usr/bin/env perl6
use v6;
use Test;
use lib 'lib';
# plan 4;

use Result;
{
    my $err = result { die "bang!" }
    is $err.defined, True, 'result returns defined value for exception';
    is $err.is-err, True, 'result returns an Err on an exception';
}

{
    my $ok = result { 'foo' }
    is $ok.defined, True, 'result returns defined value for normal return';
    is $ok.ok('Should not be an error'), 'foo', 'result returns an Ok for normal return';
}

{
    my $err = result { fail "boo!" }
    is $err.defined, True, 'result returns defined value for failure';
    is $err.is-err, True, 'result returns an Err return of failure';
}

{
    my $ok = result { Ok "foo" }
    is $ok.defined, True, 'result returns defined value for Result::Ok';
    is $ok.value, 'foo', 'result returns an Ok for return of Result::Ok';
}

{
    my $err = result { Err "biff!" }
    is $err.defined, True, 'result returns defined value for Result::Err';
    is $err.error, 'biff!', 'result returns an Err for return of Result::Err';
}

# Issue #2 double call of code
{
    my Int $mutable = 1;
    my $ok = result -> { $mutable += 1 }
    is $ok.value, 2, "Callable is only called once";

    $ok = result -> { Ok $mutable += 1 }
    is $ok.value, 3, "Callable is only called once";
}

done-testing