Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

RFC 52 implementation #87

Merged
merged 16 commits into from
Apr 6, 2017
Merged

RFC 52 implementation #87

merged 16 commits into from
Apr 6, 2017

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Feb 28, 2017

It is like `when` but it expects the result of the callable to be
`void`.
Hywan added 12 commits February 28, 2017 21:21
The `assert` asserter helps to test a piece of code using the `assert`
intrinsic.
The `hoa test:generate` command was obselete. It is rewritten to:

  1. Generate unit test suites based on contracts written in Praspel,
  2. Generate integration test suites based on examples in the API
     documentations.

The Praspel supports is not implemented yet. The API documentation
supports is implemented.

`hoa test:generate …` to generate the test suites, and then `hoa
test:run` to run them. This is the current workflow.
Deduce generator directory and generator namespace from the
`composer.json` file if it exists. If no `composer.json` file is found,
do not generate doc tests.
A code block has a type t and a content c:

    ```t
    c
    ```

The type can be optional. In our contexte, a missing type is equivalent
to the `php` type.

The generator only handles the `php` type, in order to compile them into
test cases.
Each code block must has the type `php`. However, the type is a list of
verbs, separated by a comma. So `php,ignore` is valid, and contains two
verbs: `php`, and `ignore`.

If the verb `ignore` is present, then the code block is compiled into a
test case but it is marked as `skip`.
Just type `ignore`, `must_throw` is a new verb that can be used in a
code block type. Thus `php,must_throw` represents an code block that is
a PHP program but it must throw an exception.

So far, exceptions must be of kind `Exception`. It restricts exceptions
to the runtime exception category.
New verb `must_throw` for the code block type has been introduced
recently. This patch enhances it by adding a parameter to this verb:
`must_throw(E)` where `E` is a class name, expecting to be an exception.

Thus, the following example will fail:

        /**
         * # Examples
         *
         * ```php,must_throw(ErrorException)
         * throw new \RuntimeException();
         * ```
         */
Code block can have comments. If the comment starts with `#` (shell
style), then the whole comment is removed for the HTML API browser, but
they are kept when compiling examples into test cases. So the following
example:

     # $a = 1;
     $b = 2;
     assert(3 === $a + $b);

will be compiled as the following test case:

     $a = 1;
     $b = 2;
     assert(3 === $a + $b);

and will be displayed as follows for the HTML version:

     $b = 2;
     assert(3 === $a + $b);

This is useful when we would like to add `use` statements in
comments. In our context, we cannot use `use` because test cases are
methods, and the syntax of PHP does not allow `use` statements inside
methods. So we must expanded `use` statements to remove them.

To address that, the new `unfoldCode` method lexes and expands `use`
statements. For instance:

    # use Foo\Bar;
    new Bar\Baz();

will be unfolded as:

    # use Foo\Bar;
    new \Foo\Bar\Baz();

To achieve this, first, the comments are removed, and, second, `<?php`
is prepended:

    <?php
    use Foo\Bar;
    new Bar\Baz();

Then, third, we use the `token_get_all` native lexer to lex the code
block, and rewrite it. Finally, when rewriting, the `use` statements are
put in comments (of kinds `#`), even if they were not in comments
before. The `<?php` opening tag is removed too. The result is:

    # use Foo\Bar;
    new \Foo\Bar\Baz();
@Hywan Hywan changed the title WIP for the RFC 52 RFC 52 implementation Mar 24, 2017
@Hywan Hywan requested a review from vonglasow March 24, 2017 13:02
Bin/Generate.php Outdated
$generator = new \Atoum\PraspelExtension\Praspel\Generator();
$generator->setTestNamespacer(function ($namespace) {
$parts = explode('\\', $namespace);
var_dump($generateDocumentation, $directoryToScan, $namespaceToScan);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove the var_dump


return array_reduce(
token_get_all($code),
function ($accumulator, $token) use (&$buffer, &$state, &$aliases) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe separate the function definition to this usage to improve code clarity ?

$reducing = function (...

return array_reduce(token_get_all($code), $reducing...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we will gain a lot, bu why not 🙂.

@Bhoat Bhoat merged commit 513c395 into hoaproject:master Apr 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

3 participants