-
Notifications
You must be signed in to change notification settings - Fork 8
Compile API documentation examples in executable tests #52
Comments
👍 |
@1e1 How can it reduce the global code size? What can flood the code, I don't get it? Also, the namespace for tests are (for a library called |
Seems really interesting, also adding examples in the source code documentation help developers to understand better the code behaviour... @1e1 since it's "only" for the documentation tests you are not adding the tests in the class, only ensure examples quality 😄. @Hywan I just saw one issue around documentation test obsolescence. Do you think these tests must be added to the Git ? Will they update automatically using CI or is the developer which will need to update documentation tests ? Since it's generated tests, I think we don't need to store them somewhere because we always be able to regenerate them... |
@shulard Good note. We should add |
@shulard Thx |
@1e1 You have the API documentation which contains an |
Full example: <?php
namespace Hoa\Xyz;
class Foo
{
/**
* This is an API documentation for the `f` method.
*
* # Examples
*
* This example creates 2 variables, namely `$x` and `$y`, and sums them
* with the help of the `Foo::f` method.
*
* ```php
* # use Hoa\Xyz\Foo;
* $x = 1;
* $y = 2;
* $foo = new Foo();
*
* assert(3 === $foo->f($x, $y));
* ```
*/
public function f(int $x, int $y): int
{
// …
}
} Resulting test suite for the class namespace Hoa\Xyz\Test;
use Hoa\Test;
class Foo extends Test\Documentation\Suite
{
public function case_f_example_1()
{
$this
->do(function () {
$x = 1;
$y = 2;
$foo = new \Hoa\Xyz\Foo();
assert(3 === $foo->f($x, $y), new AssertionError());
});
}
} Also, the result in the API documentation browser:
Isn't clearer? |
I agree to the literal lines. Why not extracting examples from the test suite? |
This idea of "documentation as tests" looks a lot to what I had in mind when I started working on Rusty If you decide to really implement this RFC, it might be worth considering using Rusty (I'd be willing to help, of course :) ). |
@K-Phoen Yup, I know this project. What is a blocker for me:
Do you want to address these points? |
Integrating rusty with atoum/phpunit is something that I also wanted. Splitting the project in two and provide both a CLI application and a library could be done too. |
I am also pretty sure we can do something much more simpler. I will try soon, and compare my POC with Rusty. |
@Hywan eg the API generator cannot guess the sentences ;) I don't understand. If there is some tests (in Moreover if the doc comment contains a Praspel instruction, this one will appear in the final documentation. I miss something. I guess some issues have the same goal but the steps are already defined (like: #53 ). |
@1e1 The goal of this RFC is to compile examples into tests. That's all. Unit tests are not examples, they form an executable informal specification. An example illustrates a particular usage of a method, or a datum, relevant to understand its global usage or an edge case. So the direction is Examples to Tests, not the opposite. If the API documentation contains a contract written in Praspel, this is not related to this RFC at all. We are talking about the Examples Section, not the Praspel/Contracts Section. RFC #53 has nothing to do with this RFC neither. The common basis between #52, #53, and #58 is the new API documentation format. This new format allows many features, like the ones described in all the RFC. Is it clear :-)? |
Hello, So this Gist https://gist.github.com/Hywan/b8bd387def5e3cc13e024c4f924e8c3c makes everything work. It just does not save the result in specific files, but here is what it does so far:
Result: <?php
namespace Hoa\Acl\Test\Integration;
use Hoa\Test;
class A extends Test\Integration\Suite
{
public function case_sum_example_0()
{
$this
->do(function () {
$x = 1;
$y = 3;
assert(3 === $x + $y);
});
}
public function case_sum_example_1()
{
$this
->do(function () {
$x = 1;
$y = 2;
$a = new \Hoa\Acl\A();
assert(3 === $a->sum($x, $y));
});
}
} From this: <?php
namespace Hoa\Acl;
class A
{
/**
* The `sum` method will compute the sum of two integers, `$x` and `$y`.
*
* # A section
*
* Bla bla bla
*
* # Examples
*
* This first example shows a regular sum with the `+` operator. Looser.
*
* ```
* $x = 1;
* $y = 2;
*
* assert(3 === $x + $y);
* ```
*
* This example shows how a real programmer will use the `sum` method.
*
* ```php
* $x = 1;
* $y = 2;
* $a = new A();
*
* assert(3 === $a->sum($x, $y));
* ```
*
* # Exceptions
*
* Nothing special. Baboum.
*/
public function sum(int $x, int $y): int
{
return $x + $y;
}
public function noDoc()
{
}
/**
* This method has no example.
*
* # A section
*
* Bla bla bla
*/
public function noExample()
{
}
} This patch hoaproject/Test#87 introduces the What it is not supported yet:
My opinion: A simple class can do the trick (< 200 LOC). This is a very simple compilation/transformation step, no need to have multiple classes & co. The only dependency we add to |
I got some free time tonight, so I made some progresses on hoaproject/Test#87: The implementation for this RFC.
Considering the same code sample above, the produced test suite is the following: <?php
namespace Hoa\Acl\Test\Documentation;
use Hoa\Test;
class A extends Test\Integration\Suite
{
public function case_sum_example_0()
{
$this
->assert(function () {
$x = 1;
$y = 2;
assert(3 === $x + $y);
});
}
public function case_sum_example_1()
{
$this
->assert(function () {
$x = 1;
$y = 2;
$a = new \Hoa\Acl\A();
assert(3 === $a->sum($x, $y));
});
}
} It is saved in the Important things to notice:
Output if everything is working great:
Output when at least one test case fails, here we replaced - assert(3 === $a->sum($x, $y));
+ assert(4 === $a->sum($x, $y));
Note the:
This is the information we need. No diff, no exception stack trace, just the failing assert. |
Next steps:
|
Progression:
Next steps:
# use Hoa\Foo\Bar;
$a = new Bar(); I don't know if it is hard. I don't want to use a lexer, nor a parser, for PHP. I would like to keep it simple, let's see. |
The ability to define an exception type is a must have, love the syntax ! |
Done, hoaproject/Test@f48c6c2. |
|
Done. This RFC is implemented! |
Congrats everyone ❤️! |
Hello fellow @hoaproject/hoackers and users!
Introduction
This RFC aims at including the documentation of Hoa's libraries into the test quality process by having executable and testable examples. This is a sequel/extend to the RFC #58 which introduces a new API format. In particular, each method will have an API documentation written in markdown, with special sections, like Examples. In this section, we can have code blocks representing PHP code. The goal of this RFC is to define a process that: (i) extract the code from these API documentation, (ii) transform them into a unit or integration test, (iii) execute them.
The same process can be extended to RFC #51, where the Invalid examples and Valid examples Sections define the same kind of code blocks, with the same goal. The difference is that an invalid example is expected to fail, but the process is the same.
Extracting the code blocks
Based on the tools defined in the RFC #53, we will be able to:
/** … */
), if the “Examples” Section is present, iterate over code blocks.Now, a code block will has the following form:
where c (body of the code block) is PHP code, and pl (programming language type of the code block, also called code block type) is
php
or none. If none,php
will be the default.Compiling to tests
The body of the code block will not use any test framework API, so no atoum or
Hoa\Test
API in our case. Why? Because this is documentation. Documentation has nothing to do with an API from our test framework. However, the documentation can containassert
intrinsics (also called Expectations). Example:This example illustrates the usage of
Sum
, it shows also its result. Theassert
is here to illustrate the form of the results thatSum
can produce. This way to write an example for the documentation provides all these benefits. I think this is adequate for this kind of documentation.Since PHP 7.0,
assert
can take a second argument, called$exception
, which is an instance of an exception of kindAssertionError
. This exception will be thrown if the assertion fails. We can change the code to automatically add an exception as a second argument.If we take our previous example, the code block will be compiled into:
Because the
when
pseudo-asserter has no specific meaning, we can create a new pseudo-asserter inHoa\Test
like:do
, as an alias towhen
. Thus:If the test is failing, atoum will catch that an unexpected exception has been thrown, and the test will fail.
This test case will belong to a specific test suite. The structure will be like this:
The
Test/Documentation/Foo.php
file will contain a test suite defined as:where
methodName
is the name of the method whom API documentation is being tested.Because the API documentation examples can be either a unit test or an integration test, a special
Documentation
namespace is created. It targets all the documentation tests.Catching expected fail tests
This is possible that an example shows that an exception must be thrown. In this case, the test will fail while the example is valid. To avoid that, the following code block type must be used:
In this situation, the test case will compile to:
Ignoring examples
To ignore an example, use the following code block type:
Run tests
So far, tests are executed with the
hoa test:run
command. We also havehoa test:generate
andhoa test:clean
. These latters target Praspel, but we can re-use them to generate the documentation. We could select what kind of tests we would like to generate with an option, likehoa test:generate --documentation
, orhoa test:generate --praspel
for instance.By default,
hoa test:run
will runhoa test:generate --documentation
if noDocumentation
directory exists. The goal is to not modify the.travis.yml
file to include these new tests.In the contributor guide, we must stipulate that
hoa test:generate --documentation
must be run beforehoa test:run
when iterating on the code (edit, test, edit, test, edit, test…). Maybe we could introduce a cache invalidation system to re-generate only the specific documentation test suite. This should not be too much complicated.Visually, there will be no difference between unit, integration or documentation test execution in the CLI report. Only the test suite namespaces will provide this information, like
Hoa\Xyz\Test\Unit\Foo\Bar
orHoa\Xuz\Test\Documentation\Foo\Bar
. Room for improvements in the CLI report to add a separator between “test namespaces” (betweenUnit
,Integration
etc.)?Conclusion
I think with this approach we will be able to automatically test the API documentation examples. This will be a good win. With RFC #53 in mind, we will ensure that all examples will always be valid. This will improve the whole quality of the project. The test workflow will not be disturbed since
hoa test:run
will still control everything. The contributor workflow might change a little bit, but the impact is minimal compared to the guarantees it provides.Thoughts?
The text was updated successfully, but these errors were encountered: