|
3 | 3 | This page is specifically about the test suite named `rustdoc-json`, which tests rustdoc's [json output].
|
4 | 4 | For other test suites used for testing rustdoc, see [Rustdoc tests](../rustdoc.md#tests).
|
5 | 5 |
|
6 |
| -Tests are run with compiletest, and have access to the usuall set of [directives](../tests/directives.md). |
| 6 | +Tests are run with compiletest, and have access to the usual set of [directives](../tests/directives.md). |
7 | 7 | Frequenly used directives here are:
|
8 | 8 |
|
9 | 9 | - [`//@ aux-build`][aux-build] to have dependencies.
|
10 | 10 | - `//@ edition: 2021` (or some other edition).
|
11 | 11 | - `//@ compile-flags: --document-hidden-items` to enable [document private items].
|
12 | 12 |
|
13 |
| -Each crate's json output is checked by 2 programs: [jsondoclint] and [jsondocck]. |
| 13 | +Each crate's json output is checked by 2 programs: [jsondoclint](#jsondocck) and [jsondocck](#jsondocck). |
14 | 14 |
|
15 | 15 | ## jsondoclint
|
16 | 16 |
|
17 | 17 | [jsondoclint] checks that all [`Id`]s exist in the `index` (or `paths`).
|
18 |
| -This makes sure their are no dangling [`Id`]s. |
| 18 | +This makes sure there are no dangling [`Id`]s. |
19 | 19 |
|
20 | 20 | <!-- TODO: It does some more things too?
|
21 | 21 | Also, talk about how it works
|
22 | 22 | -->
|
23 | 23 |
|
24 | 24 | ## jsondocck
|
25 | 25 |
|
26 |
| -<!-- TODO: shlex, jsonpath, values, variables --> |
| 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. |
27 | 30 |
|
28 | 31 | ### Directives
|
29 | 32 |
|
30 | 33 | - `//@ has <path>`:: Checks `<path>` exists, i.e. matches at least 1 value.
|
31 | 34 | - `//@ !has <path>`:: Checks `<path>` doesn't exist, i.e. matches 0 values.
|
32 |
| -- `//@ has <path> <value>`: Check `<path>` exists, and 1 of the matches is equal to the given `<value>` |
| 35 | +- `//@ has <path> <value>`: Check `<path>` exists, and at least 1 of the matches is equal to the given `<value>` |
33 | 36 | - `//@ !has <path> <value>`: Checks `<path>` exists, but none of the matches equal the given `<value>`.
|
34 |
| -- `//@ is <path> <value>`: Check `<path>` matches exacly one value, and it's equal to the given `<value>`. |
| 37 | +- `//@ is <path> <value>`: Check `<path>` matches exactly one value, and it's equal to the given `<value>`. |
35 | 38 | - `//@ is <path> <value> <value>...`: Check that `<path>` matches to exactly every given `<value>`.
|
36 | 39 | Ordering doesn't matter here.
|
37 | 40 | - `//@ !is <path> <value>`: Check `<path>` matches exactly one value, and that value is not equal to the given `<value>`.
|
38 | 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: |
39 | 67 |
|
| 68 | +```rust |
| 69 | +//@ is '$.is[?(@.docs == "foo")].some.field' '"bar"' |
| 70 | +``` |
40 | 71 |
|
| 72 | +[^why quote]: The value needs to be `"bar"` *after* shlex splitting, because we |
| 73 | + it needs to be a JSON string value. |
41 | 74 |
|
42 | 75 | [json output]: https://doc.rust-lang.org/nightly/rustdoc/unstable-features.html#json-output
|
43 | 76 | [jsondocck]: https://github.com/rust-lang/rust/tree/master/src/tools/jsondocck
|
44 | 77 | [jsondoclint]: https://github.com/rust-lang/rust/tree/master/src/tools/jsondoclint
|
45 | 78 | [aux-build]: ../tests/compiletest.md#building-auxiliary-crates
|
46 | 79 | [`Id`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc_json_types/struct.Id.html
|
47 | 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