Permalink
Browse files

added multiple overrides and restore w/tests

  • Loading branch information...
1 parent 780f3f2 commit 143f8e62715eabb39f4064f37db043f18877b3b0 @tempire tempire committed Feb 15, 2011
Showing with 34 additions and 17 deletions.
  1. +26 −17 lib/Mock/Quick/Class.pm
  2. +8 −0 t/Class.t
View
@@ -89,28 +89,37 @@ sub _is_sub_ref {
sub override {
my $self = shift;
my $package = $self->package;
- my ( $name, $orig_value ) = @_;
- my $real_value = _is_sub_ref( $orig_value )
- ? $orig_value
- : sub { $orig_value };
-
- my $original = $package->can( $name );
- $self->{$name} ||= $original;
- inject( $package, $name, $real_value );
- return $original;
+ my %pairs = @_;
+ my @originals;
+
+ while( my ( $name, $orig_value ) = each %pairs) {
+
+ my $real_value = _is_sub_ref( $orig_value )
+ ? $orig_value
+ : sub { $orig_value };
+
+ my $original = $package->can( $name );
+ $self->{$name} ||= $original;
+ inject( $package, $name, $real_value );
+
+ push @originals, $original;
+ }
+ return @originals;
}
sub restore {
my $self = shift;
- my ( $name ) = @_;
- my $original = $self->{$name};
- if ( $original ) {
- my $sub = _is_sub_ref( $original ) ? $original : sub { $original };
- inject( $self->package, $name, $sub );
- }
- else {
- $self->clear( $name );
+ while( my $name = shift @_) {
+ my $original = $self->{$name};
+
+ if ( $original ) {
+ my $sub = _is_sub_ref( $original ) ? $original : sub { $original };
+ inject( $self->package, $name, $sub );
+ }
+ else {
+ $self->clear( $name );
+ }
}
}
View
@@ -64,6 +64,14 @@ tests override => sub {
is( $obj->package->bar, 'xxx', "overriden" );
$obj->restore( 'bar' );
ok( !$obj->package->can( 'bar' ), "original value is nill" );
+
+ # Multiple overrides
+ $obj->override( foo => sub { 'foo' }, bar => sub { 'bar' } );
+ is $obj->package->foo => 'foo', "overriden";
+ is $obj->package->bar => 'bar', "overriden";
+ $obj->restore(qw/ foo bar /);
+ is $obj->package->foo => 'bar', "original value";
+ ok !$obj->package->can('bar'), "original value is nil";
};
tests undefine => sub {

0 comments on commit 143f8e6

Please sign in to comment.