Thread: [Perlunit-users] Test-Unit on Win32: OutputDebugString at 'assert' failure time?
Status: Beta
Brought to you by:
mca1001
From: Phlip <pl...@sy...> - 2002-11-08 18:29:57
|
Perlies: I like unit tests. My employers like Win32, VC++, and Perl. So here I am writing an "acceptance test" rig for our VC++ code, and it calls Perl embedded in the C++ code. When an assertion fails, such as this one... $self -> assert ($result =~ /LW is god/); ...I want the VC++ Output window to contain the file name, line number, and spit-list of the failure: c:\projects\testage.pm:14: get a clue, homey... That way I can tap <F4> and go straight to the offending line in the editor. No more reading & GotoLine-ing. That's the computer's job. (Other Perl editors may support these kinds of features before the new Millenium is old...) So first I get OutputDebugString from here: Win32-API-OutputDebugString-0.03.tar.gz Now I feel like overriding Test::Unit::Debug::debug so it also pushes text out OutputDebugString: use Test::Unit::Debug; { package Test::Unit::Debug; *Test::Unit::Debug::debug = sub { my ($package, $filename, $line) = caller(); print $out "***************************************that's the facts, Jack!\n"; print $out @_ if $DEBUG{$package}; } } That's as far as I can decipher the documentation on global hostile takeover-style overrides. But even with "strict" and "-w" turned off, Perl still screams at me: "Subroutine debug redefined..." Then, if Perl think's it's "redefined" (past-tense), why don't Perl then call it? How am I expected to do this? (BTW I can't "inherit" Test::Unit::Debug and then overload because the other Test-Unit stuff won't call my derived class.) -- Phlip http://www.greencheese.org/NorovostokNovus -- All sensors report Patti having a very good time -- |
From: Phlip <pl...@sy...> - 2002-11-08 19:06:34
|
> If you're using Perl 5.6 or greater, the warnings pragma can help: > > use Test::Unit::Debug; > > { > no warnings 'redefine'; > Test::Unit::Debug::debug = sub { ... }; > } I thought that needed a star: use Test::Unit::Debug; { no warnings 'redefine'; package Test::Unit::Debug; *Test::Unit::Debug::debug = sub { my ($package, $filename, $line) = caller(); print $out "***************************************that's the facts, Jack!\n"; print $out @_ if $DEBUG{$package}; } } Several variations on that produce this: Variable "%DEBUG" is not imported at c:../ProgrammerTests/SystranTP.pm line 24. BEGIN not safe after errors--compilation aborted at c:../ProgrammerTests/Systran TP.pm line 369. -- Phlip http://www.greencheese.org/HatTrick |
From: Phlip <pl...@sy...> - 2002-11-08 20:20:32
|
> use Test::Unit::Debug; > { > no warnings 'redefine'; > package Test::Unit::Debug; > *Test::Unit::Debug::debug = sub { > my ($package, $filename, $line) = caller(); > print $out "***************************************that's the > facts, Jack!\n"; > print $out @_ if $DEBUG{$package}; > } > } Trying to get that working exceeded my time box, so I went in for a hack. Firstly, 'debug' was the wrong method to try to override. I'l eventually figure out how to perform a hostile override, but for now I went with these adjustments to the actual source. First I hack Exception.pm so errors are in this format: path\file(line): sub stringify { my $self = shift; my $file = $self->file; my $line = $self->line; my $message = $self->text || 'Died'; my $object = $self->object; my $winFile = $file; $winFile =~ s/\//\\/g; # "it's under a Big Dubb'ya"! my $str = "$winFile($line)"; $str .= ': ' . $object->to_string() if $object && $object->can('to_string'); $str .= "\n" . $message; return $str; } Next, I transmogrify TestRunner::print_failures: ... for my $f (@{$result->failures()}) { chomp(my $f_to_str = $f); $self->_print("\n") if $i++; $self->_print("$i) $f_to_str\n"); use Win32::API::OutputDebugString qw(OutputDebugString); OutputDebugString("$f_to_str\n"); $self->_print("\nAnnotations:\n", $f->object->annotations()) if $f->object->annotations(); } ... The result: I can use tests to drive development in an app written in C++, using an enourmous test suite written in Perl. All using OneButtonTesting, without manually navigating back to failure lines. -- Phlip http://www.greencheese.org/SkeletonCrew -- Have a :-) day -- |
From: Phlip <pl...@sy...> - 2002-11-08 20:23:35
|
> my $winFile = $file; > $winFile =~ s/\//\\/g; # "it's under a Big Dubb'ya"! Despite my most Perlesque comment, that line's not needed. VC++ accepts / path separators. -- Phlip |
From: Phlip <pl...@sy...> - 2002-11-08 20:33:32
|
> Since you're using the full namespace to the method, you don't have to switch > packages. The result compiles, but control flow does not step inside the new version. Maybe either ptkdb or my embedding rig are interfering. Thanks, but enough spelunking. Now to add tests! http://www.c2.com/cgi/wiki?SheChangeDesignInTheDatabase -- Phlip http://www.c2.com/cgi/wiki?RatAndTuyen |
From: Adam S. <ad...@sp...> - 2002-11-09 14:12:06
|
Phlip (pl...@sy...) wrote: > Perlies: > > I like unit tests. My employers like Win32, VC++, and Perl. So here I am > writing an "acceptance test" rig for our VC++ code, and it calls > Perl embedded in the C++ code. > > When an assertion fails, such as this one... > > $self -> assert ($result =~ /LW is god/); That assertion is potentially buggy. Please read the Test::Unit::Assert manpage carefully, and use assert_matches() instead. |