Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocked subs invoked as plain functions are not being unloaded #19

Open
slightlytwisted opened this issue Mar 29, 2017 · 2 comments
Open

Comments

@slightlytwisted
Copy link

I'm running into an issue that is probably best illustrated with code, below. I'm not sure if I'm just using the class wrong, or if this is a bug in the Mock::Quick package:

#!/usr/bin/env perl

use Mock::Quick;

test1();
test2();

sub test1 {
  my $control = qimplement 'Foo' => (
      bar => sub { say STDOUT 'I am in test1' },
    );

  Foo::bar();

  $control->undefine();
  $control = undef;
}

sub test2 {
  my $control = qimplement 'Foo' => (
      bar => sub { say STDOUT 'I am in test2' },
    );

  Foo::bar();

  $control->undefine();
  $control = undef;
}

Expected output:

test1
test2

Actual output:

test1
test1

Mock::Quick version: 1.111
Perl Version: 5.22.1

@slightlytwisted
Copy link
Author

Investigating further, it looks like this issue only occurs when the mocked sub is invoked as a plain function (e.g. Foo::bar()). The issue does not occur when the mocked sub is invoked as an object method (e.g. Foo->new()->bar()), as shown in the code below.

I will update the issue title to reflect this.

#!/usr/bin/env perl

use Mock::Quick;

test1();
test2();

sub test1 {
  my $control = qimplement 'Foo' => (
      -with_new => 1,
      bar => sub { say STDOUT 'I am in test1' },
    );

  $control->package->new->bar();
}

sub test2 {
  my $control = qimplement 'Foo' => (
      -with_new => 1,
      bar => sub { say STDOUT 'I am in test2' },
    );

  $control->package->new->bar();
}

Output:

test1
test2

@slightlytwisted slightlytwisted changed the title Implemented classes are not fully unloaded, causing side effects Mocked subs invoked as plain functions are not being unloaded Mar 29, 2017
@slightlytwisted
Copy link
Author

slightlytwisted commented Mar 29, 2017

Doing some more investigation. It seems that calling undef(*{"$package\::"}) (as on Mock::Quick::Class line 241), causes some unexpected behavior. Consider the following:

#!/usr/bin/env perl

no warnings 'redefine';

*{'Foo::Bar'} = sub { say STDOUT 'test1' };

Foo::Bar();

undef(*{'Foo::'});

*{'Foo::Bar'} = sub { say STDOUT 'test2' };

Foo::Bar();

Which outputs the following:

test1
test1

Removing the line undef(*{'Foo::'}) or replacing it with undef(*{'Foo::Bar'}) yields the expected (at least to me) behavior of outputting test1\ntest2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant