|
1 | 1 | # The `rustdoc-json` test suite
|
2 | 2 |
|
3 |
| -> **FIXME**: This section is a stub. It will be populated by [PR #2422](https://github.com/rust-lang/rustc-dev-guide/pull/2422/). |
| 3 | +This page is specifically about the test suite named `rustdoc-json`, which tests rustdoc's [json output]. |
| 4 | +For other test suites used for testing rustdoc, see [§Rustdoc test suites](../tests/compiletest.md#rustdoc-test-suites). |
| 5 | + |
| 6 | +Tests are run with compiletest, and have access to the usual set of [directives](../tests/directives.md). |
| 7 | +Frequenly used directives here are: |
| 8 | + |
| 9 | +- [`//@ aux-build`][aux-build] to have dependencies. |
| 10 | +- `//@ edition: 2021` (or some other edition). |
| 11 | +- `//@ compile-flags: --document-hidden-items` to enable [document private items]. |
| 12 | + |
| 13 | +Each crate's json output is checked by 2 programs: [jsondoclint](#jsondocck) and [jsondocck](#jsondocck). |
| 14 | + |
| 15 | +## jsondoclint |
| 16 | + |
| 17 | +[jsondoclint] checks that all [`Id`]s exist in the `index` (or `paths`). |
| 18 | +This makes sure there are no dangling [`Id`]s. |
| 19 | + |
| 20 | +<!-- TODO: It does some more things too? |
| 21 | +Also, talk about how it works |
| 22 | + --> |
| 23 | + |
| 24 | +## jsondocck |
| 25 | + |
| 26 | +[jsondocck] processes direcives given in comments, to assert that the values in the output are expected. |
| 27 | +It's alot like [htmldocck](./rustdoc-test-suite.md) in that way. |
| 28 | + |
| 29 | +It uses [JSONPath] as a query language, which takes a path, and returns a *list* of values that that path is said to match to. |
| 30 | + |
| 31 | +### Directives |
| 32 | + |
| 33 | +- `//@ has <path>`: Checks `<path>` exists, i.e. matches at least 1 value. |
| 34 | +- `//@ !has <path>`: Checks `<path>` doesn't exist, i.e. matches 0 values. |
| 35 | +- `//@ has <path> <value>`: Check `<path>` exists, and at least 1 of the matches is equal to the given `<value>` |
| 36 | +- `//@ !has <path> <value>`: Checks `<path>` exists, but none of the matches equal the given `<value>`. |
| 37 | +- `//@ is <path> <value>`: Check `<path>` matches exactly one value, and it's equal to the given `<value>`. |
| 38 | +- `//@ is <path> <value> <value>...`: Check that `<path>` matches to exactly every given `<value>`. |
| 39 | + Ordering doesn't matter here. |
| 40 | +- `//@ !is <path> <value>`: Check `<path>` matches exactly one value, and that value is not equal to the given `<value>`. |
| 41 | +- `//@ count <path> <number>`: Check that `<path>` matches to `<number>` of values. |
| 42 | +- `//@ set <name> = <path>`: Check that `<path>` matches exactly one value, and store that value to the variable called `<name>`. |
| 43 | + |
| 44 | +These are defined in [`directive.rs`]. |
| 45 | + |
| 46 | +### Values |
| 47 | + |
| 48 | +Values can be either JSON values, or variables. |
| 49 | + |
| 50 | +- JSON values are JSON literals, e.g. `true`, `"string"`, `{"key": "value"}`. |
| 51 | + These often need to be quoted using `'`, to be processed as 1 value. See [§Argument spliting](#argument-spliting) |
| 52 | +- Variables can be used to store the value in one path, and use it in later queries. |
| 53 | + They are set with the `//@ set <name> = <path>` directive, and accessed with `$<name>` |
| 54 | + |
| 55 | + ```rust |
| 56 | + //@ set foo = $some.path |
| 57 | + //@ is $.some.other.path $foo |
| 58 | + ``` |
| 59 | + |
| 60 | +### Argument spliting |
| 61 | + |
| 62 | +Arguments to directives are split using the [shlex] crate, which implements POSIX shell escaping. |
| 63 | +This is because both `<path>` and `<value>` arguments to [directives](#directives) frequently have both |
| 64 | +whitespace and quote marks. |
| 65 | + |
| 66 | +To use the `@ is` with a `<path>` of `$.index[?(@.docs == "foo")].some.field` and a value of `"bar"` [^why_quote], you'd write: |
| 67 | + |
| 68 | +```rust |
| 69 | +//@ is '$.is[?(@.docs == "foo")].some.field' '"bar"' |
| 70 | +``` |
| 71 | + |
| 72 | +[^why_quote]: The value needs to be `"bar"` *after* shlex splitting, because we |
| 73 | + it needs to be a JSON string value. |
| 74 | + |
| 75 | +[json output]: https://doc.rust-lang.org/nightly/rustdoc/unstable-features.html#json-output |
| 76 | +[jsondocck]: https://github.com/rust-lang/rust/tree/master/src/tools/jsondocck |
| 77 | +[jsondoclint]: https://github.com/rust-lang/rust/tree/master/src/tools/jsondoclint |
| 78 | +[aux-build]: ../tests/compiletest.md#building-auxiliary-crates |
| 79 | +[`Id`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc_json_types/struct.Id.html |
| 80 | +[document private items]: https://doc.rust-lang.org/nightly/rustdoc/command-line-arguments.html#--document-private-items-show-items-that-are-not-public |
| 81 | +[`directive.rs`]: https://github.com/rust-lang/rust/blob/master/src/tools/jsondocck/src/directive.rs |
| 82 | +[shlex]: https://docs.rs/shlex/1.3.0/shlex/index.html |
| 83 | +[JSONPath]: https://www.rfc-editor.org/rfc/rfc9535.html |
0 commit comments