Skip to content

Commit a00eb7e

Browse files
committed
Add @is command to jsondocck
1 parent 9b471a3 commit a00eb7e

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/test/rustdoc-json/nested.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
// edition:2018
22

3-
// @has nested.json "$.index[*][?(@.name=='nested')].kind" \"module\"
4-
// @has - "$.index[*][?(@.name=='nested')].inner.is_crate" true
3+
// @is nested.json "$.index[*][?(@.name=='nested')].kind" \"module\"
4+
// @is - "$.index[*][?(@.name=='nested')].inner.is_crate" true
55
// @count - "$.index[*][?(@.name=='nested')].inner.items[*]" 1
66

7-
// @has nested.json "$.index[*][?(@.name=='l1')].kind" \"module\"
8-
// @has - "$.index[*][?(@.name=='l1')].inner.is_crate" false
7+
// @is nested.json "$.index[*][?(@.name=='l1')].kind" \"module\"
8+
// @is - "$.index[*][?(@.name=='l1')].inner.is_crate" false
99
// @count - "$.index[*][?(@.name=='l1')].inner.items[*]" 2
1010
pub mod l1 {
1111

12-
// @has nested.json "$.index[*][?(@.name=='l3')].kind" \"module\"
13-
// @has - "$.index[*][?(@.name=='l3')].inner.is_crate" false
12+
// @is nested.json "$.index[*][?(@.name=='l3')].kind" \"module\"
13+
// @is - "$.index[*][?(@.name=='l3')].inner.is_crate" false
1414
// @count - "$.index[*][?(@.name=='l3')].inner.items[*]" 1
1515
pub mod l3 {
1616

17-
// @has nested.json "$.index[*][?(@.name=='L4')].kind" \"struct\"
18-
// @has - "$.index[*][?(@.name=='L4')].inner.struct_type" \"unit\"
17+
// @is nested.json "$.index[*][?(@.name=='L4')].kind" \"struct\"
18+
// @is - "$.index[*][?(@.name=='L4')].inner.struct_type" \"unit\"
1919
pub struct L4;
2020
}
21-
// @has nested.json "$.index[*][?(@.inner.span=='l3::L4')].kind" \"import\"
22-
// @has - "$.index[*][?(@.inner.span=='l3::L4')].inner.glob" false
21+
// @is nested.json "$.index[*][?(@.inner.span=='l3::L4')].kind" \"import\"
22+
// @is - "$.index[*][?(@.inner.span=='l3::L4')].inner.glob" false
2323
pub use l3::L4;
2424
}

src/tools/jsondocck/src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ pub struct Command {
4848
pub enum CommandKind {
4949
Has,
5050
Count,
51+
Is,
5152
}
5253

5354
impl CommandKind {
5455
fn validate(&self, args: &[String], command_num: usize, lineno: usize) -> bool {
5556
let count = match self {
5657
CommandKind::Has => (1..=3).contains(&args.len()),
57-
CommandKind::Count => 3 == args.len(),
58+
CommandKind::Count | CommandKind::Is => 3 == args.len(),
5859
};
5960

6061
if !count {
@@ -83,6 +84,7 @@ impl fmt::Display for CommandKind {
8384
let text = match self {
8485
CommandKind::Has => "has",
8586
CommandKind::Count => "count",
87+
CommandKind::Is => "is",
8688
};
8789
write!(f, "{}", text)
8890
}
@@ -127,6 +129,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
127129
let cmd = match cmd {
128130
"has" => CommandKind::Has,
129131
"count" => CommandKind::Count,
132+
"is" => CommandKind::Is,
130133
_ => {
131134
print_err(&format!("Unrecognized command name `@{}`", cmd), lineno);
132135
errors = true;
@@ -180,6 +183,7 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
180183
/// Performs the actual work of ensuring a command passes. Generally assumes the command
181184
/// is syntactically valid.
182185
fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
186+
// FIXME: Be more granular about why, (eg syntax error, count not equal)
183187
let result = match command.kind {
184188
CommandKind::Has => {
185189
match command.args.len() {
@@ -220,6 +224,18 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
220224
Err(_) => false,
221225
}
222226
}
227+
CommandKind::Is => {
228+
// @has <path> <jsonpath> <value> = check *exactly one* item matched by path, and it equals value
229+
assert_eq!(command.args.len(), 3);
230+
let val = cache.get_value(&command.args[0])?;
231+
match select(&val, &command.args[1]) {
232+
Ok(results) => {
233+
let pat: Value = serde_json::from_str(&command.args[2]).unwrap();
234+
results.len() == 1 && *results[0] == pat
235+
}
236+
Err(_) => false,
237+
}
238+
}
223239
};
224240

225241
if result == command.negated {

0 commit comments

Comments
 (0)