| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2020-2026 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Test::Future::IO 0.07; | ||||||||||
| 7 | |||||||||||
| 8 | 8 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 8 | ▶ | use feature qw( signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 8 | ▶ | no warnings qw( experimental::signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 8 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 8 | ▶ | use Test::ExpectAndCheck::Future 0.05; # ->whenever | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 8 | ▶ | use Test::Deep (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | =head1 NAME | ||||||||||
| 20 | |||||||||||
| 21 | C<Test::Future::IO> - unit testing on C<Future::IO> | ||||||||||
| 22 | |||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||
| 24 | |||||||||||
| 25 | =for highlighter language=perl | ||||||||||
| 26 | |||||||||||
| 27 | use Test::More; | ||||||||||
| 28 | use Test::Future::IO; | ||||||||||
| 29 | |||||||||||
| 30 | my $controller = Test::Future::IO->controller; | ||||||||||
| 31 | |||||||||||
| 32 | { | ||||||||||
| 33 | $controller->expect_syswrite_anyfh( "Hello, world\n" ); | ||||||||||
| 34 | $controller->expect_sysread_anyfh( 256 ) | ||||||||||
| 35 | ->will_done( "A string\n" ); | ||||||||||
| 36 | |||||||||||
| 37 | code_under_test(); | ||||||||||
| 38 | |||||||||||
| 39 | $controller->check_and_clear( 'code under test did correct IO' ); | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||
| 43 | |||||||||||
| 44 | This package provides a means to apply unit testing around code which uses | ||||||||||
| 45 | L<Future::IO>. It operates in an "expect-and-check" style of mocking, | ||||||||||
| 46 | requiring the test script to declare upfront what methods are expected to be | ||||||||||
| 47 | called, and what values they return. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | =head1 EXPECTATIONS | ||||||||||
| 52 | |||||||||||
| 53 | Each of the actual C<Future::IO> methods has a corresponding expectation | ||||||||||
| 54 | method on the controller object, whose name is prefixed with C<expect_>. A | ||||||||||
| 55 | single call to one of these methods by the unit test script represents a | ||||||||||
| 56 | single call to a C<Future::IO> method that the code under test is expected to | ||||||||||
| 57 | make. The arguments to the expectation method should match those given by the | ||||||||||
| 58 | code under test. Each expectation method returns an object which has | ||||||||||
| 59 | additional methods to control the behaviour of that invocation. | ||||||||||
| 60 | |||||||||||
| 61 | $exp = $controller->expect_accept( $fh ); | ||||||||||
| 62 | |||||||||||
| 63 | $exp = $controller->expect_connect( $fh, $name ); | ||||||||||
| 64 | |||||||||||
| 65 | $exp = $controller->expect_sleep( $secs ); | ||||||||||
| 66 | |||||||||||
| 67 | $exp = $controller->expect_sysread( $fh, $len ); | ||||||||||
| 68 | $exp = $controller->expect_syswrite( $fh, $bytes ); | ||||||||||
| 69 | |||||||||||
| 70 | Also, I<since version 0.07>: | ||||||||||
| 71 | |||||||||||
| 72 | $exp = $controller->expect_recv( $fh, $length, $flags ); | ||||||||||
| 73 | $exp = $controller->expect_recvfrom( $fh, $length, $flags ); | ||||||||||
| 74 | |||||||||||
| 75 | $exp = $controller->expect_send( $fh, $bytes, $flags, $to ); | ||||||||||
| 76 | |||||||||||
| 77 | For testing simpler code that does not operate on multiple filehandles, two | ||||||||||
| 78 | additional methods that ignore the filehandle argument may be more convenient: | ||||||||||
| 79 | |||||||||||
| 80 | $exp = $controller->expect_sysread_anyfh( $len ); | ||||||||||
| 81 | $exp = $controller->expect_syswrite_anyfh( $bytes ); | ||||||||||
| 82 | |||||||||||
| 83 | In each case the returned expectation object allows the test script to specify | ||||||||||
| 84 | what such an invocation should return. | ||||||||||
| 85 | |||||||||||
| 86 | $exp->will_done( @result ); | ||||||||||
| 87 | |||||||||||
| 88 | Expectations can make methods fail instead. | ||||||||||
| 89 | |||||||||||
| 90 | $exp->will_fail( $message ); | ||||||||||
| 91 | $exp->will_fail( $message, $category, @details ); | ||||||||||
| 92 | |||||||||||
| 93 | Expectations can be set to remain pending rather than completing. | ||||||||||
| 94 | |||||||||||
| 95 | $exp->remains_pending; | ||||||||||
| 96 | |||||||||||
| 97 | As a convenience, a C<syswrite> and C<send> expectation will default to | ||||||||||
| 98 | returning a future that will complete yielding its length (as is usual for | ||||||||||
| 99 | successful writes), and a C<sleep> or C<connect> expectation will return a | ||||||||||
| 100 | future that completes yielding nothing. | ||||||||||
| 101 | |||||||||||
| 102 | Testing event-based code with C<expect_sysread> can be fragile, as it relies | ||||||||||
| 103 | on exact ordering, buffer sizes, and so on. A more flexible approach that | ||||||||||
| 104 | leads to less brittle tests is to use a buffer around that filehandle that is | ||||||||||
| 105 | provided by the test module. The test module then intercepts all C<sysread> | ||||||||||
| 106 | method calls on the given filehandle to return data from that buffer: | ||||||||||
| 107 | |||||||||||
| 108 | $controller->use_sysread_buffer( $fh ); | ||||||||||
| 109 | |||||||||||
| 110 | $controller->write_sysread_buffer( $fh, $data ); | ||||||||||
| 111 | |||||||||||
| 112 | As a convenience for filling the sysread buffer at the right time, any | ||||||||||
| 113 | expectation returned by this module supports two extra methods for invoking | ||||||||||
| 114 | C<write_sysread_buffer> when another expectation completes: | ||||||||||
| 115 | |||||||||||
| 116 | $exp->will_write_sysread_buffer( $fh, $data ); | ||||||||||
| 117 | |||||||||||
| 118 | $exp->will_write_sysread_buffer_later( $fh, $data ); | ||||||||||
| 119 | |||||||||||
| 120 | These are both shortcuts for calling L</write_sysread_buffer> from within a | ||||||||||
| 121 | C<will_also> or C<will_also_later> code block. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | 8 | my ( $controller, $obj ) = Test::Future::IO::_Controller->create; | |||||||||
| 126 | |||||||||||
| 127 | 8 | my %sysread_buffers; | |||||||||
| 128 | |||||||||||
| 129 | 8 | require Future::IO; | |||||||||
| 130 | 8 | Future::IO->override_impl( $obj ); | |||||||||
| 131 | |||||||||||
| 132 | 3 | ▶ | sub expect_accept ( $self, $fh ) | ||||||||
|
Subroutine
expect_accept: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 133 | { | ||||||||||
| 134 | 3 | return $controller->expect( accept => $fh ); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 3 | ▶ | sub expect_connect ( $self, $fh, $name ) | ||||||||
|
Subroutine
expect_connect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 138 | { | ||||||||||
| 139 | 3 | return $controller->expect( connect => $fh, $name ) | |||||||||
| 140 | ->will_done(); | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 1 | ▶ | sub expect_recv ( $self, $fh, $length, $flags = undef ) | ||||||||
|
Subroutine
expect_recv: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 144 | 1 | { | |||||||||
| 145 | 1 | return $controller->expect( recv => $fh, $length, $flags ); | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 1 | ▶ | sub expect_recvfrom ( $self, $fh, $length, $flags = undef ) | ||||||||
|
Subroutine
expect_recvfrom: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 149 | 1 | { | |||||||||
| 150 | 1 | return $controller->expect( recvfrom => $fh, $length, $flags ); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 1 | ▶ | sub expect_send ( $self, $fh, $bytes, $flags = undef, $to = undef ) | ||||||||
|
Subroutine
expect_send: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 154 | 1 | { | |||||||||
| 155 | # TODO: Think about what missing flags or to means | ||||||||||
| 156 | 1 | return $controller->expect( send => $fh, $bytes, $flags, $to ) | |||||||||
| 157 | ->will_done( length $bytes ); | ||||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 4 | ▶ | sub expect_sleep ( $self, $secs ) | ||||||||
|
Subroutine
expect_sleep: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 161 | { | ||||||||||
| 162 | 4 | return $controller->expect( sleep => $secs ) | |||||||||
| 163 | ->will_done(); | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 1 | ▶ | sub expect_sysread ( $self, @args ) | ||||||||
|
Subroutine
expect_sysread: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||
| 167 | { | ||||||||||
| 168 | 1 | my ( $fh, $len ) = @args; | |||||||||
| 169 | 1 | ▶ | if( @args == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | carp "->expect_sysread with one argument is now deprecated"; | |||||||||
| 171 | 0 | ( $fh, $len ) = ( Test::Deep::ignore(), @args ); | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 1 | return $controller->expect( sysread => $fh, $len ); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 1 | ▶ | sub expect_syswrite ( $self, @args ) | ||||||||
|
Subroutine
expect_syswrite: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||
| 178 | { | ||||||||||
| 179 | 1 | my ( $fh, $bytes ) = @args; | |||||||||
| 180 | 1 | ▶ | if( @args == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | 0 | carp "->expect_syswrite with one argument is now deprecated"; | |||||||||
| 182 | 0 | ( $fh, $bytes ) = ( Test::Deep::ignore(), @args ); | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 1 | return $controller->expect( syswrite => $fh, $bytes ) | |||||||||
| 186 | ->will_done( length $bytes ); | ||||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | 1 | ▶ | sub expect_sysread_anyfh ( $self, @args ) | ||||||||
|
Subroutine
expect_sysread_anyfh: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 190 | { | ||||||||||
| 191 | 1 | $self->expect_sysread( Test::Deep::ignore() => @args ); | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | 1 | ▶ | sub expect_syswrite_anyfh ( $self, @args ) | ||||||||
|
Subroutine
expect_syswrite_anyfh: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 195 | { | ||||||||||
| 196 | 1 | $self->expect_syswrite( Test::Deep::ignore() => @args ); | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | =head1 METHODS | ||||||||||
| 200 | |||||||||||
| 201 | =cut | ||||||||||
| 202 | |||||||||||
| 203 | =head2 controller | ||||||||||
| 204 | |||||||||||
| 205 | $controller = Test::Future::IO->controller; | ||||||||||
| 206 | |||||||||||
| 207 | Returns the control object, on which the various C<expect_*> methods and | ||||||||||
| 208 | C<check_and_clear> can be invoked. | ||||||||||
| 209 | |||||||||||
| 210 | =cut | ||||||||||
| 211 | |||||||||||
| 212 | 7 | ▶ | sub controller ( $ ) { __PACKAGE__ } | ||||||||
|
Subroutine
controller: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 213 | |||||||||||
| 214 | =head2 check_and_clear | ||||||||||
| 215 | |||||||||||
| 216 | $controller->check_and_clear( $name ); | ||||||||||
| 217 | |||||||||||
| 218 | Checks that by now, every expected method has been called, and emits a new | ||||||||||
| 219 | test output line via L<Test::Builder>. Regardless, the expectations are also | ||||||||||
| 220 | cleared out ready for the start of the next test. | ||||||||||
| 221 | |||||||||||
| 222 | =cut | ||||||||||
| 223 | |||||||||||
| 224 | 19 | ▶ | sub check_and_clear ( $, $name ) | ||||||||
|
Subroutine
check_and_clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 225 | { | ||||||||||
| 226 | 19 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||
| 227 | 19 | $controller->check_and_clear( $name ); | |||||||||
| 228 | } | ||||||||||
| 229 | |||||||||||
| 230 | =head2 use_sysread_buffer | ||||||||||
| 231 | |||||||||||
| 232 | $controller->use_sysread_buffer( $fh ); | ||||||||||
| 233 | |||||||||||
| 234 | I<Since version 0.05.> | ||||||||||
| 235 | |||||||||||
| 236 | This method enables a read buffer for a given filehandle, that provides an | ||||||||||
| 237 | alternative means of testing reading on a filehandle than using | ||||||||||
| 238 | C<expect_sysread>. Once enabled, C<< Future::IO->sysread >> calls on the given | ||||||||||
| 239 | filehandle handled internally by the test controller. | ||||||||||
| 240 | |||||||||||
| 241 | The sysread buffer is initially empty, and can be written to by | ||||||||||
| 242 | L</write_sysread_buffer>. | ||||||||||
| 243 | |||||||||||
| 244 | This is provided using a C<Test::ExpectAndCheck::Future> C<< ->whenever >> | ||||||||||
| 245 | expectation, which is returned by this method. This is useful in case you want | ||||||||||
| 246 | to call the C<< ->indefinitely >> method on it, meaning it will survive past | ||||||||||
| 247 | calls to L</check_and_clear>. | ||||||||||
| 248 | |||||||||||
| 249 | $controller->use_sysread_buffer( "FH" ) | ||||||||||
| 250 | ->indefinitely; | ||||||||||
| 251 | |||||||||||
| 252 | =cut | ||||||||||
| 253 | |||||||||||
| 254 | 2 | ▶ | sub use_sysread_buffer ( $self, $fh ) | ||||||||
|
Subroutine
use_sysread_buffer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 255 | { | ||||||||||
| 256 | 2 | require Future::Buffer; | |||||||||
| 257 | |||||||||||
| 258 | # Not //= so that each test gets a new buffer | ||||||||||
| 259 | 2 | my $buffer = $sysread_buffers{$fh} = Future::Buffer->new; | |||||||||
| 260 | |||||||||||
| 261 | return $controller->whenever( sysread => $fh, Test::Deep::ignore() ) | ||||||||||
| 262 | 2 | ▶ | ->will_return_using( sub ( $args ) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 263 | 2 | return $buffer->read_atmost( $args->[1] ); | |||||||||
| 264 | 2 | }); | |||||||||
| 265 | } | ||||||||||
| 266 | |||||||||||
| 267 | =head2 write_sysread_buffer | ||||||||||
| 268 | |||||||||||
| 269 | $controller->write_sysread_buffer( $fh, $data ); | ||||||||||
| 270 | |||||||||||
| 271 | I<Since version 0.05.> | ||||||||||
| 272 | |||||||||||
| 273 | Appends more data to the sysread buffer previously established by the | ||||||||||
| 274 | L</use_sysread_buffer>. | ||||||||||
| 275 | |||||||||||
| 276 | Typically this is performed either initially as part of test setup, or later | ||||||||||
| 277 | as a side-effect of other expectations completing. | ||||||||||
| 278 | |||||||||||
| 279 | For example: | ||||||||||
| 280 | |||||||||||
| 281 | $controller->use_sysread_buffer( "FH" ); | ||||||||||
| 282 | |||||||||||
| 283 | $controller->expect_syswrite( "FH", "Question?\n" ) | ||||||||||
| 284 | ->will_write_sysread_buffer_later( "FH", "Answer!\n" ); | ||||||||||
| 285 | |||||||||||
| 286 | =cut | ||||||||||
| 287 | |||||||||||
| 288 | 2 | ▶ | sub write_sysread_buffer ( $self, $fh, $data ) | ||||||||
|
Subroutine
write_sysread_buffer: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 289 | { | ||||||||||
| 290 | 2 | ▶ | my $buffer = $sysread_buffers{$fh} or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 291 | croak "Filehandle $fh is not managed by a Test::Future::IO buffer"; | ||||||||||
| 292 | |||||||||||
| 293 | 2 | $buffer->write( $data ); | |||||||||
| 294 | } | ||||||||||
| 295 | |||||||||||
| 296 | { | ||||||||||
| 297 | package Test::Future::IO::_Controller; | ||||||||||
| 298 | 8 | ▶ | use base qw( Test::ExpectAndCheck::Future ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 299 | 8 | ▶ | use constant EXPECTATION_CLASS => "Test::Future::IO::_Expectation"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 300 | } | ||||||||||
| 301 | |||||||||||
| 302 | { | ||||||||||
| 303 | 8 | package Test::Future::IO::_Expectation; | |||||||||
| 304 | 8 | ▶ | use base qw( Test::ExpectAndCheck::Future::_Expectation ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 305 | |||||||||||
| 306 | 0 | ▶ | sub will_write_sysread_buffer ( $self, $fh, $data ) | ||||||||
|
Subroutine
will_write_sysread_buffer: not called CC=1 SCAR=6.9
| |||||||||||
| 307 | { | ||||||||||
| 308 | 0 | ▶ | return $self->will_also( sub () { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 309 | 0 | Test::Future::IO->write_sysread_buffer( $fh, $data ); | |||||||||
| 310 | 0 | }); | |||||||||
| 311 | } | ||||||||||
| 312 | |||||||||||
| 313 | 1 | ▶ | sub will_write_sysread_buffer_later ( $self, $fh, $data ) | ||||||||
|
Subroutine
will_write_sysread_buffer_later: called CC=1 SCAR=0.0
| |||||||||||
| 314 | { | ||||||||||
| 315 | 1 | ▶ | return $self->will_also_later( sub () { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 316 | 1 | Test::Future::IO->write_sysread_buffer( $fh, $data ); | |||||||||
| 317 | 1 | }); | |||||||||
| 318 | } | ||||||||||
| 319 | } | ||||||||||
| 320 | |||||||||||
| 321 | =head1 TODO | ||||||||||
| 322 | |||||||||||
| 323 | =over 4 | ||||||||||
| 324 | |||||||||||
| 325 | =item * | ||||||||||
| 326 | |||||||||||
| 327 | Provision of a mock filehandle object to assist unit tests. | ||||||||||
| 328 | |||||||||||
| 329 | =back | ||||||||||
| 330 | |||||||||||
| 331 | =head1 AUTHOR | ||||||||||
| 332 | |||||||||||
| 333 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 334 | |||||||||||
| 335 | =cut | ||||||||||
| 336 | |||||||||||
| 337 | 8 | 0x55AA; | |||||||||