From 4b2a1eb775d7a9ff37b5b4dc990c287f427c2c80 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Sat, 26 Nov 2022 15:32:49 +0000 Subject: [PATCH 1/2] Support unit tests for jsondoclint --- src/bootstrap/builder.rs | 1 + src/bootstrap/test.rs | 36 ++++++++++++++++++++ src/tools/jsondoclint/src/json_find.rs | 3 ++ src/tools/jsondoclint/src/json_find/tests.rs | 4 +++ 4 files changed, 44 insertions(+) create mode 100644 src/tools/jsondoclint/src/json_find/tests.rs diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 251431a15eb..cff5fd8c5b0 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -644,6 +644,7 @@ impl<'a> Builder<'a> { test::CrateLibrustc, test::CrateRustdoc, test::CrateRustdocJsonTypes, + test::CrateJsonDocLint, test::Linkcheck, test::TierCheck, test::ReplacePlaceholderTest, diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index a69979d7f07..39cedfdac5f 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -90,6 +90,42 @@ fn try_run_quiet(builder: &Builder<'_>, cmd: &mut Command) -> bool { true } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct CrateJsonDocLint { + host: TargetSelection, +} + +impl Step for CrateJsonDocLint { + type Output = (); + const ONLY_HOSTS: bool = true; + const DEFAULT: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.path("src/tools/jsondoclint") + } + + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(CrateJsonDocLint { host: run.target }); + } + + fn run(self, builder: &Builder<'_>) { + let bootstrap_host = builder.config.build; + let compiler = builder.compiler(0, bootstrap_host); + + let cargo = tool::prepare_tool_cargo( + builder, + compiler, + Mode::ToolBootstrap, + bootstrap_host, + "test", + "src/tools/jsondoclint", + SourceType::InTree, + &[], + ); + try_run(builder, &mut cargo.into()); + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct Linkcheck { host: TargetSelection, diff --git a/src/tools/jsondoclint/src/json_find.rs b/src/tools/jsondoclint/src/json_find.rs index 95ea8866609..0fbffc0a005 100644 --- a/src/tools/jsondoclint/src/json_find.rs +++ b/src/tools/jsondoclint/src/json_find.rs @@ -72,3 +72,6 @@ fn find_selector_recursive( } } } + +#[cfg(test)] +mod tests; diff --git a/src/tools/jsondoclint/src/json_find/tests.rs b/src/tools/jsondoclint/src/json_find/tests.rs new file mode 100644 index 00000000000..b3e009729b1 --- /dev/null +++ b/src/tools/jsondoclint/src/json_find/tests.rs @@ -0,0 +1,4 @@ +#[test] +fn should_fail() { + assert_eq!(true, false); +} From 09818a8ccaec19d96ec4d364e668742cf7b10522 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Sat, 26 Nov 2022 16:24:43 +0000 Subject: [PATCH 2/2] Add a test that makes sense --- src/tools/jsondoclint/src/json_find.rs | 2 +- src/tools/jsondoclint/src/json_find/tests.rs | 27 ++++++++++++++++++-- src/tools/jsondoclint/src/main.rs | 4 +-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/tools/jsondoclint/src/json_find.rs b/src/tools/jsondoclint/src/json_find.rs index 0fbffc0a005..70e7440f730 100644 --- a/src/tools/jsondoclint/src/json_find.rs +++ b/src/tools/jsondoclint/src/json_find.rs @@ -2,7 +2,7 @@ use std::fmt::Write; use serde_json::Value; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum SelectorPart { Field(String), Index(usize), diff --git a/src/tools/jsondoclint/src/json_find/tests.rs b/src/tools/jsondoclint/src/json_find/tests.rs index b3e009729b1..2a533530714 100644 --- a/src/tools/jsondoclint/src/json_find/tests.rs +++ b/src/tools/jsondoclint/src/json_find/tests.rs @@ -1,4 +1,27 @@ +use super::*; + #[test] -fn should_fail() { - assert_eq!(true, false); +fn basic_find() { + use SelectorPart::*; + + let j = serde_json::json!({ + "index": { + "4": { + "inner": { + "items": ["1", "2", "3"] + } + } + } + }); + + let sel = find_selector(&j, &serde_json::json!("1")); + let exp: Vec> = vec![vec![ + Field("index".to_owned()), + Field("4".to_owned()), + Field("inner".to_owned()), + Field("items".to_owned()), + Index(0), + ]]; + + assert_eq!(exp, sel); } diff --git a/src/tools/jsondoclint/src/main.rs b/src/tools/jsondoclint/src/main.rs index 70d7a82a576..fc54c421b4b 100644 --- a/src/tools/jsondoclint/src/main.rs +++ b/src/tools/jsondoclint/src/main.rs @@ -9,13 +9,13 @@ pub(crate) mod item_kind; mod json_find; mod validator; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] struct Error { kind: ErrorKind, id: Id, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] enum ErrorKind { NotFound, Custom(String),