diff --git a/src/adapter/tests.rs b/src/adapter/tests.rs index 434a30c4..77f6d30f 100644 --- a/src/adapter/tests.rs +++ b/src/adapter/tests.rs @@ -5754,3 +5754,138 @@ fn union_field_positions() { similar_asserts::assert_eq!(expected_results, results); } + +#[test] +fn non_exhaustive_attribute() { + get_test_data!(data, non_exhaustive_attribute); + let adapter = RustdocAdapter::new(&data, None); + let adapter = Arc::new(&adapter); + + let enum_query = r#" + { + Crate { + item { + ... on Enum { + enum_name: name @output + enum_attrs: attrs @output + + enum_attr_: attribute @optional { + raw: raw_attribute @output + content { + base @output + } + } + + variant { + variant: name @output + variant_attrs: attrs @output + + variant_attr_: attribute @optional { + raw: raw_attribute @output + content { + base @output + } + } + } + } + } + } + } + "#; + + let variables: BTreeMap<&str, &str> = BTreeMap::new(); + let schema = + Schema::parse(include_str!("../rustdoc_schema.graphql")).expect("schema failed to parse"); + + #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize)] + struct EnumOutput { + enum_name: String, + enum_attrs: Vec, + enum_attr_raw: Option, + enum_attr_base: Option, + variant: String, + variant_attrs: Vec, + variant_attr_raw: Option, + variant_attr_base: Option, + } + + let mut results: Vec = + trustfall::execute_query(&schema, adapter.clone(), enum_query, variables) + .expect("failed to run query") + .map(|row| row.try_into_struct().expect("shape mismatch")) + .collect(); + results.sort_unstable(); + + let mut expected_results = vec![ + EnumOutput { + enum_name: "NonExhaustiveEnum".into(), + enum_attrs: vec!["#[non_exhaustive]".into()], + enum_attr_raw: Some("#[non_exhaustive]".into()), + enum_attr_base: Some("non_exhaustive".into()), + variant: "First".into(), + variant_attrs: vec![], + variant_attr_raw: None, + variant_attr_base: None, + }, + EnumOutput { + enum_name: "MyEnum".into(), + enum_attrs: vec![], + enum_attr_raw: None, + enum_attr_base: None, + variant: "NonExhaustiveVariant".into(), + variant_attrs: vec!["#[non_exhaustive]".into()], + variant_attr_raw: Some("#[non_exhaustive]".into()), + variant_attr_base: Some("non_exhaustive".into()), + }, + ]; + expected_results.sort_unstable(); + + similar_asserts::assert_eq!(expected_results, results); + + let struct_query = r#" + { + Crate { + item { + ... on Struct { + name @output + attrs @output + + attr_: attribute @optional { + raw: raw_attribute @output + content { + base @output + } + } + } + } + } + } + "#; + + let variables: BTreeMap<&str, &str> = BTreeMap::new(); + + #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize)] + struct Output { + name: String, + attrs: Vec, + attr_raw: Option, + attr_base: Option, + } + + let mut results: Vec = + trustfall::execute_query(&schema, adapter.clone(), struct_query, variables) + .expect("failed to run query") + .map(|row| row.try_into_struct().expect("shape mismatch")) + .collect(); + results.sort_unstable(); + + let mut expected_results = vec![Output { + name: "NonExhaustive".into(), + attrs: vec!["#[non_exhaustive]".into()], + attr_raw: Some("#[non_exhaustive]".into()), + attr_base: Some("non_exhaustive".into()), + }]; + expected_results.sort_unstable(); + + similar_asserts::assert_eq!(expected_results, results); +} diff --git a/test_crates/non_exhaustive_attribute/Cargo.toml b/test_crates/non_exhaustive_attribute/Cargo.toml new file mode 100644 index 00000000..5e2b5d83 --- /dev/null +++ b/test_crates/non_exhaustive_attribute/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "non_exhaustive_attribute" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/non_exhaustive_attribute/src/lib.rs b/test_crates/non_exhaustive_attribute/src/lib.rs new file mode 100644 index 00000000..e969a692 --- /dev/null +++ b/test_crates/non_exhaustive_attribute/src/lib.rs @@ -0,0 +1,14 @@ +#[non_exhaustive] +pub enum NonExhaustiveEnum { + First, +} + +#[non_exhaustive] +pub struct NonExhaustive { + pub x: i64, +} + +pub enum MyEnum { + #[non_exhaustive] + NonExhaustiveVariant(i64), +}