Skip to content
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

Use AsciiDoc snippets to include code snippets in documentation #334

Merged
merged 25 commits into from
Feb 19, 2022
Merged

Use AsciiDoc snippets to include code snippets in documentation #334

merged 25 commits into from
Feb 19, 2022

Conversation

Michael1993
Copy link
Member

@Michael1993 Michael1993 commented Sep 8, 2020

closes #335

Signed-off-by: Mihaly Verhas misi.verhas@gmail.com

I hereby agree to the terms of the JUnit Pioneer Contributor License Agreement.

Signed-off-by: Mihaly Verhas <misi.verhas@gmail.com>
@sonarcloud
Copy link

sonarcloud bot commented Sep 8, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Bukama
Copy link
Member

Bukama commented Sep 9, 2020

@Michael1993 Michael1993 marked this pull request as draft September 9, 2020 13:24
@nipafx
Copy link
Member

nipafx commented Sep 29, 2020

I really like this approach to showing code snippets because we can always be sure that they actually work. @Bukama's concerns are valid, though. Tests and code snippets have very different concerns and we should hence separate them.

I propose to create a new source set demo (as src/demo/java) and put all the demos there. They must be written as proper tests, so we can execute them during the build, but they can be written and styled according to the documentation's needs.

If possible, we could even use Spotless to limit line length to 60 chars (if we can have different configurations for different folders - I think we already had that request once).

@nipafx
Copy link
Member

nipafx commented Sep 29, 2020

Also, @Michael1993 can you please open an issue, so we can discuss there?

@Michael1993
Copy link
Member Author

I opened #335 if that's OK?

Base automatically changed from master to main March 2, 2021 20:12
@Bukama
Copy link
Member

Bukama commented Dec 7, 2021

Any motivation to further work on this @Michael1993?

@Michael1993
Copy link
Member Author

Any motivation to further work on this @Michael1993?

Whoops, I forgot about this. I'll do some work on it in December.

Copy link
Member

@nipafx nipafx left a comment

Choose a reason for hiding this comment

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

Looks good. The extra ceremony of creating, tagging, and referencing the source file is non-trivial, though. I think the benefits are worth it, but we could discuss whether short snippets (like, three lines or so) could be allowed in the Asciidoc or whether this is the new standard for all tests, regardless of complexity.

demo/build.gradle.kts Outdated Show resolved Hide resolved
build.gradle.kts Outdated Show resolved Hide resolved
Co-authored-by: Simon Schrottner <simon.schrottner@gmail.com>
@Michael1993 Michael1993 marked this pull request as ready for review February 9, 2022 09:42
@Michael1993
Copy link
Member Author

The branch is ready for review - however if it gets merged, there should be another PR that migrates the rest of the docs to the new 'system'.

@Michael1993 Michael1993 added the full-build Triggers full build suite on PR label Feb 9, 2022
build.gradle.kts Outdated Show resolved Hide resolved
@nipafx
Copy link
Member

nipafx commented Feb 17, 2022

however if it gets merged, there should be another PR that migrates the rest of the docs to the new 'system'.

Can you create an issue for that?

@Michael1993
Copy link
Member Author

however if it gets merged, there should be another PR that migrates the rest of the docs to the new 'system'.

Can you create an issue for that?

#595

docs/report-entries.adoc Outdated Show resolved Hide resolved
@nipafx
Copy link
Member

nipafx commented Feb 18, 2022

Can you extend CONTRIBUTING.md with this text (at the end of the section "Feature Documentation"):

Code blocks in these files should not be pure text.
Instead, in the `src/demo/java` source tree, create/update a `...Demo` class that is dedicated to this feature and place code snippets in `@Test`-annotated methods in `...Demo`.
Write each snippet as needed for the documentation and bracket it with tags:

Code blocks in these files should not be pure text.
Instead, in the `src/demo/java` source tree, create/update a `...Demo` class that is dedicated to this feature and place code snippets in `@Test`-annotated methods.
Write each snippet as needed for the documentation and bracket it with tags:

```java
// tagging the entire test method:

// tag::$TAG_NAME[]
@Test
@SomePioneerExtension
void simple() {
	// demonstrate extension
}
// end::$TAG_NAME[]


// tagging a few lines from the test:

@Test
void simple() {
	// tag::$TAG_NAME[]
	SomePioneerExtension ex = // ...
	// demonstrate extension
	// end::$TAG_NAME[]
	assertThat(ex). // ...
}
```

Where feasible, include or follow up with assertions that ensure correct behavior.
Thus `...Demo` classes guarantee that snippets compile and (roughly) behave as explained.

In the documentation file, include an attribute pointing at the demo source file:

```adoc
:snippetsource: ../src/demo/java/org/junitpioneer/jupiter/...Demo.java
```

To include these snippets, use a block like the following:

```adoc
[source,java,indent=0]
----
include::{snippetsource}[tag=$TAG_NAME]
----
```

Proposed commit message:

Configure build and docs to compile and test code snippets (#335 / #334)

Code snippets in the documentation files in `docs` are just text and
thus run the risk of getting outdated when APIs evolve or
implementations change their behavior. To address that, we now use
Asciidoc's capability to include tagged regions from external
files[1] that are compiled and tested by the build. These files'
primary purpose is to demonstrate features (not to test them) and so
they are placed in a new source set `src/demojava`.

[1]: https://docs.asciidoctor.org/asciidoc/latest/directives/include-tagged-regions/

Closes: #335
PR: #334 

@nipafx nipafx merged commit de82d95 into junit-pioneer:main Feb 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full-build Triggers full build suite on PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AsciiDoc code snippets in documentation
4 participants