-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add basic support for doctests #9315
Conversation
CT Test Results 4 files 226 suites 1h 54m 43s ⏱️ Results for commit 1e6b11f. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting. looks useful, i wonder how/if it could be extended for code samples requiring some env preparation setup ...
In the zstd_SUITE tests I needed this so there is support for passing an The example tested using this is |
@garazdawi this is very exciting! I understand this is initial approach but here are some questions to consider:
Elixir considers the first two to have two separate doctests. FWIW, the reason why we detect empty lines is precisely because we encourage the first style, where you don't have to wrap your code in |
|
I pushed a bunch of examples showing what can be done. Copied below to be easier to find:
Extra features that I'm thinking of adding are:
|
IMHO multiple tests should be multiple code blocks, like: > List = [1, 2, 3],
> lists:sum(List).
6 > List = [1, 2, 3, 4],
> lists:sum(List).
10 Or > List1 = [1, 2, 3],
> lists:sum(List1).
6
> List2 = [1, 2, 3, 4],
> lists:sum(List2).
10 And not > List = [1, 2, 3],
> lists:sum(List).
6
> List = [1, 2, 3, 4],
> lists:sum(List).
10 |
I think José was suggesting: > List = [1, 2, 3],
> lists:sum(List).
6
> List = [1, 2, 3, 4],
> lists:sum(List).
10 i.e. that there needs to be a double-newline to trigger a new test environment. I agree with you though that it probably is not needed. |
@garazdawi Would it make sense to expose some sort of API that would return the tests as a data structure? Some sort of The motivation is that the errors you'd get from using cc @robertoaloi |
Hrm, relying on
But using pattern matching is probably impossible without showing the representation everywhere in the docs? Although another way would be to write all of them as:
|
@michalmuskala we can do that, but later on. I will keep things simple for now. @josevalim alternative 2 is how I did it in the zstd module for opaques (#9316). |
ca3b1ab
to
160684e
Compare
In Rust, there's special syntax to allow setup code or hiding portions of the example. See https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example |
b062ab6
to
1c14565
Compare
1e6b11f
to
edc69ed
Compare
Merged! Looking forward to everyone helping out writing doctests for all our modules now! 😄 |
This PR adds basic support for running doctests on documentation in Erlang/OTP. It is not yet made available for public use as the API may change, but we it can be used within Erlang/OTP to test documentation examples.
The syntax for writing doctests is like this:
When something like the above is found in the docs, the expression will be run and tested against the return value. It is possible to write more complex things, but that it is the basic idea.