Fuckin-Lazy

 view release on metacpan or  search on metacpan

lib/Fuckin/Lazy.pm  view on Meta::CPAN

package Fuckin::Lazy;
use strict;
use warnings;

use Data::Dumper;
use base 'Exporter';
use Carp qw/croak/;

our $VERSION = "0.000002";
our @EXPORT = ('LAZY');

sub produce_data {
    my ($struct, $line, $match) = @_;
    local $Data::Dumper::Purity   = 1;
    local $Data::Dumper::Useqq    = 1;
    local $Data::Dumper::Deepcopy = 1;
    local $Data::Dumper::Sortkeys = 1;
    local $Data::Dumper::Indent   = 0;

    my $data = Dumper($struct);

    $data =~ s/\$VAR1 = //;
    $data =~ s/;$//;
    chomp($data);

    return $data;
}

sub LAZY {
    my ($struct) = @_;

    my ($caller, $file, $line) = caller();

    open(my $fh, '<', $file) || die "Could not open $file: $!";

    my $updated = 0;
    my @lines;
    while (my $line = <$fh>) {
        if ($line =~ m/(LAZY|Fuckin'Lazy|Fuckin::Lazy)\s*\(/) {
            my $match = $1;
            croak "$1() must be called with parentheses, and must be given a scalar variable for an arg."
                unless $line =~ m/$match\(\s*\$[0-9A-Za-z_]+\s*\)/;

            my $data = produce_data($struct, $line, $match);
            $line =~ s/$match\(\s*\$[0-9A-Za-z_]+\s*\)/$data/;
        }
        push @lines => $line;
    }

    close($fh);
    open($fh, '>', $file) || die "Cannot open $file for writing: $!";

    print $fh @lines;

    return $struct;
}

*Fuckin::Lazy = \&LAZY;

1;

__END__

=head1 NAME

Fuckin::Lazy - Lazy way to produce test structures (Or a very bad idea)

=head1 WARNING

Warning! Using the module would fall squarely into B<BAD PRACTICE>. You should
predict what structures your code produces and put them into the test yourself.
Simply dumping a result and testing against it is a bad idea. The only
exception to this policy is when writing tests to make sure the output does not
change from what it currently is.

=head1 SYNOPSIS

    use Test::More;
    use Fuckin::Lazy qw/LAZY/;

    my $foo = { a => 1, b => 2 };

    is_deeply($foo, Fuckin'Lazy($foo), "Foo");

    is_deeply(
        $foo,
        LAZY($foo),
        "Foo"
    );

    is_deeply($foo, LAZY($foo), "Foo");

    done_testing;

The first time you run the above test it will alter the test file itself to produce this:

    use Test::More;
    use Fuckin::Lazy qw/LAZY/;

    my $foo = { a => 1, b => 2 };

    is_deeply($foo, {"a" => 1,"b" => 2}, "Foo");

    is_deeply(
        $foo,



( run in 0.879 second using v1.01-cache-2.11-cpan-8dfa8b56332 )