/usr/share/perl5/DBD/Mock/Session.pm is in libdbd-mock-perl 1.45-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | package DBD::Mock::Session;
use strict;
use warnings;
my $INSTANCE_COUNT = 1;
# - Class - #
sub new {
my $class = shift;
my $name = ref( $_[0] ) ? "Session $INSTANCE_COUNT" : shift;
$INSTANCE_COUNT++;
$class->_verify_states( $name, @_ );
bless {
name => $name,
states => \@_,
state_index => 0
}, $class;
}
sub _verify_state {
my ( $class, $state, $index, $name ) = @_;
die "You must specify session states as HASH refs"
if ref($state) ne 'HASH';
die "Bad state '$index' in DBD::Mock::Session ($name)"
if not exists $state->{statement}
or not exists $state->{results};
my $stmt = $state->{statement};
my $ref = ref $stmt;
die "Bad 'statement' value '$stmt' in DBD::Mock::Session ($name)",
if ref($stmt) ne ''
and $ref ne 'CODE'
and $ref ne 'Regexp';
}
sub _verify_states {
my ( $class, $name, @states ) = @_;
die "You must specify at least one session state"
if scalar @states == 0;
for ( 0 .. scalar @states - 1 ) {
$class->_verify_state( $states[$_], $_, $name );
}
}
# - Instance - #
sub name {
my $self = shift;
$self->{name};
}
sub reset {
my $self = shift;
$self->{state_index} = 0;
}
sub current_state {
my $self = shift;
my $idx = $self->{state_index};
return $self->{states}[$idx];
}
sub has_states_left {
my $self = shift;
return $self->{state_index} < $self->_num_states;
}
sub verify_statement {
my ( $self, $got ) = @_;
unless ( $self->has_states_left ) {
die "Session states exhausted, only '"
. $self->_num_states
. "' in DBD::Mock::Session ($self->name})";
}
my $state = $self->current_state;
my $expected = $state->{statement};
my $ref = ref($expected);
if ( $ref eq 'Regexp' and $got !~ /$expected/ ) {
die "Statement does not match current state (with Regexp) in "
. "DBD::Mock::Session ($self->{name})\n"
. " got: $got\n"
. " expected: $expected",
}
if ( $ref eq 'CODE' and not $expected->( $got, $state ) ) {
die "Statement does not match current state (with CODE ref) in "
. "DBD::Mock::Session ($self->{name})";
}
if ( not $ref and $got ne $expected ) {
die "Statement does not match current state in "
. "DBD::Mock::Session ($self->{name})\n"
. " got: $got\n"
. " expected: $expected";
}
}
sub results_for {
my ( $self, $statment ) = @_;
$self->_find_state_for($statment)->{results};
}
sub verify_bound_params {
my ( $self, $params ) = @_;
my $current_state = $self->current_state;
if ( exists ${$current_state}{bound_params} ) {
my $expected = $current_state->{bound_params};
if ( scalar @$expected != scalar @$params ) {
die "Not the same number of bound params in current state in "
. "DBD::Mock::Session ($self->{name})\n"
. " got: @{$params}"
. " expected: @{$expected}";
}
for ( 0 .. scalar @{$params} - 1 ) {
$self->_verify_bound_param( $params->[$_], $expected->[$_], $_ );
}
}
# and make sure we go to
# the next statement
$self->{state_index}++;
}
sub _find_state_for {
my ( $self, $statement ) = @_;
foreach ( $self->_remaining_states ) {
my $stmt = $_->{statement};
my $ref = ref($stmt);
return $_ if ( $ref eq 'Regexp' and $statement =~ /$stmt/ );
return $_ if ( $ref eq 'CODE' and $stmt->( $statement, $_ ) );
return $_ if ( not $ref and $stmt eq $statement );
}
die "Statement '$statement' not found in session ($self->{name})";
}
sub _num_states {
my $self = shift;
scalar @{ $self->{states} };
}
sub _remaining_states {
my $self = shift;
my $start_index = $self->{state_index};
my $end_index = $self->_num_states - 1;
@{ $self->{states} }[ $start_index .. $end_index ];
}
sub _verify_bound_param {
my ( $self, $got, $expected, $index ) = @_;
no warnings;
my $ref = ref $expected;
if ( $ref eq 'Regexp' and $got !~ /$expected/ ) {
die "Bound param $index do not match (using regexp) "
. "in current state in DBD::Mock::Session ($self->{name})"
. " got: $got\n"
. " expected: $expected";
}
if ( $got ne $expected ) {
die "Bound param $index do not match "
. "in current state in DBD::Mock::Session ($self->{name})\n"
. " got: $got\n"
. " expected: $expected";
}
}
1;
|