Mock::...:Class(3pm)
π 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 ofpackage->can( $name ). which means if you modified or replaced the method without usingMock::Quickbefore 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 π
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
- debian bookworm: libmock-quick-perl 1.111-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmock-quick-perl 1.111-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmock-quick-perl 1.111-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmock-quick-perl 1.111-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmock-quick-perl 1.111-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms