Skip to content

Add documentation for isOverrideFunction and getUnitTests traits #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2013

Conversation

jacob-carlborg
Copy link
Contributor

I guess this shouldn't be pulled at least until the pull request for isOverrideFunction is merged. getUnitTests is already implemented.

@ghost
Copy link

ghost commented Jul 23, 2013

W.r.t. getUnitTests: Could you please add the following code snippet, it will show people how to avoid running the tests automatically:

import core.runtime;

shared static this()
{
    Runtime.moduleUnitTester = { return true; };
}

unittest
{
    assert(0);
}

This will stop the unittests from running, but continue execution into main (one can return false to avoid calling main as well). Also make a note that Runtime.moduleUnitTester must be set in a module constructor, and not in the main function (because the unittests are executed before main).

@ghost
Copy link

ghost commented Jul 23, 2013

Btw, is it possible to make __traits(getUnitTests) work with a ModuleInfo? It would allow this code:

void main()
{
    foreach (m; ModuleInfo)
    {
        foreach (test; __traits(getUnitTests, m))
        {
            // run each test for every module
        }
    }
}

Otherwise we are forced to have a single module which imports all other modules in order to be able to use __traits(getUnitTests).

@ghost
Copy link

ghost commented Jul 23, 2013

One other thing I have w.r.t. this feature, __traits(getUnitTests) doesn't seem to work recursively when you call it on a module, it only picks up the unittests in a module but not the unittests in the aggregates in the module. So currently one has to iterate through all aggregates somehow (and recursively) to find all the unittests in a module, but I would prefer if the compiler did this automatically when passed a module, because this is likely difficult to implement in library code.

@jacob-carlborg
Copy link
Contributor Author

@AndrejMitrovic W.r.t. getUnitTests: Could you please add the following code snippet, it will show people how to avoid running the tests automatically

Yes, I can do that.

@jacob-carlborg
Copy link
Contributor Author

Btw, is it possible to make __traits(getUnitTests) work with a ModuleInfo? It would allow this code

No, I don't think so. At least not easily. __traits is designed to work at compile time. In the example you're iterating runtime data. One also need to use getUnitTests at compile time to be able to access UDA's.

The unit tests are already accessible from the module info. What would this gain? Accessing the individual unit test functions and not only the wrapper? Or accessing UDA's?

Otherwise we are forced to have a single module which imports all other modules in order to be able to use __traits(getUnitTests).

Yeah, I know, that's a problem. I have another pull request that is intended to solve this problem. Doing the same as RTInfo but for modules instead of types. But that was not entirely liked. dlang/dmd#2271

@jacob-carlborg
Copy link
Contributor Author

One other thing I have w.r.t. this feature, __traits(getUnitTests) doesn't seem to work recursively when you call it on a module, it only picks up the unittests in a module but not the unittests in the aggregates in the module.

Yes, that's how the rest of these traits work.

So currently one has to iterate through all aggregates somehow (and recursively) to find all the unittests in a module, but I would prefer if the compiler did this automatically when passed a module, because this is likely difficult to implement in library code.

I thought that was fairly easy solvable using __traits(allMembers).

@ghost
Copy link

ghost commented Jul 23, 2013

I thought that was fairly easy solvable using __traits(allMembers).

Well if it's implementable in a library I suggest we add something like this to Phobos, e.g. a getAllUnittests template which takes an alias to a module and extracts all unittests in that module.

The unit tests are already accessible from the module info. What would this gain? Accessing the individual unit test functions and not only the wrapper? Or accessing UDA's?

Both! But yeah __traits is the wrong way around this, the ModuleInfo itself would likely need to have an array of unittests instead.

@jacob-carlborg
Copy link
Contributor Author

Added example of overriding the default unit test runner.

{
// Override the default unit test runner to do nothing. After that, "main" will
// be called.
Runtime.moduleUnitTester = { return true; };
Copy link
Contributor

Choose a reason for hiding this comment

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

To do this, import core.runtime; is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. I'll fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@jacob-carlborg
Copy link
Contributor Author

Semicolon, done.

@9rnsr
Copy link
Contributor

9rnsr commented Nov 4, 2013

OK, LGTM.

9rnsr added a commit that referenced this pull request Nov 4, 2013
Add documentation for isOverrideFunction and getUnitTests traits
@9rnsr 9rnsr merged commit e82637e into dlang:master Nov 4, 2013
@jacob-carlborg jacob-carlborg deleted the traits branch November 4, 2013 08:20
@jacob-carlborg
Copy link
Contributor Author

Thanks 😃

9rnsr added a commit that referenced this pull request Nov 4, 2013
Add documentation for isOverrideFunction and getUnitTests traits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants