Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

It doesn't take a tremendous amount of work to convert a script into a modulino. And by so doing, if there are any subroutines within those scripts, they would become testable more in isolation. I wouldn't put the level of work into the category of rewrite, more in the category of refactor, and often it's a pretty simple refactor.

Consider the following script:

#!/usr/bin/env perl use strict; use warnings; use IO::Prompt::Tiny qw(prompt); my $user = prompt("What is your name? "); greet($user); exit(0); sub make_hello { return "Hello"; } sub make_subject { return shift; } sub greeting { my $who = shift; return make_hello() . ' ' . make_subject($who) } sub greet { print greeting(shift), "\n"; }

In its current form you would have to invoke the script, and this can be done from within a test, capturing the output and even providing input. But it's not ideal. Let's convert it to a modulino.

#!/usr/bin/env perl package MyScript; use strict; use warnings; use IO::Prompt::Tiny qw(prompt); main() if !caller(); exit(0); sub main { my $user = prompt("What is your name? "); greet($user); } sub make_hello { return "Hello"; } sub make_subject { return shift; } sub greeting { my $who = shift; return make_hello() . ' ' . make_subject($who) } sub greet { print greeting(shift), "\n"; } 1;

Now you can test it more easily:

use strict; use warnings; use Test::More; require '/path/to/myscript.pl'; can_ok MyScript => qw(greet greeting make_subject make_hello main); is MyScript::make_hello, 'Hello', 'Got Hello'; is MyScript::make_subject('Monk'), 'Monk', 'Got correct subject.'; is MyScript::greeting('Monk'), 'Hello Monk', 'Got a correct greeting.' +; use Capture::Tiny qw(capture_stdout); my $resp = capture_stdout {MyScript::greet('Monk')}; is $resp, "Hello Monk\n", 'Got correct output in STDOUT.'; done_testing();

The testing code is untested. ;)

Now you've tested at least minimally each of the main components except for main() itself, and if you wanted to, you could do that too. And all it required was adding the main() unless caller();, wrapping the top level code in a main subroutine, and putting a 1; at the end of the modulino. It really adds about four or five lines of code, and some indenting. You could almost write a script to apply it to all your scripts, and then to do a compile-check to verify they still compile. Keep backups of the original, unaltered scripts, though. ;)


Dave


In reply to Re: Needing help on testing by davido
in thread Needing help on testing by Zenzizenzizenzic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2019-10-19 00:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?