Req #16393 [Com]: Better support for file-level code
| From: | dw-pear@digger.net | Date: | Wed, 16 Sep 2009 06:18:52 +0000 |
| Subject: | Req #16393 [Com]: Better support for file-level code | ||
| References: | 1 | Groups: | php.pear.bugs |
| Request: | Send a blank email to pear-bugs+get-1646@lists.php.net to get a copy of this message | ||
Edit report at http://pear.php.net/bugs/bug.php?id=16393&edit=1
ID: 16393
Comment by: dw-pear@digger.net
Reported By: dw-pear at digger dot net
Summary: Better support for file-level code
Status: Feedback
Type: Feature/Change Request
Package: Testing_DocTest
Operating System: n/a
Package Version: 0.4.1
PHP Version: 5.2.9
Roadmap Versions:
New Comment:
After a bit more consideration, it occurs to me that perhaps I
overestimated the ordering problems that could be caused by making the
test code optional. If the allowed states are managed carefully, it
might not be an issue. This second patch makes the test code optional
by allowing expects: and expects-file: to appear after anything other
than the other expects*: form or clean:. Since the only keyword that is
allowed after either expects*: form is clean:, everything should be
fine. But approach with caution.
Previous Comments:
------------------------------------------------------------------------
[2009-09-16 07:09:18] digger
Added #patch bug:16393;patch:test-code-optional;revision:1253081358;.
------------------------------------------------------------------------
[2009-09-16 06:42:59] digger
This patch is my attempt to implement this feature request. It turned
out much simpler than I'd expected, which probably means I've missed any
number of ghastly side-effects. It seems to do what I want it to do,
but I'm not entirely certain what effect it'll have on existing tests.
First of all, I noticed that a couple of my samples in the feature
request have errors in them. In the second one, my expects: line
doesn't match the 'echo' in the script. And I accidentally commented
out the actual test code (echo OS_TYPE;) in the third one. Oops.
The next thing I realized is that if setup: is implemented like clean:,
with the code in comments, then there is no longer any need for the
test: keyword I proposed. The setup: block would be in comments,
followed by the uncommented test code. Following the existing clean:
structure also gave me code to copy. With that change, my third sample
looks like this:
/**
* <code>
* // doctest: Both setup and test code
* // setup:
* // $_ENV['OSTYPE'] = 'linux';
* echo OS_TYPE;
* // expects:
* // linux
* </code>
*/
define('OS_TYPE', $_ENV['OSTYPE']);
Note that the only problem here is that if you try to comment out the
first line of your test code, it'll effectively move up into the setup:
block. But once I got into the code I realized that problem already
exists for any keyword that precedes the test code. So if that counts
as a bug, it's a pre-existing bug that I'll just ignore. :)
Testing/DocTest/TestCase.php:
Line 98:
Initialized 'setupCode' to null.
Testing/DocTest/Parser/Default.php
Line 98:
Add KW_DOCTEST_SETUP keyword.
Line 145:
Add STATE_SETUP state.
Line 189:
Add KW_DOCTEST_SETUP to keyword parse list.
Line 237:
Add a case for KW_DOCTEST_SETUP to call _handleSetupLine().
Line 580:
Add STATE_SETUP to the list of states that expects: can follow.
Note that this makes STATE_CODE optional, so that expects: can
follow setup code, test code, or both. Note also that this does
NOT allow the 'neither' case described in my feature request. I
chose not to implement that, since it would mean allowing expects:
to follow the flags:, doctest:, skip-if:, and (undocumented!)
ini-set: keywords. Doing that would allow all kinds of undesireable
ordering issues. And it's easy enough to put in a one-line dummy
test code that it's not worth the hassle of dealing with those
problems.
Line 610:
Add STATE_SETUP to the list of states that expects-file: can follow.
The same notes above apply here, to. (Is expects-file: documented?
I don't recall seeing it, but didn't really look.)
Line 637:
Add STATE_CLEAN to the list of states that test code cannot follow.
I think not having it in this list is a bug.
Line 720:
Add _handleSetupLine() function, based on _handleCleanLine().
Collects setup: code into setupCode variable and sets state
to STATE_SETUP. It disallows setup: after test code, expects:,
expects-file:, and clean:.
Line 773:
Add a case for STATE_SETUP to call _handleSetupLine().
Testing/DocTest/Runner/Default.php
Line 112:
Add %s to $codetpl for injection of setupCode.
Line 140:
Add $testCase->setupCode to sprintf() call to inject it into
$codetpl.
At this point, it is possible that either $testCase->setupCode or
$testCase->code will be null, unlike before where the expects:
keyword
required $testCase->code to be filled. However, I don't think it
matters, since null should get put into %s as an empty string.
And that's it. I really does look too easy. What did I miss?
------------------------------------------------------------------------
[2009-09-16 06:37:30] digger
Added #patch bug:16393;patch:add-setup-keyword;revision:1253079450;.
------------------------------------------------------------------------
[2009-08-26 15:19:44] izi
-Status: Open
+Status: Feedback
Hi dave,
Sorry for the delay.
This is gonna be a bit complicated to implement, I'm currently working
on other things and will not have time to implement it before long,
would you like to try to work on a patch ? I'll be happy to review it !
Thanks,
--
David
------------------------------------------------------------------------
[2009-07-01 03:36:01] digger
Description:
------------
Some of the files I would like to add doctests to contain bare code
(outside of functions and classes), and have behavior that depends on
external values (web server environment variables, command-line
arguments, etc). Since DocTest require()s the tested source before
running the test, the tested code has already executed by the time test
code gets a chance to set up the necessary data.
What would be nice to see is a keyword ("setup:" in my examples below)
that could be used to specify test setup code to run BEFORE the tested
file is require()d. This way the test code could prepare some necessary
data, then the tested file would be require()d, and finally the test
code would be run.
Similarly, scripts with bare code like this will often produce output
without any actual test code needed. But if the code section of the
test is omitted, DocTest complains "Error: unexpected expects line". So
the second part of this request is that the test code be optional. That
is, a test could contain the setup code (described above), or the usual
test code, or both, or neither.
The "both" case is a bit tricky, due to the need to somehow separate
the setup code and the test code. Perhaps there could be an optional
keyword ("test:" in my examples) which if present just indicates the
start of the test code, acting as a separator if necessary.
Test script:
---------------
/**
* <code>
* // doctest: Web page test
* // setup:
* // $_SERVER['REQUEST_URI'] = '/test/script';
* // expects:
* // You requested '/test/script'.
* </code>
*/
echo "You requested '{$_SERVER['REQUEST_URI']}'.";
//===================
/**
* <code>
* // doctest: Command line test
* // setup:
* // $argc = 1;
* // $argv = array('test');
* // expects:
* // Your command was 'test'.
* </code>
*/
if ($argc > 0) {
echo "Your command was: '{$argv[0]}'.";
}
//===================
/**
* <code>
* // doctest: Both setup and test code
* // setup:
* // $_ENV['OSTYPE'] = 'linux';
* // test:
* // echo OS_TYPE;
* // expects:
* // linux
* </code>
*/
define('OS_TYPE', $_ENV['OSTYPE']);
------------------------------------------------------------------------
--
Edit this bug report at http://pear.php.net/bugs/bug.php?id=16393&edit=1