Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature RFC: support pluggable expectations #832

Closed
happy-barney opened this issue May 12, 2019 · 1 comment
Closed

Feature RFC: support pluggable expectations #832

happy-barney opened this issue May 12, 2019 · 1 comment

Comments

@happy-barney
Copy link

Inspiration: Test-More/test-exception#16

Motivation is to provide unified behaviour for another test modules (like Test::Exception) to accept different kind of expectations as well as allow new expectation mechanisms (like Test::Deep to rise and being used by existing modules.

Let support combinations of such libraries by providing Expectation base class Test::Builder::Expectation

  • with method expect ($got) it will compare given date with expectation, returning Result instance
  • should support aggregation (or even operator overloading) like Test::Deep
  • should support negation (eg: foo & ! bar)

'Test::Build::Expectation::Result`

  • with method 'ok' (and boolean overload as well)
  • with method diag to format diagnostic message
    -- format_got to properly format given data
    -- format_expect to properly format what was expected

Functions like Test::Builder::maybe_regex should return Expectation instance as well so for example (taken from Text::Exception

    my $regex = $Tester->maybe_regex( $expecting );
    my $ok = $regex 
        ? ( $exception =~ m/$regex/ ) 
        : eval { 
            $exception->isa( ref $expecting ? ref $expecting : $expecting ) 
        };
    $Tester->ok( $ok, $description );
    unless ( $ok ) {
        $Tester->diag( _exception_as_string( "expecting:", $expecting ) );
        $Tester->diag( _exception_as_string( "found:", $exception ) );
    };
}

will be written as

  # with subclassing / wrapping `_exception_as_string` as `format_got`
   my $expectation = $self->expectation_instance( $expecting )
       || $self->expectation_regex( $expecting )
       || $self->expectation_isa( $expecting );

   my $result = $expectation->expect( $expection );
   $Tester->ok( $result, $description );
   $Tester->diag( $result->diag ) unless $result;

With such layout (and similar support by Test::Deep) mentioned pull request will not be necessary.

@exodist
Copy link
Member

exodist commented May 14, 2019

At this point the tools in Test::More are considered practically unchangeable (as far as interface and new features go, under the hood changes, and bugfixes can still happen). Any significant change like this has the potential to impact a LOT of things. Also it would be a nasty cycle to have Test::More in any way depend on Test::Deep, even for dev/test deps.

New features, interfaces, and support belong in new/alt libraries. Currently Test2 is the successor as far as where new ideas go to be tried. Test2-Suite is the package you are looking for. That said Test2::Suite has it's own deep comparison tools that have a lot of overlap with Test::Deep. Maybe it would be a good idea to make Test::Deep and Test2::Tools::Compare (part of Test2-Suite) support eachothers 'expected' structures.

@exodist exodist closed this as completed Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants