tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

O wise ones,

I have 2 questions, a specific question about Text::xSV and a more general question about testing.

I think I may have discovered a minor bug in xSV, as shown in the script below. My first question is, is this indeed a bug or did I misunderstand the documentation.

But more importantly, I want to add testing to my coding process. How could this type of "failure" of object creation, if that's what it is, be rewritten with testing... so that I could report it the right way... and eventually begin testing sexier things, like PUGS :)

A specific question I have about testing is, should I not use the strictures with testing, because that would cause the test script not to compile before failing?

use warnings; use strict; use Text::xSV; #documentation in http://search.cpan.org/~tilly/Text-xS +V-0.14/lib/Text/xSV.pm #filename set in new(). works. my $csv1 = Text::xSV->new( filename => "filename.txt", header => ["partner", "kampagne", "keywordCluster", "keyword", "clicks", "leads", "orders", "jaronVerguetung", "partnerVerguetung", "profit"], ); $csv1->set_sep(';'); #filename set in set_filename. Doesn't work. #error is "global symbol... requires explicit package name. Fails on +set_filename. But not on set_sep. Weird. my $csv2 = Text::xSV->new( header => ["partner", "kampagne", "keywordCluster", "keyword", "clicks", "leads", "orders", "jaronVerguetung", "partnerVerguetung", "profit"], ); $csv2->set_sep(';'); $cvs2->set_filename("popularixCombined.csv");
I am reading the documentation for Test::Simple, Test::More, and the testing tutorial included with the core install of perl. If there's anything else worth taking a look at, or thinking about, would appreciate insight from those with more experience than me.

UPDATE: Deleted filename => "filename.txt", from creation of second object; didn't mean to have this. Still get the error though. Thanks Corion.

UPDATE 2: Rewrote this using Test::Simple, see below.

UPDATEe 3: This was a variable name typo issue. Well, at least I got started learning the testing stuff... gulp..... /me slinks off.....

Replies are listed 'Best First'.
Re: xSV question, and general testing question.
by Corion (Patriarch) on May 18, 2005 at 09:05 UTC

    Tests are programs like any other program and thus should use strict; wherever possible. I drop strict 'refs' from time to time because it's easier and more clear (in a short script) to overwrite globs with a symbolic reference, but strict prevents me from making stupid typo errors.

    If you think your script does not compile with strict in effect, theree is always eval STRING, but in your case, eval BLOCK should already be enough, as it is not a compilation/syntax error but a runtime error. I would rewrite the second test as:

    my $csv2 = Text::xSV->new( filename => 'filename.txt', ... ); my $new_filename = "popularixCombined.csv"; eval { $csv2->set_filename($new_filename) }; is( $@, undef, "Changing the filename does not raise an error"); is( $csv->get_filename, $new_filename, "... and sets the new filename" + );
Re: xSV question, and general testing question.
by dragonchild (Archbishop) on May 18, 2005 at 13:07 UTC
    Corion fixed the immediate problem, which is good. Here's how I would go about doing your tests. Everything is tested.
    use strict; # no "use warnings" unless I guarantee this is 5.6+. # CPAN modules may not have that guarantee. # If Makefile.PL has a check for 5.6, I can add this back in. use Test::More tests => 6; my $CLASS = 'Text::xSV'; use_ok( $CLASS ); my $obj; $obj = $CLASS->new( ... ); isa_ok( $obj ); ok( $obj->set_sep(';'), "Set separator" ); $obj = $CLASS->new( ... ); isa_ok( $obj ); ok( $obj->set_sep(';'), "Set separator" ); ok( $obj->set_filename("popularixCombined.csv"), "Set filename" );

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: xSV question, and general testing question.
by tphyahoo (Vicar) on May 18, 2005 at 09:36 UTC
    OK, here's the script using Test::Simple, and using using eval as corion suggested. However, it still fails on compilation, complaining that $csv2 needs an explicit package name, on the last line, set_filename. If I comment this line out, it works.

    Is there any way I can create a test script that will run the first three tests and fail out on the final test?

    And why is this final test failing, anyway?

    use warnings; use strict; use Text::xSV; #documentation in http://search.cpan.org/~tilly/Text-xS +V-0.14/lib/Text/xSV.pm use Test::Simple tests => 4; #filename set in new(). works. my $csv1 = Text::xSV->new( filename => "filename.txt", header => ["partner", "kampagne", "keywordCluster", "keyword", "clicks", "leads", "orders", "jaronVerguetung", "partnerVerguetung", "profit"], ); ok( ( defined($csv1) ) , 'Csv object created using filename => "filena +me.txt"'); ok( $csv1->set_sep(';') , "Separator set for this object." ); #filename set in set_filename. Doesn't work. #error is "global symbol... requires explicit package name. Fails on +set_filename. But not on set_sep. Weird. my $csv2 = Text::xSV->new(); ok( ( defined($csv2) ) , 'Csv object created with new, without using f +ilename => "filename.txt"'); ok( ( eval { $csv2->set_filename("popularixCombined.csv") } ) ,"Filena +me set for this object.");
    UPDATE: Corrected the variable name. All four tests now run. Oops.

      That's what use strict; is for:

      Compare $cvs and $csv...

      From time to time, Perls error messages actually tell you what is wrong, especially when it's a variable name ...

Re: xSV question, and general testing question.
by mrborisguy (Hermit) on May 18, 2005 at 13:48 UTC
    more importantly, you declare $csv2 and use it for a while, and then suddenly for set_filename, you use $cvs2 ( 'v' and 's' switched ). Could this have