Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I wrote a command line script to aid my review of trigonometry, and improve my understanding of Perl and its testing tools. My initial script accepts 2 sequences 3 integers (representing the degrees, minutes, and seconds of an angle. Positive and negative values are accepted), separated by any one of the basic arithmetic operations (+,-,*). It returns the correct arithmetic result in reduced form (ie. min or sec must be a positive integer from 0-59, with borrow/carry done appropriately).

Example input:
prompt> trg_calc 179 0 59 + 0 59 1 Reduced Answer: 180 0 0

My manual testing indicates it works as I intend. When I run perl -w and perl -c on the script, no problems occur. My goal was to write this with automated testing in mind. This is where I have problems. I am going by the example in the Perl Testing: A Developer's Notebook for testing scripts (vs. modules). FWIW, I have not read it in depth; I have skipped around trying to pick out what appears immediately useful. Here is my test file:

#!/usr/bin/env/perl use strict; use warnings; use Test::More 'no_plan'; my $test_path = "C:/Users/Greyhat/PDL_Old/trig/src"; require "$test_path/trig_calc.pl"; # Degree Addition Tests ok( main() eq 'Operation is undef.', '2 +. Tests invocation with empty arg list'); ok( main( '90 35 29 + 90 24 29' ) eq '180, 59, 58', '3 +. no carry needed' ); ok( main( '179 0 59 + 0 0 1' ) eq '179 1 0', '4 +. test if single carry works correctly' ); + ok( main( '179 0 59 + 0 59 1' ) eq '180 0 0', '5 +. tests if multiple carry works correctly' ); + ok( main( '-179 0 59 + 0 59 1' ) eq '-178 0 0 ', '6 +. test mainition with negatives' ); ok( main( '0 0 1 + 0 0 59' ) eq '0 1 0', '7 +. test if single reduce works correctly' ); ok( main( '90 180 270 + 0 180 90' ) eq '96 6 0', '8 +. tests if multiple reduce calls work correctly' );

With that, I get a host of uninstantiated variable errors, even for things that are declared in the source file, so it appears that my arguments are not making it all the way to the needed subroutine. My assumption is that calling the main function like this simulates the actions of someone invoking this from the command line. When I do use the script from the command line, it works as I expect, so there must be a gap in my understanding of the testing tools. The tests for reading the file in and the one for invoking with empty arguments, pass. But all of the other ones fail. Some of my concerns about the tests above:

  • 1. The expression is in quotes, when I would simply prefer to pass an array of numbers. I've tried that, but I still get syntax errors when I run the test file under perl -c trig_calc.t.
  • 2. How should I use return values in the *.pl file so the test tools have something to check? I am not exactly clear how the return values are used.
  • 3. How should I test for array equality in this particular case? I don't think numeric equality is correct here.
  • Thanks for the assistance.


    In reply to How to write testable command line script? by thechartist

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



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others wandering the Monastery: (4)
    As of 2024-04-19 04:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found