Mock::...:Class(3pm)

πŸ“¦
version 1.111 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Mock::Quick::Class β€” the perl manpage but make it cute πŸ’–

okay bestie, gather round, because today we're talking about Mock::Quick::Class, which is the Mock::Quick module that handles class mocking. yes. class mocking. it's giving fake-it-till-you-make-it but for packages. πŸŽ€


✨ NAME ✨

Mock::Quick::Class β€” class mocking for Mock::Quick. that's the whole vibe. πŸ’…

🌸 DESCRIPTION 🌸

it provides class mocking for Mock::Quick. short, sweet, iconic, no notes. ✨

πŸ¦‹ SYNOPSIS πŸ¦‹

πŸ’œ IMPLEMENT A CLASS πŸ’œ

so this one implements a class at the namespace you hand it through the -implement argument. the class must not already be loaded, okay? like, do not. and once you've done it, the real class is gonna be blocked from loading until you call undefine() on the control object. periodt. πŸ‘‘

use Mock::Quick::Class;

my $control = Mock::Quick::Class->new(
    -implement => 'My::Package',

    # Insert a generic new() method (blessed hash)
    -with_new => 1,

    # Inheritance
    -subclass => 'Some::Class',
    # Can also do
    -subclass => [ 'Class::A', 'Class::B' ],

    # generic get/set attribute methods.
    -attributes => [ qw/a b c d/ ],

    # Method that simply returns a value.
    simple => 'value',

    # Custom method.
    method => sub { ... },
);

my $obj = $control->package->new;
# OR
my $obj = My::Package->new;

# Override a method
$control->override( foo => sub { ... });

# Restore it to the original
$control->restore( 'foo' );

# Remove the namespace we created, which would allow the real thing to load
# in a require or use statement.
$control->undefine();

and bestie, you can also use the implement method instead of new, because options are a love language πŸ’–:

use Mock::Quick::Class;

my $control = Mock::Quick::Class->implement(
    'Some::Package',
    %args
);

🧚 ANONYMOUS MOCKED CLASS 🧚

this is the one for when you just need to generate a class and honestly the package name does not matter. main character energy with no name. πŸ’«

this happens when the -takeover and -implement arguments are both omitted. iykyk.

use Mock::Quick::Class;

my $control = Mock::Quick::Class->new(
    # Insert a generic new() method (blessed hash)
    -with_new => 1,

    # Inheritance
    -subclass => 'Some::Class',
    # Can also do
    -subclass => [ 'Class::A', 'Class::B' ],

    # generic get/set attribute methods.
    -attributes => [ qw/a b c d/ ],

    # Method that simply returns a value.
    simple => 'value',

    # Custom method.
    method => sub { ... },
);

my $obj = $control->package->new;

# Override a method
$control->override( foo => sub { ... });

# Restore it to the original
$control->restore( 'foo' );

# Remove the anonymous namespace we created.
$control->undefine();

πŸ”₯ TAKING OVER EXISTING/LOADED CLASSES πŸ”₯

this is when the class already exists and you're just, like, moving in. slay.

use Mock::Quick::Class;

my $control = Mock::Quick::Class->takeover( 'Some::Package' );

# Override a method
$control->override( foo => sub { ... });

# Restore it to the original
$control->restore( 'foo' );

# Destroy the control object and completely restore the original class
# Some::Package.
$control = undef;

you can also do this through new() πŸ’…:

use Mock::Quick::Class;

my $control = Mock::Quick::Class->new(
    -takeover => 'Some::Package',
    %overrides
);

πŸ‘‘ ACCESSING THE CONTROL OBJECY πŸ‘‘

(yes it says OBJECY, we love a typo, it's part of the lore now 🫢)

while the control object exists, you can reach it through "YOUR::PACKAGE-"MQ_CONTROL()>. and important: this method disappears the moment the control object you're tracking falls out of scope. it's giving ghosting. 😭

example (taken from Class.t):

$obj = $CLASS->new( -takeover => 'Baz' );
$obj->override( 'foo', sub {
    my $class = shift;
    return "PREFIX: " . $class->MQ_CONTROL->original( 'foo' )->();
});

is( Baz->foo, "PREFIX: foo", "Override and accessed original through MQ_CONTROL" );
$obj = undef;

is( Baz->foo, 'foo', 'original' );
ok( !Baz->can('MQ_CONTROL'), "Removed control" );

πŸ’… METHODS πŸ’…

  • $package = $obj->package() β€” get the name of the package this object is controlling. that's it, that's the method. πŸ“¦
  • $bool = $obj->is_takeover() β€” check if this control object was created to takeover an existing class. true/false, no cap. βœ…
  • $bool = $obj->is_implement() β€” check if this control object was created to implement a class. ✨
  • $data = $obj->metrics() β€” returns a hash where the keys are method names and the values are the number of times the method has been called. and here's the tea: when a method is altered or removed, the key is deleted. gone. πŸ—‘οΈ
  • $obj->override( name => sub { ... }) β€” override a method. simple as. πŸ”§
  • $obj->original( $name ); β€” get the original method as a coderef. note: the first time you call this it finds and remembers the value of package->can( $name ). which means if you modified or replaced the method without using Mock::Quick before calling this, you'll get the updated method β€” not the true original. 😬

the override() method will call this first, so the original method is cached and ready for restore(). and once a value is set, it is never replaced or cleared. it's permanent. it's committed. πŸ’

  • $obj->restore( $name ) β€” restore a method. (also resets metrics, cute little two-for-one πŸŽ€)
  • $obj->undefine() β€” undefine the package controlled by the control. bye bestie πŸ‘‹

🫢 AUTHORS 🫢

  • Chad Granum exodist7@gmail.com
  • Glen Hinkle glen@empireenterprises.com

Copyright (C) 2011 Chad Granum

Mock-Quick is free software; Standard perl licence.

Mock-Quick is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.


okay that's the whole page, you're welcome, go forth and mock responsibly 🌸✨ this was Mock::Quick::Class for perl v5.34.0, dated 2022-06-15, and yes we're bringing back this energy, periodt πŸ’–πŸ¦‹πŸ’…

πŸ“¦ where this page comes from