Test-ExpectAndCheck-0.06000755001750001750 014513512607 13712 5ustar00leoleo000000000000Test-ExpectAndCheck-0.06/.editorconfig000444001750001750 5314513512607 16462 0ustar00leoleo000000000000root = true [*.{pm,pl,t}] indent_size = 3 Test-ExpectAndCheck-0.06/Build.PL000444001750001750 113314513512607 15341 0ustar00leoleo000000000000use v5; use strict; use warnings; use Module::Build; my $build = Module::Build->new( module_name => 'Test::ExpectAndCheck', requires => { 'perl' => '5.014', 'List::Util' => 0, 'Test::Builder' => '1.302', # subtest indentation format 'Test::Deep' => 0, 'Test::Future::Deferred' => 0, }, configure_requires => { 'Module::Build' => '0.4004', # test_requires }, test_requires => { 'Test::Builder::Tester' => 0, 'Test2::V0' => 0, }, license => 'perl', create_license => 1, create_readme => 1, ); $build->create_build_script; Test-ExpectAndCheck-0.06/Changes000444001750001750 311614513512607 15343 0ustar00leoleo000000000000Revision history for Test-ExpectAndCheck 0.06 2023-10-17 [CHANGES] * Ensure that the callback in `->will_return_using` can modify caller arguments if necessary for successfully mocking mutator functions * Various module style updates: + use Perl 5.14 + swap unit tests to Test2::V0 0.05 2022-10-07 [CHANGES] * Added `->whenever` for creating stub helper methods that assist the code-under-test * Added `->will_return_using` for dynamic result creation * Added `$exp->indefinitely` for whenever expectations that outlive calls to `->check_and_clear` 0.04 2022-10-04 [CHANGES] * Added entire new set of `->will_*` API methods, deprecating also the previous set * Added new `->will_also` and `->will_also_later` methods 0.03 2021-07-14 [CHANGES] * Print more detailed error message on expectation failure to give details about what the next expectation was * Set @CARP_NOT to get slightly better call location reporting * Added ->remains_pending method to allow returned futures to not complete at all [BUGFIXES] * Fix printable-or-not detection logic so it applies to all bytes * Handle `undef` correctly in stringification logic 0.02 2021-01-02 [CHANGES] * Return deferred futures by default * Added ->immediately method to make some returns immediate 0.01 2020-11-25 First version, released on an unsuspecting world. Test-ExpectAndCheck-0.06/LICENSE000444001750001750 4375514513512607 15112 0ustar00leoleo000000000000This software is copyright (c) 2023 by Paul Evans . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2023 by Paul Evans . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2023 by Paul Evans . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Test-ExpectAndCheck-0.06/MANIFEST000444001750001750 33114513512607 15155 0ustar00leoleo000000000000.editorconfig Build.PL Changes lib/Test/ExpectAndCheck.pm lib/Test/ExpectAndCheck/Future.pm LICENSE MANIFEST This list of files META.json META.yml README t/00use.t t/01expect.t t/02whenever.t t/10future.t t/99pod.t Test-ExpectAndCheck-0.06/META.json000444001750001750 254714513512607 15500 0ustar00leoleo000000000000{ "abstract" : "C-style unit testing with object methods", "author" : [ "Paul Evans " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4234", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Test-ExpectAndCheck", "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.4004" } }, "runtime" : { "requires" : { "List::Util" : "0", "Test::Builder" : "1.302", "Test::Deep" : "0", "Test::Future::Deferred" : "0", "perl" : "5.014" } }, "test" : { "requires" : { "Test2::V0" : "0", "Test::Builder::Tester" : "0" } } }, "provides" : { "Test::ExpectAndCheck" : { "file" : "lib/Test/ExpectAndCheck.pm", "version" : "0.06" }, "Test::ExpectAndCheck::Future" : { "file" : "lib/Test/ExpectAndCheck/Future.pm", "version" : "0.06" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.06", "x_serialization_backend" : "JSON::PP version 4.07" } Test-ExpectAndCheck-0.06/META.yml000444001750001750 157514513512607 15330 0ustar00leoleo000000000000--- abstract: 'C-style unit testing with object methods' author: - 'Paul Evans ' build_requires: Test2::V0: '0' Test::Builder::Tester: '0' configure_requires: Module::Build: '0.4004' dynamic_config: 1 generated_by: 'Module::Build version 0.4234, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Test-ExpectAndCheck provides: Test::ExpectAndCheck: file: lib/Test/ExpectAndCheck.pm version: '0.06' Test::ExpectAndCheck::Future: file: lib/Test/ExpectAndCheck/Future.pm version: '0.06' requires: List::Util: '0' Test::Builder: '1.302' Test::Deep: '0' Test::Future::Deferred: '0' perl: '5.014' resources: license: http://dev.perl.org/licenses/ version: '0.06' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Test-ExpectAndCheck-0.06/README000444001750001750 1300614513512607 14747 0ustar00leoleo000000000000NAME Test::ExpectAndCheck - expect/check-style unit testing with object methods SYNOPSIS use Test::More; use Test::ExpectAndCheck; my ( $controller, $mock ) = Test::ExpectAndCheck->create; { $controller->expect( act => 123, 45 ) ->will_return( 678 ); is( $mock->act( 123, 45 ), 678, '$mock->act returns result' ); $controller->check_and_clear( '->act' ); } done_testing; DESCRIPTION This package creates objects that assist in writing unit tests with mocked object instances. Each mock instance will expect to receive a given list of method calls. Each method call is checked that it received the right arguments, and will return a prescribed result. At the end of each test, each object is checked to ensure all the expected methods were called. METHODS create ( $controller, $mock ) = Test::ExpectAndCheck->create; Objects are created in "entangled pairs" by the create method. The first object is called the "controller", and is used by the unit testing script to set up what method calls are to be expected, and what their results shall be. The second object is the "mock", the object to be passed to the code being tested, on which the expected method calls are (hopefully) invoked. It will have whatever interface is implied by the method call expectations. expect $exp = $controller->expect( $method, @args ); Specifies that the mock will expect to receive a method call of the given name, with the given arguments. The argument values are compared using "cmp_deeply" in Test::Deep. Values can be specified literally, or using any of the "Special Comparisons" defined by Test::Deep. The test script can call the "will_return" or "will_throw" methods on the expectation to set what the result of invoking this method will be. whenever $exp = $controller->whenever( $method, @args ); Since version 0.05. Specifies that the mock might expect to receive method calls of the given name with the given arguments. These expectations are not expired once called, nor do they expect to be called in any particular order. Furthermore it is not a test failure for one of these not to be invoked at all. These expectations do not directly form part of the test assertions checked by the "check_and_clear" method, but they may be useful to assist the code under test, such as providing support behaviours that it may rely on but would make the test script too fragile if spelled out in full using a regular expect. These expectations are only used as a fallback mechanism, if the next real expect-based expectation does not match a method call. Individual special cases can still be set up using expect even though a whenever exists that might also match it. As with "expect", the argument values are compared using Test::Deep, and results can be set with "will_return" or "will_throw". check_and_clear $controller->check_and_clear( $name ); Checks that by now, every expected method has been called, and emits a new test output line via Test::Builder. Regardless, the expectations are also cleared out ready for the start of the next test. EXPECTATIONS Each value returned by the "expect" method is an "expectation", an object that represents one expected method call, the arguments it should receive, and the return value it should provide. will_return $exp->will_return( @result ); Since version 0.04. Sets the result that will be returned by this method call. This method used to be named returns, which should be avoided in new code. Uses of the old name will print a deprecation warning. will_return_using $exp->will_return_using( sub ($args) { ... } ); Since version 0.05. Sets the result that will be returned, calculated by invoking the code. The code block is invoked at the time that a result is needed. It is invoked with an array reference containing the arguments to the original method call. This is especially useful for expectations created using "whenever". Since version 0.06 the code block is passed a reference to the caller's actual arguments array, and therefore can modify values in it if required - e.g. when trying to mock functions such as open() or sysread() which modify lvalues passed in as arguments. There is no corresponding will_throw_using, but an exception thrown by this code will be seen by the calling code. will_throw $exp->will_throw( $e ); Since version 0.04. Sets the exception that will be thrown by this method call. This method used to be named throws, which should be avoided in new code. will_also $exp->will_also( sub { ... } ); Since version 0.04. Adds extra code which is run when the expected method is called, in addition to generating the result value or exception. When invoked, the code body is invoked in void context with no additional arguments. indefinitely $exp->indefinitely; Since version 0.05. On an expectation created using "whenever", this expectation will not be cleared by "check_and_clear", effectively establishing its effects for the entire lifetime of the test script. On an expectation created using "expect" this has no effect; such an expectation will still be cleared as usual. AUTHOR Paul Evans Test-ExpectAndCheck-0.06/lib000755001750001750 014513512607 14460 5ustar00leoleo000000000000Test-ExpectAndCheck-0.06/lib/Test000755001750001750 014513512607 15377 5ustar00leoleo000000000000Test-ExpectAndCheck-0.06/lib/Test/ExpectAndCheck.pm000444001750001750 2624514513512607 20734 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2021-2023 -- leonerd@leonerd.org.uk package Test::ExpectAndCheck 0.06; use v5.14; use warnings; use Carp; use List::Util qw( first ); use Scalar::Util qw( blessed ); use Test::Deep (); use constant EXPECTATION_CLASS => "Test::ExpectAndCheck::_Expectation"; =head1 NAME C - C-style unit testing with object methods =head1 SYNOPSIS use Test::More; use Test::ExpectAndCheck; my ( $controller, $mock ) = Test::ExpectAndCheck->create; { $controller->expect( act => 123, 45 ) ->will_return( 678 ); is( $mock->act( 123, 45 ), 678, '$mock->act returns result' ); $controller->check_and_clear( '->act' ); } done_testing; =head1 DESCRIPTION This package creates objects that assist in writing unit tests with mocked object instances. Each mock instance will expect to receive a given list of method calls. Each method call is checked that it received the right arguments, and will return a prescribed result. At the end of each test, each object is checked to ensure all the expected methods were called. =cut =head1 METHODS =cut =head2 create ( $controller, $mock ) = Test::ExpectAndCheck->create; Objects are created in "entangled pairs" by the C method. The first object is called the "controller", and is used by the unit testing script to set up what method calls are to be expected, and what their results shall be. The second object is the "mock", the object to be passed to the code being tested, on which the expected method calls are (hopefully) invoked. It will have whatever interface is implied by the method call expectations. =cut sub create { my $class = shift; my $controller = bless { expectations => [], whenever => {}, }, $class; my $mock = Test::ExpectAndCheck::_Obj->new( $controller ); return ( $controller, $mock ); } =head2 expect $exp = $controller->expect( $method, @args ); Specifies that the mock will expect to receive a method call of the given name, with the given arguments. The argument values are compared using L. Values can be specified literally, or using any of the "Special Comparisons" defined by L. The test script can call the L or L methods on the expectation to set what the result of invoking this method will be. =cut sub expect { my $self = shift; my ( $method, @args ) = @_; my ( undef, $file, $line ) = caller(1); defined $file or ( undef, $file, $line ) = caller(0); push @{ $self->{expectations} }, my $exp = $self->EXPECTATION_CLASS->new( $method => [ @args ], $file, $line, ); return $exp; } =head2 whenever $exp = $controller->whenever( $method, @args ); I Specifies that the mock might expect to receive method calls of the given name with the given arguments. These expectations are not expired once called, nor do they expect to be called in any particular order. Furthermore it is not a test failure for one of these not to be invoked at all. These expectations do not directly form part of the test assertions checked by the L method, but they may be useful to assist the code under test, such as providing support behaviours that it may rely on but would make the test script too fragile if spelled out in full using a regular C. These expectations are only used as a fallback mechanism, if the next real C-based expectation does not match a method call. Individual special cases can still be set up using C even though a C exists that might also match it. As with L, the argument values are compared using C, and results can be set with L or L. =cut sub whenever { my $self = shift; my ( $method, @args ) = @_; my ( undef, $file, $line ) = caller(1); defined $file or ( undef $file, $line ) = caller(0); push @{ $self->{whenever}{$method} }, my $exp = $self->EXPECTATION_CLASS->new( $method => [ @args ], $file, $line, ); return $exp; } sub _stringify { my ( $v ) = @_; if( !defined $v ) { return "undef"; } elsif( blessed $v and $v->isa( "Test::Deep::Ignore" ) ) { return "ignore()"; } elsif( $v =~ m/^-?[0-9]+$/ ) { return sprintf "%d", $v; } elsif( $v =~ m/^[\x20-\x7E]*\z/ ) { $v =~ s/([\\'])/\\$1/g; return qq('$v'); } else { if( $v =~ m/[^\n\x20-\x7E]/ ) { # string contains something non-printable; just hexdump it all $v =~ s{(.)}{sprintf "\\x%02X", ord $1}gse; } else { $v =~ s/([\\'\$\@])/\\$1/g; $v =~ s{\n}{\\n}g; } return qq("$v"); } } sub _stringify_args { join ", ", map { _stringify $_ } @_; } sub _call { my $self = shift; my $method = shift; my $args = \@_; my $e; $e = first { !$_->_called } @{ $self->{expectations} } and $e->_consume( $method, @$args ) and return $e->_result( $args ); if( my $wh = first { $_->_consume( $method, @$args ) } @{ $self->{whenever}{$method} } ) { return $wh->_result( $args ); } my $message = Carp::shortmess( "Unexpected call to ->$method(${\ _stringify_args @$args })" ); $message .= "... while expecting " . $e->_stringify if $e; $message .= "... after all expectations done" if !$e; die "$message.\n"; } =head2 check_and_clear $controller->check_and_clear( $name ); Checks that by now, every expected method has been called, and emits a new test output line via L. Regardless, the expectations are also cleared out ready for the start of the next test. =cut sub check_and_clear { my $self = shift; my ( $name ) = @_; my $builder = Test::Builder->new; local $Test::Builder::Level = $Test::Builder::Level + 1; $builder->subtest( $name, sub { my $count = 0; foreach my $exp ( @{ $self->{expectations} } ) { $exp->_check( $builder ); $count++; } $builder->ok( 1, "No calls made" ) if !$count; }); undef @{ $self->{expectations} }; # Only clear the non-indefinite ones foreach my $method ( keys %{ $self->{whenever} } ) { my $whenevers = $self->{whenever}{$method}; @$whenevers = grep { $_->{indefinitely} } @$whenevers; @$whenevers or delete $self->{whenever}{$method}; } } package Test::ExpectAndCheck::_Expectation; use List::Util qw( all ); =head1 EXPECTATIONS Each value returned by the L method is an "expectation", an object that represents one expected method call, the arguments it should receive, and the return value it should provide. =cut sub new { my $class = shift; my ( $method, $args, $file, $line ) = @_; return bless { method => $method, args => $args, file => $file, line => $line, }, $class; } =head2 will_return $exp->will_return( @result ); I Sets the result that will be returned by this method call. This method used to be named C, which should be avoided in new code. Uses of the old name will print a deprecation warning. =cut sub will_return { my $self = shift; my @result = @_; return $self->will_return_using( sub { return @result } ); } sub returns { warnings::warnif deprecated => "Calling \$exp->returns() is now deprecated; use ->will_return instead"; return shift->will_return( @_ ); } =head2 will_return_using $exp->will_return_using( sub ($args) { ... } ); I Sets the result that will be returned, calculated by invoking the code. The code block is invoked at the time that a result is needed. It is invoked with an array reference containing the arguments to the original method call. This is especially useful for expectations created using L. I the code block is passed a reference to the caller's actual arguments array, and therefore can modify values in it if required - e.g. when trying to mock functions such as C or C which modify lvalues passed in as arguments. There is no corresponding C, but an exception thrown by this code will be seen by the calling code. =cut sub will_return_using { my $self = shift; my ( $code ) = @_; $self->{gen_return} = $code; return $self; } =head2 will_throw $exp->will_throw( $e ); I Sets the exception that will be thrown by this method call. This method used to be named C, which should be avoided in new code. =cut sub will_throw { my $self = shift; my ( $exception ) = @_; return $self->will_return_using( sub { die $exception } ); } sub throws { warnings::warnif deprecated => "Calling \$exp->throws() is now deprecated; use ->will_throw instead"; return shift->will_throw( @_ ); } =head2 will_also $exp->will_also( sub { ... } ); I Adds extra code which is run when the expected method is called, in addition to generating the result value or exception. When invoked, the code body is invoked in void context with no additional arguments. =cut sub will_also { my $self = shift; push @{ $self->{also} }, @_; return $self; } =head2 indefinitely $exp->indefinitely; I On an expectation created using L, this expectation will not be cleared by L, effectively establishing its effects for the entire lifetime of the test script. On an expectation created using L this has no effect; such an expectation will still be cleared as usual. =cut sub indefinitely { my $self = shift; $self->{indefinitely}++; return $self; } sub _consume { my $self = shift; my ( $method, @args ) = @_; $method eq $self->{method} or return 0; my ( $ok, $stack ) = Test::Deep::cmp_details( \@args, $self->{args} ); unless( $ok ) { $self->{diag} = Test::Deep::deep_diag( $stack ); return 0; } $self->{called}++; return 1; } sub _check { my $self = shift; my ( $builder ) = @_; my $method = $self->{method}; $builder->ok( $self->{called}, "->$method(${\ Test::ExpectAndCheck::_stringify_args @{ $self->{args} } })" ); $builder->diag( $self->{diag} ) if defined $self->{diag}; } sub _result { my $self = shift; my ( $args ) = @_; if( my $also = $self->{also} ) { $_->() for @$also; } my @result; @result = $self->{gen_return}->( $args ) if $self->{gen_return}; return @result if wantarray; return $result[0]; } sub _called { my $self = shift; return $self->{called}; } sub _stringify { my $self = shift; return "->$self->{method}(${\( Test::ExpectAndCheck::_stringify_args @{ $self->{args} } )}) at $self->{file} line $self->{line}"; } package Test::ExpectAndCheck::_Obj; our @CARP_NOT = qw( Test::ExpectAndCheck ); sub new { my $class = shift; my ( $controller ) = @_; return bless [ $controller ], $class; } sub AUTOLOAD { my $self = shift; ( our $AUTOLOAD ) =~ m/::([^:]+)$/; my $method = $1; return if $method eq "DESTROY"; return $self->[0]->_call( $method, @_ ); } =head1 AUTHOR Paul Evans =cut 0x55AA; Test-ExpectAndCheck-0.06/lib/Test/ExpectAndCheck000755001750001750 014513512607 20210 5ustar00leoleo000000000000Test-ExpectAndCheck-0.06/lib/Test/ExpectAndCheck/Future.pm000444001750001750 1357714513512607 22212 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2020-2023 -- leonerd@leonerd.org.uk package Test::ExpectAndCheck::Future 0.06; use v5.14; use warnings; use base qw( Test::ExpectAndCheck ); use constant EXPECTATION_CLASS => "Test::ExpectAndCheck::Future::_Expectation"; =head1 NAME C - C-style unit testing with C-returning methods =head1 SYNOPSIS use Test::More; use Test::ExpectAndCheck::Future; use Future::AsyncAwait; my ( $controller, $mock ) = Test::ExpectAndCheck::Future->create; { $controller->expect( act => 123, 45 ) ->will_done( 678 ); is( await $mock->act( 123, 45 ), 678, '$mock->act yields result' ); $controller->check_and_clear( '->act' ); } done_testing; =head1 DESCRIPTION This package creates objects that assist in writing unit tests with mocked object instances. Each mocked instance will expect to receive a given list of method calls. Each method call is checked that it received the right arguments, and will return a L instance to yield the prescribed result. At the end of each test, each object is checked to ensure all the expected methods were called. It is a variation of L, assistance around the results of invoked methods. Every invoked method will return a L instance. The L or L method can then set the desired eventual result of that future instance for each expectation. These return instances are implemented using L, so they are not immediately ready. Instead they will only become ready after a toplevel C expression or call to the C method. This should help unit tests to run similarly to real-world behaviour, where most futures returned by real-world interfaces (such as IO systems) would not be immediately ready. This behaviour can be switched off for individual expectations by using the L method. =cut package Test::ExpectAndCheck::Future::_Expectation; use base qw( Test::ExpectAndCheck::_Expectation ); use Test::Future::Deferred; use Carp; our @CARP_NOT = qw( Test::ExpectAndCheck ); use constant { BEHAVE_NOFUTURE => 0, BEHAVE_DONE => 1, BEHAVE_FAIL => 2, BEHAVE_PENDING => 3, BEHAVE_IMM_MASK => 4, }; =head1 EXPECTATIONS =cut =head2 will_done $exp->will_done( @result ); I Sets that method call will return a C instance which will succeed with the given result. =cut sub will_done { my $self = shift; my $imm = ( $self->{behaviour}[0] // 0 ) & BEHAVE_IMM_MASK; $self->{behaviour} = [ BEHAVE_DONE|$imm, @_ ]; return $self; } # This was a bad API; "returns" on a T:EAC:Future expectation would set the # future done result, not the immediate method call result sub returns { warnings::warnif deprecated => "Calling \$exp->returns() on a Future expectation is now deprecated; use ->will_done instead"; return shift->will_done( @_ ); } =head2 will_fail $exp->will_fail( $message, $category, @more ); I Sets that method call will return a C instance which will fail with the given message, and optionally category name and extra details. =cut sub will_fail { my $self = shift; my $imm = ( $self->{behaviour}[0] // 0 ) & BEHAVE_IMM_MASK; $self->{behaviour} = [ BEHAVE_FAIL|$imm, @_ ]; return $self; } sub fails { warnings::warnif deprecated => "Calling \$exp->fails() is now deprecated; use ->will_fail instead"; return shift->will_fail( @_ ); } # Reset the future-type behaviour on these sub will_return_using { my $self = shift; $self->SUPER::will_return_using( @_ ); $self->{behaviour} = [ BEHAVE_NOFUTURE ]; return $self; } =head2 immediately $exp->will_done( ... )->immediately; $exp->will_fail( ... )->immediately; I Switches this expectation to return an immediate future, rather than a deferred one. =cut sub immediately { my $self = shift; $self->{behaviour}[0] |= BEHAVE_IMM_MASK; return $self; } =head2 remains_pending $exp->remains_pending; I Sets that the future returned by this method will not complete and simply remain pending. =cut sub remains_pending { my $self = shift; $self->{behaviour}[0] = BEHAVE_PENDING; return $self; } =head2 will_also_later $exp->will_also_later( sub { ... } ); I Adds extra code which will run when the expected method is called, after the returned future has completed. This is performed by the use of C. When invoked, the code body is invoked in void context with no additional arguments. =cut sub will_also_later { my $self = shift; push @{ $self->{also_later} }, @_; return $self; } sub _result { my $self = shift; my $behaviour = $self->{behaviour} // [ 0 ]; my ( $type, @args ) = @$behaviour; # $type == BEHAVE_NOFUTURE is zero and ignored if( $type == BEHAVE_DONE ) { $self->SUPER::will_return( Test::Future::Deferred->done_later( @args ) ); } elsif( $type == (BEHAVE_DONE|BEHAVE_IMM_MASK) ) { $self->SUPER::will_return( Future->done( @args ) ); } elsif( $type == BEHAVE_FAIL ) { $self->SUPER::will_return( Test::Future::Deferred->fail_later( @args ) ); } elsif( $type == (BEHAVE_FAIL|BEHAVE_IMM_MASK) ) { $self->SUPER::will_return( Future->fail( @args ) ); } elsif( $type == BEHAVE_PENDING ) { $self->SUPER::will_return( Future->new ); } elsif( $type ) { die "TODO: Need result type $type"; } if( my $also_later = $self->{also_later} ) { Test::Future::Deferred->done_later ->on_done( sub { $_->() for @$also_later } ) ->retain; } return $self->SUPER::_result( @_ ); } =head1 AUTHOR Paul Evans =cut 0x55AA; Test-ExpectAndCheck-0.06/t000755001750001750 014513512607 14155 5ustar00leoleo000000000000Test-ExpectAndCheck-0.06/t/00use.t000444001750001750 25014513512607 15410 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; require Test::ExpectAndCheck; require Test::ExpectAndCheck::Future; pass( 'Modules loaded' ); done_testing; Test-ExpectAndCheck-0.06/t/01expect.t000444001750001750 1076214513512607 16156 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test::Builder::Tester; use Test2::V0; use Test::ExpectAndCheck; my ( $controller, $mock ) = Test::ExpectAndCheck->create; # pass { test_out q[# Subtest: ->amethod]; test_out q[ ok 1 - ->amethod('0.5')]; test_out q[ 1..1]; test_out q[ok 1 - ->amethod]; $controller->expect( amethod => 0.5 ); $mock->amethod( 0.5 ); $controller->check_and_clear( '->amethod' ); test_test 'amethod OK'; } # will_return { test_out q[ok 1 - $mock->one returns 1]; test_out q[# Subtest: ->one]; test_out q[ ok 1 - ->one()]; test_out q[ 1..1]; test_out q[ok 2 - ->one]; $controller->expect( one => ) ->will_return( 1 ); is( $mock->one, 1, '$mock->one returns 1' ); $controller->check_and_clear( '->one' ); test_test 'one OK'; } # will_throw { test_out q[ok 1 - $mock->two throws]; test_out q[# Subtest: ->two]; test_out q[ ok 1 - ->two()]; test_out q[ 1..1]; test_out q[ok 2 - ->two]; $controller->expect( two => ) ->will_throw( "Oopsie\n" ); is( !eval { $mock->two } && $@, "Oopsie\n", '$mock->two throws' ); $controller->check_and_clear( '->two' ); test_test 'two throws'; } # will_return_using { test_out q[ok 1 - args to ->will_return_using sub]; test_out q[ok 2 - $mock->three returns 3]; test_out q[ok 3 - ->will_return_using can modify args]; test_out q[# Subtest: ->three]; test_out q[ ok 1 - ->three('abc')]; test_out q[ 1..1]; test_out q[ok 4 - ->three]; my $result = 3; $controller->expect( three => "abc" ) ->will_return_using( sub { my ( $args ) = @_; is( $args, [ "abc" ], 'args to ->will_return_using sub' ); $args->[0] = "def"; return $result; } ); my $val = "abc"; is( $mock->three( $val ), 3, '$mock->three returns 3' ); is( $val, "def", '->will_return_using can modify args' ); $controller->check_and_clear( '->three' ); test_test 'three OK'; } # fail not called { test_out q[# Subtest: ->bmethod]; test_out q[ not ok 1 - ->bmethod('0.5')]; test_out q[ 1..1]; test_out q[not ok 1 - ->bmethod]; test_err q[ # Failed test '->bmethod('0.5')']; test_err qr/\s*# at .* line \d+\.\n/; test_err q[ # Looks like you failed 1 test of 1.]; test_fail +3; $controller->expect( bmethod => 0.5 ); $controller->check_and_clear( '->bmethod' ); test_test 'bmethod fail not called'; } # fail wrong args { test_out q[ok 1 - ->cmethod with wrong args dies]; test_out q[# Subtest: ->cmethod fails]; test_out q[ not ok 1 - ->cmethod('0.5')]; test_out q[ 1..1]; test_out q[not ok 2 - ->cmethod fails]; test_err q[ # Failed test '->cmethod('0.5')']; test_err qr/\s*# at .* line \d+\.\n/; test_err q[ # Compared $data->[0]]; test_err q[ # got : '1']; test_err q[ # expect : '0.5']; test_err q[ # Looks like you failed 1 test of 1.]; test_fail +6; my $line = __LINE__; $controller->expect( cmethod => 0.5 ); ok( !defined eval { $mock->cmethod( 1.0 ) }, '->cmethod with wrong args dies' ); my $e = "$@"; $controller->check_and_clear( '->cmethod fails' ); test_test 'cmethod fail wrong args'; # I can't actually break out of TBT mode here so lets nest it instead test_out q[ok 1 - thrown exception from expectation failure]; is( $e, "Unexpected call to ->cmethod(1) at $0 line ${\( $line+2 )}.\n" . "... while expecting ->cmethod('0.5') at $0 line ${\( $line+1 )}.\n", 'thrown exception from expectation failure' ); test_test 'exception message check'; } # fail unexpected { test_out q[ok 1 - unexpected ->dmethod dies]; test_out qr/\s*# Subtest: ->dmethod fails\n/; test_out q[ ok 1 - No calls made]; test_out q[ 1..1]; test_out q[ok 2 - ->dmethod fails]; ok( !defined eval { $mock->dmethod( 2.0 ) }, 'unexpected ->dmethod dies' ); $controller->check_and_clear( '->dmethod fails' ); test_test 'dmethod fail unexpected'; } # will_also { test_out q[ok 1 - $mock->more returns 1]; test_out q[# Subtest: ->more]; test_out q[ ok 1 - ->more()]; test_out q[ 1..1]; test_out q[ok 2 - ->more]; test_out q[ok 3 - ->will_also code is invoked]; my $called; $controller->expect( more => ) ->will_return( 1 ) ->will_also( sub { $called++ } ); is( $mock->more, 1, '$mock->more returns 1' ); $controller->check_and_clear( '->more' ); ok( $called, '->will_also code is invoked' ); test_test 'more OK'; } done_testing; Test-ExpectAndCheck-0.06/t/02whenever.t000444001750001750 516114513512607 16467 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test::Builder::Tester; use Test2::V0; use Test::ExpectAndCheck; my ( $controller, $mock ) = Test::ExpectAndCheck->create; # pass { test_out q[ok 1 - ->one]; test_out q[ok 2 - ->one again]; test_out q[# Subtest: ->one]; test_out q[ ok 1 - No calls made]; test_out q[ 1..1]; test_out q[ok 3 - ->one]; $controller->whenever( one => ) ->will_return( 1 ); is( $mock->one, 1, '->one' ); is( $mock->one, 1, '->one again' ); $controller->check_and_clear( '->one' ); test_test 'one OK'; } # choice of args { test_out q[ok 1 - ->add 1+1]; test_out q[ok 2 - ->add 2+2]; test_out q[ok 3 - ->add 2+2 again]; test_out q[ok 4 - ->add 1+1 again]; test_out q[# Subtest: ->add]; test_out q[ ok 1 - No calls made]; test_out q[ 1..1]; test_out q[ok 5 - ->add]; $controller->whenever( add => 1, 1 ) ->will_return( 2 ); $controller->whenever( add => 2, 2 ) ->will_return( 4 ); is( $mock->add( 1, 1 ), 2, '->add 1+1' ); is( $mock->add( 2, 2 ), 4, '->add 2+2' ); is( $mock->add( 2, 2 ), 4, '->add 2+2 again' ); is( $mock->add( 1, 1 ), 2, '->add 1+1 again' ); $controller->check_and_clear( '->add' ); test_test 'add OK'; } # will_return_using args { test_out q[ok 1 - ->mul 2*2]; test_out q[ok 2 - ->mul 2*3]; test_out q[# Subtest: ->mul]; test_out q[ ok 1 - No calls made]; test_out q[ 1..1]; test_out q[ok 3 - ->mul]; $controller->whenever( mul => Test::Deep::ignore(), Test::Deep::ignore() ) ->will_return_using( sub { my ($args) = @_; return $args->[0] * $args->[1]; } ); is( $mock->mul( 2, 2 ), 4, '->mul 2*2' ); is( $mock->mul( 2, 3 ), 6, '->mul 2*3' ); $controller->check_and_clear( '->mul' ); test_test 'mul OK'; } # indefinite whenevers outlive ->check_and_clear { $controller->whenever( div => Test::Deep::ignore(), Test::Deep::ignore() ) ->will_return_using( sub { my ($args) = @_; return $args->[0] / $args->[1]; } ) ->indefinitely; test_out q[ok 1 - ->div returns result]; test_out q[# Subtest: ->div]; test_out q[ ok 1 - No calls made]; test_out q[ 1..1]; test_out q[ok 2 - ->div]; is( $mock->div( 10, 5 ), 2, '->div returns result' ); $controller->check_and_clear( '->div' ); test_out q[ok 3 - ->div returns result]; test_out q[# Subtest: ->div again]; test_out q[ ok 1 - No calls made]; test_out q[ 1..1]; test_out q[ok 4 - ->div again]; is( $mock->div( 12, 4 ), 3, '->div returns result' ); $controller->check_and_clear( '->div again' ); test_test 'indefinitely'; } done_testing; Test-ExpectAndCheck-0.06/t/10future.t000444001750001750 1004414513512607 16171 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test::Builder::Tester; use Test2::V0; eval { require Future } or plan skip_all => "Future is not available"; use Test::ExpectAndCheck::Future; my ( $controller, $mock ) = Test::ExpectAndCheck::Future->create; # pass { test_out q[ok 1 - $mock->sleep is not done before get]; test_out q[ok 2 - $mock->sleep is done after get]; test_out q[# Subtest: ->sleep]; test_out q[ ok 1 - ->sleep('0.5')]; test_out q[ 1..1]; test_out q[ok 3 - ->sleep]; $controller->expect( sleep => 0.5 ) ->will_done; my $f = $mock->sleep( 0.5 ); ok( !$f->is_done, '$mock->sleep is not done before get' ); $f->get; ok( $f->is_done, '$mock->sleep is done after get' ); $controller->check_and_clear( '->sleep' ); test_test 'sleep OK'; } # will_done { test_out q[ok 1 - $mock->one returns 1]; test_out q[# Subtest: ->one]; test_out q[ ok 1 - ->one()]; test_out q[ 1..1]; test_out q[ok 2 - ->one]; $controller->expect( one => ) ->will_done( 1 ); is( $mock->one->get, 1, '$mock->one returns 1' ); $controller->check_and_clear( '->one' ); test_test 'one OK'; } # will_fail { test_out q[ok 1 - $mock->two fails]; test_out q[# Subtest: ->two]; test_out q[ ok 1 - ->two()]; test_out q[ 1..1]; test_out q[ok 2 - ->two]; $controller->expect( two => ) ->will_fail( "Oopsie\n" ); is( $mock->two->failure, "Oopsie\n", '$mock->two fails' ); $controller->check_and_clear( '->two' ); test_test 'two fails'; } # immediate { test_out q[ok 1 - $f is done immediately]; test_out q[ok 2 - $f->result]; test_out q[# Subtest: ->imm]; test_out q[ ok 1 - ->imm('ABC')]; test_out q[ 1..1]; test_out q[ok 3 - ->imm]; $controller->expect( imm => "ABC" ) ->immediately ->will_done( "DEF" ); my $f = $mock->imm( "ABC" ); ok( $f->is_done, '$f is done immediately' ); is( $f->result, "DEF", '$f->result' ); $controller->check_and_clear( '->imm' ); test_test 'immediate'; } # can fall back to ->will_return { test_out q[ok 1 - $f is direct return]; test_out q[# Subtest: ->direct]; test_out q[ ok 1 - ->direct()]; test_out q[ 1..1]; test_out q[ok 2 - ->direct]; $controller->expect( direct => ) ->will_return( "now" ); my $f = $mock->direct; is( $f, "now", '$f is direct return' ); $controller->check_and_clear( '->direct' ); test_test 'direct return'; } # will_also { test_out q[ok 1 - $f->get returns 1]; test_out q[# Subtest: ->more]; test_out q[ ok 1 - ->more()]; test_out q[ 1..1]; test_out q[ok 2 - ->more]; test_out q[ok 3 - ->will_also code is invoked]; my $called; $controller->expect( more => ) ->will_done( 1 ) ->will_also( sub { $called++ } ); my $f = $mock->more; is( $f->get, 1, '$f->get returns 1' ); $controller->check_and_clear( '->more' ); ok( $called, '->will_also code is invoked' ); test_test 'more OK'; } # remains pending { test_out q[ok 1 - $f is still pending]; test_out q[# Subtest: ->pending]; test_out q[ ok 1 - ->pending()]; test_out q[ 1..1]; test_out q[ok 2 - ->pending]; $controller->expect( pending => )->remains_pending; my $f = $mock->pending(); ok( !$f->is_ready, '$f is still pending' ); $controller->check_and_clear( '->pending' ); test_test 'remains_pending'; } # will_also_later happens at the right time { test_out q[ok 1 - $f->get returns done]; test_out q[# Subtest: ->seq]; test_out q[ ok 1 - ->seq()]; test_out q[ 1..1]; test_out q[ok 2 - ->seq]; test_out q[ok 3 - $sequence is correct]; my $sequence; $controller->expect( seq => ) ->will_also( sub { $sequence .= "1"; } ) ->will_done( "done" ) ->will_also_later( sub { $sequence .= "3"; } ); my $f = $mock->seq() ->on_done( sub { $sequence .= "2"; } ); is( $f->get, "done", '$f->get returns done' ); $controller->check_and_clear( '->seq' ); is( $sequence, "123", '$sequence is correct' ); test_test 'will_also_later'; } done_testing; Test-ExpectAndCheck-0.06/t/99pod.t000444001750001750 25514513512607 15425 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok();