Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we parse the #[non_exhaustive] attribute correctly. (#782) #785

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions src/adapter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
enum_attr_raw: Option<String>,
enum_attr_base: Option<String>,
variant: String,
variant_attrs: Vec<String>,
variant_attr_raw: Option<String>,
variant_attr_base: Option<String>,
}

let mut results: Vec<EnumOutput> =
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<String>,
attr_raw: Option<String>,
attr_base: Option<String>,
}

let mut results: Vec<Output> =
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);
}
6 changes: 6 additions & 0 deletions test_crates/non_exhaustive_attribute/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "non_exhaustive_attribute"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions test_crates/non_exhaustive_attribute/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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),
}