NAME
Test::MockCommand::Result - stores and emulates commands
SYNOPSIS
# as called by Test::MockCommand::Recorder
my %args = (
command => 'ls -l',
function => 'readpipe', # or 'exec', 'system' or 'open',
arguments => \@_,
caller => [ caller() ]
);
my $result = Test::MockCommand::Result->new(%args);
# as called by Test::MockCommand
my $matches = $result->matches(%args);
my $return_value = $result->handle(
%args,
all_results => [ $all, $results, $matched, $including, $yourself ]
);
DESCRIPTION
This class which is the 'result' class used, created by Test::MockCommand::Recorder and queried by Test::MockCommand. It stores and replays the results of commands.
A result class should support any special features of the recorder class that creates it.
CONSTRUCTOR
- new(%args)
-
This is called by Test::MockCommand::Recorder as part of its
handlemethod. It passes on all the same arguments it was given by the main Test::MockCommand framework. See "handle" in Test::MockCommand::Recorder for details of each argument.
METHODS
- $match_score = $result->matches(%args)
-
This method takes a list of criteria and returns a score of how well this object matches those critera. It's called by Test::MockCommand, both as part of its search for the right command to emulate, and also via the front-end
findmethod. As such it may contain zero or more criteria. If no criteria are provided, the result should always be "this matches".Return 0 to indicate no match, 1 to indicate a match, and higher numbers mean the result "matches better". If more than one result matches the given criteria, they will be sorted by this match score, highest first, and in the case of picking a result to emulate a command, the first in the list will be chosen.
The criteria are the same as listed in "handle" in Test::MockCommand::Recorder, however you can presume that the criteria
functionandcommand, if they are present, have been used to filter out results which don't match. - $return_value = $result->handle(%args)
-
This should emulate the function detailed in the arguments and return the value that the function itself is expected to return. The arguments are the same as listed in "handle" in Test::MockCommand::Recorder, and as with that method, any results for
readpipeshould be returned as a scalar, not a list, as the framework will break them into a list if needed. - $result->append_input_data($string)
-
Appends more text to the
input_dataattribute. Called by the tied filehandle'sWRITEmethod while emulating anopencall. - $result->append_output_data($string)
-
Appends more text to the
output_dataattribute. Called by the tied filehandle'sWRITEmethod while emulating anopencall. - $result->create_tied_fh()
-
Creates a Test::MockCommand::TiedFH filehandle attached to this object.
ATTRIBUTES
$attribute_value = $result->attribute_name()
$result->attribute_name($new_attribute_value)
Each result object can store a number of attributes, which you can get and set via accessor methods as shown above. The attributes are as follows:
- command
-
Roughly the command being run. Use the
functionandargumentsattributes for a completely accurate version. For examplesystem('ls', 'foo', 'bar');andsystem('ls', 'foo bar')will generate the same command attribute, "ls foo bar". - function
-
The function that generated this result. This will be
exec,open,readpipeorsystem. - arguments
-
An arrayref to the original arguments of the function. However, the first argument to open() is always turned to undef, to allow for the result to be saved and loaded into another program where a named filehandle reference might not exist.
- return_value
-
The return value originally captured from this function
- cwd
-
The current working directory at the time this function was called.
- input_data
-
Any input data sent into the command. (
openonly) - output_data
-
The output generated by the command (
openonly) - exit_code
-
The value of
$?after running the command. - all_results
-
Available only while
handleis being called. It's a list of all possible candidates for execution, including this result.