use super::*;
use crate::generated::descriptor::{EnumValueOptions, FieldOptions, MessageOptions};
fn squash(s: &str) -> String {
s.chars().filter(|c| !c.is_whitespace()).collect()
}
fn deprecated(mut field: FieldDescriptorProto) -> FieldDescriptorProto {
field.options = FieldOptions {
deprecated: Some(true),
..Default::default()
}
.into();
field
}
fn deprecated_value(mut value: EnumValueDescriptorProto) -> EnumValueDescriptorProto {
value.options = EnumValueOptions {
deprecated: Some(true),
..Default::default()
}
.into();
value
}
fn deprecated_file() -> FileDescriptorProto {
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Widget".to_string()),
field: vec![
deprecated(make_field(
"legacy_name",
1,
Label::LABEL_OPTIONAL,
Type::TYPE_STRING,
)),
make_field("label", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
],
..Default::default()
});
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![
enum_value("SIZE_UNSPECIFIED", 0),
deprecated_value(enum_value("SMALL", 1)),
deprecated_value(enum_value("TINY", 1)),
],
..Default::default()
});
file
}
fn clean_file() -> FileDescriptorProto {
let mut file = proto3_file("clean.proto");
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Widget".to_string()),
field: vec![make_field(
"legacy_name",
1,
Label::LABEL_OPTIONAL,
Type::TYPE_STRING,
)],
..Default::default()
});
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![enum_value("SIZE_UNSPECIFIED", 0), enum_value("SMALL", 1)],
..Default::default()
});
file
}
fn deprecated_file_with_redacted_label() -> FileDescriptorProto {
let mut file = deprecated_file();
file.message_type[0].field[1].options = FieldOptions {
debug_redact: Some(true),
..Default::default()
}
.into();
file
}
fn oneof_only_file() -> FileDescriptorProto {
let mut member = make_field("legacy_choice", 3, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
member.oneof_index = Some(0);
let mut msg = DescriptorProto {
name: Some("Widget".to_string()),
field: vec![
deprecated(member),
make_field("label", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
],
..Default::default()
};
msg.oneof_decl.push(OneofDescriptorProto {
name: Some("choice".to_string()),
..Default::default()
});
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.message_type.push(msg);
file
}
fn generate_squashed(file: FileDescriptorProto, config: &CodeGenConfig) -> String {
let name = file.name.clone().unwrap_or_default();
let files = generate(&[file], &[name], config).expect("should generate");
squash(&joined(&files))
}
#[test]
fn deprecated_field_carries_marker() {
let content = generate_squashed(deprecated_file(), &CodeGenConfig::default());
assert!(
content.contains(r#"#[deprecated]publegacy_name:"#),
"deprecated field must carry #[deprecated]: {content}"
);
assert!(
!content.contains(r#"#[deprecated]publabel:"#),
"a live field must not carry the marker"
);
}
#[test]
fn codec_and_debug_impls_allow_deprecated() {
let config = CodeGenConfig {
generate_text: true,
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
for impl_head in [
"impl::buffa::MessageforWidget",
"impl::core::fmt::DebugforWidget",
"impl::buffa::text::TextFormatforWidget",
] {
assert!(
content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"{impl_head} visits every field, so it must be guarded: {content}"
);
}
}
#[test]
fn setters_allow_deprecated() {
let mut file = deprecated_file();
let msg = &mut file.message_type[0];
msg.field[0].proto3_optional = Some(true);
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content.contains("#[allow(deprecated)]implWidget{"),
"the with_* setter assigns the deprecated field: {content}"
);
}
#[test]
fn reflection_impl_allows_deprecated() {
let config = CodeGenConfig {
generate_reflection: true,
generate_reflection_vtable: true,
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
assert!(
content.contains(
"#[allow(deprecated)]impl::buffa_descriptor::reflect::ReflectMessageforWidget"
),
"the vtable reads fields by index: {content}"
);
}
#[test]
fn json_deserialize_impl_allows_deprecated() {
let mut msg = DescriptorProto {
name: Some("Widget".to_string()),
field: vec![
deprecated(make_field(
"legacy_name",
1,
Label::LABEL_OPTIONAL,
Type::TYPE_STRING,
)),
make_field("label", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
],
..Default::default()
};
let mut in_oneof = make_field("alias", 3, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
in_oneof.oneof_index = Some(0);
msg.field.push(in_oneof);
msg.oneof_decl.push(OneofDescriptorProto {
name: Some("name_choice".to_string()),
..Default::default()
});
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.message_type.push(msg);
let config = CodeGenConfig {
generate_json: true,
..Default::default()
};
let content = generate_squashed(file, &config);
assert!(
content.contains("#[allow(deprecated)]impl<'de>::serde::Deserialize<'de>forWidget"),
"the visitor assigns the deprecated field: {content}"
);
}
#[test]
fn deprecated_enum_value_carries_marker_on_variant_and_alias() {
let content = generate_squashed(deprecated_file(), &CodeGenConfig::default());
assert!(
content.contains("#[deprecated]SMALL=1"),
"deprecated enum value must mark its variant: {content}"
);
assert!(
content
.contains("#[deprecated]#[allow(non_upper_case_globals)]pubconstTINY:Self=Self::SMALL"),
"an alias of a deprecated value is deprecated too: {content}"
);
assert!(
!content.contains("#[deprecated]SIZE_UNSPECIFIED=0"),
"a live value must not carry the marker"
);
}
#[test]
fn idiomatic_alias_inherits_deprecation() {
let content = generate_squashed(deprecated_file(), &CodeGenConfig::default());
assert!(
content.contains(
"#[deprecated]#[allow(non_upper_case_globals)]pubconstSmall:Self=Self::SMALL"
),
"the CamelCase alias must not be a quiet way to reach a deprecated value: {content}"
);
}
#[test]
fn enum_impls_that_name_variants_allow_deprecated() {
let content = generate_squashed(deprecated_file(), &CodeGenConfig::default());
for impl_head in [
"impl::buffa::EnumerationforSize",
"implSize{",
] {
assert!(
content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"{impl_head} names a deprecated variant, so it must be guarded: {content}"
);
}
assert!(
!content.contains("#[allow(deprecated)]impl::core::default::DefaultforSize"),
"Default names SIZE_UNSPECIFIED only: {content}"
);
}
#[test]
fn first_value_deprecation_does_not_change_default() {
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![
deprecated_value(enum_value("SIZE_UNSPECIFIED", 0)),
enum_value("SMALL", 1),
],
..Default::default()
});
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content.contains("#[allow(deprecated)]impl::core::default::DefaultforSize{fndefault()"),
"Default returns the deprecated first value: {content}"
);
}
fn enum_default_file() -> FileDescriptorProto {
let mut field = make_field("size", 1, Label::LABEL_REQUIRED, Type::TYPE_ENUM);
field.type_name = Some(".deprecate.test.Size".to_string());
field.default_value = Some("OLD".to_string());
let mut file = proto3_file("enum_default.proto");
file.syntax = Some("proto2".to_string());
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Holder".to_string()),
field: vec![field],
..Default::default()
});
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![deprecated_value(enum_value("OLD", 1)), enum_value("NEW", 2)],
..Default::default()
});
file
}
#[test]
fn enum_default_naming_deprecated_value_guards_codec() {
let content = generate_squashed(enum_default_file(), &CodeGenConfig::default());
assert!(
content.contains("#[allow(deprecated)]impl::buffa::MessageforHolder"),
"the codec's clear/default spells out Size::OLD, so it must be guarded: {content}"
);
assert!(
content.contains("Size::OLD"),
"fixture sanity: the default must reach the generated code: {content}"
);
assert!(
content.contains("#[allow(deprecated)]impl::core::default::DefaultforHolder"),
"the custom Default spells out Size::OLD: {content}"
);
assert!(
!content.contains("#[deprecated]pubsize:"),
"a live field must not carry the marker: {content}"
);
}
#[test]
fn enum_default_naming_alias_of_deprecated_value_guards_codec() {
let mut file = enum_default_file();
file.enum_type[0].value.insert(1, enum_value("SMALL", 1));
file.message_type[0].field[0].default_value = Some("SMALL".to_string());
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content
.contains("#[deprecated]#[allow(non_upper_case_globals)]pubconstSMALL:Self=Self::OLD"),
"fixture sanity: the alias inherits the marker: {content}"
);
assert!(
content.contains("Size::SMALL"),
"fixture sanity: the default must reach the generated code: {content}"
);
assert!(
content.contains("#[allow(deprecated)]impl::buffa::MessageforHolder"),
"the codec spells out Size::SMALL, so it must be guarded: {content}"
);
assert!(
content.contains("#[allow(deprecated)]impl::core::default::DefaultforHolder"),
"the custom Default spells out Size::SMALL: {content}"
);
}
#[test]
fn arbitrary_keeps_the_derive_when_only_an_alias_is_deprecated() {
let config = CodeGenConfig {
generate_arbitrary: true,
..Default::default()
};
let mut file = enum_default_file();
file.enum_type[0].value = vec![
enum_value("BIG", 1),
deprecated_value(enum_value("LARGE", 1)),
enum_value("NEW", 2),
];
file.message_type[0].field[0].default_value = Some("NEW".to_string());
let content = generate_squashed(file, &config);
assert!(
!content.contains("impl<'a>::arbitrary::Arbitrary<'a>forSize"),
"no variant is deprecated, so the derive stays: {content}"
);
assert_eq!(
content
.matches("#[cfg_attr(feature=\"arbitrary\",derive(::arbitrary::Arbitrary))]")
.count(),
2,
"`Holder` and `Size` both derive: {content}"
);
}
#[test]
fn idiomatic_const_of_an_alias_inherits_the_variant_marker() {
let config = CodeGenConfig {
idiomatic_enum_aliases: true,
..Default::default()
};
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![
deprecated_value(enum_value("SIZE_OLD", 0)),
enum_value("SIZE_SMALL", 0),
],
..Default::default()
});
let content = generate_squashed(file, &config);
assert!(
content.contains(
"#[deprecated]#[allow(non_upper_case_globals)]pubconstSmall:Self=Self::SIZE_OLD"
),
"the idiomatic const of an inheriting alias carries the marker: {content}"
);
}
#[test]
fn idiomatic_const_of_a_deprecated_alias_is_deprecated() {
let config = CodeGenConfig {
idiomatic_enum_aliases: true,
..Default::default()
};
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![
enum_value("SIZE_BIG", 0),
deprecated_value(enum_value("SIZE_LARGE", 0)),
],
..Default::default()
});
let content = generate_squashed(file, &config);
assert!(
content.contains(
"#[deprecated]#[allow(non_upper_case_globals)]pubconstLarge:Self=Self::SIZE_BIG"
),
"the idiomatic const of the deprecated alias carries the marker: {content}"
);
assert!(
!content.contains(
"#[deprecated]#[allow(non_upper_case_globals)]pubconstBig:Self=Self::SIZE_BIG"
),
"the idiomatic const of the live variant stays unmarked: {content}"
);
}
#[test]
fn enum_default_naming_a_deprecated_alias_of_a_live_variant_guards_codec() {
let mut file = enum_default_file();
file.enum_type[0].value = vec![
enum_value("BIG", 1),
deprecated_value(enum_value("LARGE", 1)),
enum_value("NEW", 2),
];
file.message_type[0].field[0].default_value = Some("LARGE".to_string());
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content.contains("#[allow(deprecated)]impl::buffa::MessageforHolder"),
"the default names the deprecated alias const: {content}"
);
}
#[test]
fn enum_default_naming_a_live_alias_beside_a_deprecated_one_is_unguarded() {
let mut file = enum_default_file();
file.enum_type[0].value = vec![
enum_value("A", 1),
deprecated_value(enum_value("B", 1)),
enum_value("C", 1),
];
file.message_type[0].field[0].default_value = Some("C".to_string());
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content.contains("Size::C"),
"fixture sanity: the default must reach the generated code: {content}"
);
assert!(
!content.contains("#[allow(deprecated)]impl::buffa::MessageforHolder"),
"`C` is live, so the default needs no guard: {content}"
);
}
#[test]
fn enum_default_naming_live_variant_with_deprecated_alias_is_unguarded() {
let mut file = enum_default_file();
file.enum_type[0].value = vec![
enum_value("BIG", 1),
deprecated_value(enum_value("LARGE", 1)),
enum_value("NEW", 2),
];
file.message_type[0].field[0].default_value = Some("BIG".to_string());
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content.contains("Size::BIG"),
"fixture sanity: the default must reach the generated code: {content}"
);
assert!(
!content.contains("#[allow(deprecated)]impl::buffa::MessageforHolder"),
"a default that names a live variant needs no guard: {content}"
);
}
#[test]
fn arbitrary_for_an_enum_with_a_deprecated_value_is_an_impl_not_a_derive() {
let config = CodeGenConfig {
generate_arbitrary: true,
..Default::default()
};
let mut file = enum_default_file();
file.enum_type.push(EnumDescriptorProto {
name: Some("Live".to_string()),
value: vec![enum_value("LIVE_A", 0), enum_value("LIVE_B", 1)],
..Default::default()
});
let content = generate_squashed(file, &config);
assert!(
content.contains("#[cfg(feature=\"arbitrary\")]impl<'a>::arbitrary::Arbitrary<'a>forSize{"),
"an enum with a deprecated value implements Arbitrary by hand: {content}"
);
assert!(
content.contains(
"letvalues=<Selfas::buffa::Enumeration>::values();\
letdraw=<u32as::arbitrary::Arbitrary>::arbitrary(u)?;\
letindex=(u64::from(draw)*values.len()asu64)>>32;\
::core::result::Result::Ok(values[indexasusize])"
),
"the impl maps a `u32` onto the declared values as the derive does: {content}"
);
assert!(
content.contains("<u32as::arbitrary::Arbitrary>::size_hint(depth)"),
"the impl reports the derive's size hint: {content}"
);
assert!(
!content.contains("impl<'a>::arbitrary::Arbitrary<'a>forLive"),
"an enum without one keeps the derive: {content}"
);
assert_eq!(
content
.matches("#[cfg_attr(feature=\"arbitrary\",derive(::arbitrary::Arbitrary))]")
.count(),
2,
"`Holder` and `Live` derive, `Size` does not: {content}"
);
}
#[test]
fn lazy_view_to_owned_allows_deprecated() {
let config = CodeGenConfig {
lazy_views: true,
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
assert!(
content.contains("#[allow(deprecated)]fnto_owned_message"),
"the lazy view's to_owned writes the owned field: {content}"
);
}
#[test]
fn table_static_allows_deprecated() {
let config = CodeGenConfig {
codec_strategy: crate::CodecStrategy::Table,
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
assert!(
content.contains("#[allow(deprecated)]pub(crate)static__BUFFA_TABLE_Widget"),
"the table entries take offsets of deprecated fields: {content}"
);
}
#[test]
fn oneof_only_deprecation_is_unmarked_and_unguarded() {
let content = generate_squashed(oneof_only_file(), &CodeGenConfig::default());
assert!(
!content.contains("#[deprecated]"),
"a deprecated oneof member is not marked: {content}"
);
for impl_head in [
"impl::buffa::MessageforWidget",
"impl<'a>::buffa::MessageView<'a>forWidgetView<'a>",
] {
assert!(
!content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"a oneof-only deprecation must not guard `{impl_head}`: {content}"
);
}
}
#[test]
fn caller_deprecated_oneof_struct_field_guards_the_impls_that_visit_it() {
let rule = |path: &str| CodeGenConfig {
oneof_struct_field_attributes: vec![(
path.to_string(),
"#[deprecated(note = \"hand\")]".to_string(),
)],
..Default::default()
};
let impl_heads = [
"impl::buffa::MessageforWidget",
"impl::core::fmt::DebugforWidget",
"impl<'a>::buffa::MessageView<'a>forWidgetView<'a>",
];
let content = generate_squashed(oneof_only_file(), &rule(".deprecate.test.Widget.choice"));
assert!(
content.contains(r#"#[deprecated(note="hand")]pubchoice:"#),
"the owned struct's oneof field takes the attribute: {content}"
);
assert_eq!(content.matches("#[deprecated").count(), 1, "{content}");
for impl_head in impl_heads {
assert!(
content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"`{impl_head}` visits the deprecated oneof field: {content}"
);
}
let content = generate_squashed(oneof_only_file(), &rule(".deprecate.test.Widget.label"));
assert!(!content.contains("#[deprecated"), "{content}");
assert!(!content.contains("#[allow(deprecated)]"), "{content}");
let mut note = make_field("note", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
note.oneof_index = Some(0);
note.proto3_optional = Some(true);
let mut file = proto3_file("synthetic.proto");
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Widget".to_string()),
field: vec![note],
oneof_decl: vec![OneofDescriptorProto {
name: Some("_note".to_string()),
..Default::default()
}],
..Default::default()
});
for path in [".", ".deprecate.test.Widget._note"] {
let content = generate_squashed(file.clone(), &rule(path));
assert!(!content.contains("#[deprecated"), "{path}: {content}");
assert!(
!content.contains("#[allow(deprecated)]"),
"{path}: {content}"
);
}
}
#[test]
fn view_field_and_owned_view_accessor_carry_marker() {
let content = generate_squashed(deprecated_file(), &CodeGenConfig::default());
assert!(
content.contains("#[deprecated]publegacy_name:&'astr"),
"the view struct's field carries the marker"
);
assert!(
content.contains(
"#[deprecated]#[allow(deprecated)]#[must_use]pubfnlegacy_name(&self)->&'_str"
),
"the `WidgetOwnedView` accessor carries the marker and guards its own read"
);
assert!(
!content.contains("#[deprecated]publabel:&'astr")
&& content.contains("///Field2:`label`#[must_use]pubfnlabel(&self)"),
"a live field's view field and accessor stay unmarked"
);
}
#[test]
fn view_impls_that_touch_fields_allow_deprecated() {
let file = deprecated_file_with_redacted_label();
let config = CodeGenConfig {
generate_json: true,
generate_reflection: true,
generate_reflection_vtable: true,
..Default::default()
};
let content = generate_squashed(file, &config);
for impl_head in [
"impl<'a>::buffa::MessageView<'a>forWidgetView<'a>",
"impl<'a>::buffa::ViewEncode<'a>forWidgetView<'a>",
"impl<'a>::core::fmt::DebugforWidgetView<'a>",
"impl<'__a>::serde::SerializeforWidgetView<'__a>",
"impl<'a>::buffa_descriptor::reflect::ReflectMessageforWidgetView<'a>",
] {
assert!(
content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"`{impl_head}` reads or writes the deprecated view field, so it must be guarded"
);
}
}
#[test]
fn lazy_view_field_carries_marker_and_its_impls_allow_deprecated() {
let file = deprecated_file_with_redacted_label();
let config = CodeGenConfig {
lazy_views: true,
generate_json: true,
..Default::default()
};
let content = generate_squashed(file, &config);
let lazy = &content[content
.find("pubstructWidgetLazyView<'a>")
.expect("the lazy view struct")..];
assert!(
lazy.contains("#[deprecated]publegacy_name:&'astr"),
"the lazy view struct's field carries the marker"
);
for impl_head in [
"impl<'a>::core::fmt::DebugforWidgetLazyView<'a>",
"impl<'__a>::serde::SerializeforWidgetLazyView<'__a>",
] {
assert!(
content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"`{impl_head}` reads or writes the deprecated lazy-view field, so it must be guarded"
);
}
assert_eq!(
content
.matches("#[allow(deprecated)]impl<'a>WidgetLazyView<'a>{")
.count(),
2,
"the decode and the encode inherent impls are both guarded"
);
assert!(
!content.contains("#[allow(deprecated)]impl<'a>::buffa::LazyMessageView<'a>"),
"the `LazyMessageView` impl delegates; only its `to_owned_message` is guarded"
);
}
#[test]
fn caller_deprecated_attribute_marks_the_view_without_its_note() {
let config = CodeGenConfig {
field_attributes: vec![(
".deprecate.test.Widget.legacy_name".to_string(),
"#[deprecated(note = \"hand\")]".to_string(),
)],
..Default::default()
};
let content = generate_squashed(clean_file(), &config);
assert_eq!(
content.matches(r#"note="hand""#).count(),
1,
"the caller's attribute lands on the owned field only: {content}"
);
assert!(
content.contains("#[deprecated]publegacy_name:&'astr"),
"the view field carries a bare marker"
);
assert!(
content.contains("#[deprecated]#[allow(deprecated)]#[must_use]pubfnlegacy_name(&self)"),
"the `WidgetOwnedView` accessor carries a bare marker"
);
assert!(
content.contains("#[allow(deprecated)]impl<'a>::buffa::MessageView<'a>forWidgetView<'a>"),
"so the view's impls are guarded"
);
}
#[test]
fn caller_deprecated_fields_are_marked_and_guarded_on_every_view_surface() {
let mut limits = make_field("limits", 1, Label::LABEL_REQUIRED, Type::TYPE_MESSAGE);
limits.type_name = Some(".deprecate.test.Limits".to_string());
let mut label = make_field("label", 3, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
label.options = FieldOptions {
debug_redact: Some(true),
..Default::default()
}
.into();
let mut file = proto3_file("required.proto");
file.syntax = Some("proto2".to_string());
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Limits".to_string()),
..Default::default()
});
file.message_type.push(DescriptorProto {
name: Some("Widget".to_string()),
field: vec![
limits,
make_field("code", 2, Label::LABEL_REQUIRED, Type::TYPE_INT32),
label,
],
..Default::default()
});
let note = "#[deprecated(note = \"hand\")]".to_string();
let config = CodeGenConfig {
field_attributes: vec![
(".deprecate.test.Widget.limits".to_string(), note.clone()),
(".deprecate.test.Widget.code".to_string(), note),
],
lazy_views: true,
generate_json: true,
generate_reflection: true,
generate_reflection_vtable: true,
..Default::default()
};
let content = generate_squashed(file, &config);
assert_eq!(
content.matches(r#"note="hand""#).count(),
2,
"the caller's attribute stays on the two owned fields: {content}"
);
for field in ["limits", "code"] {
assert_eq!(
content
.matches(&format!("#[deprecated]pub{field}:"))
.count(),
2,
"`{field}` carries a bare marker on the view and the lazy view: {content}"
);
assert_eq!(
content
.matches(&format!(
"#[deprecated]#[allow(deprecated)]#[must_use]#[inline]pubconstfnhas_{field}(&self)"
))
.count(),
2,
"`has_{field}` is marked on the view and the lazy view: {content}"
);
assert!(
content.contains(&format!(
"#[deprecated]#[allow(deprecated)]#[must_use]pubfn{field}("
)),
"the `WidgetOwnedView` accessor for `{field}` is marked: {content}"
);
}
for impl_head in [
"impl<'a>::buffa::MessageView<'a>forWidgetView<'a>",
"impl<'a>::buffa::ViewEncode<'a>forWidgetView<'a>",
"impl<'a>::core::fmt::DebugforWidgetView<'a>",
"impl<'__a>::serde::SerializeforWidgetView<'__a>",
"impl<'a>::buffa_descriptor::reflect::ReflectMessageforWidgetView<'a>",
"impl<'a>::core::fmt::DebugforWidgetLazyView<'a>",
"impl<'__a>::serde::SerializeforWidgetLazyView<'__a>",
] {
assert!(
content.contains(&format!("#[allow(deprecated)]{impl_head}")),
"`{impl_head}` names a caller-deprecated view field, so it must be guarded"
);
}
assert_eq!(
content
.matches("#[allow(deprecated)]impl<'a>WidgetLazyView<'a>{")
.count(),
2,
"the lazy view's decode and encode inherent impls are both guarded"
);
}
#[test]
fn deprecated_map_field_carries_marker_on_each_struct() {
let mut key = make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
key.json_name = Some("key".to_string());
let mut value = make_field("value", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING);
value.json_name = Some("value".to_string());
let entry = DescriptorProto {
name: Some("AttrsEntry".to_string()),
field: vec![key, value],
options: MessageOptions {
map_entry: Some(true),
..Default::default()
}
.into(),
..Default::default()
};
let mut attrs = make_field("attrs", 1, Label::LABEL_REPEATED, Type::TYPE_MESSAGE);
attrs.type_name = Some(".deprecate.test.Widget.AttrsEntry".to_string());
let mut file = proto3_file("map.proto");
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Widget".to_string()),
field: vec![deprecated(attrs)],
nested_type: vec![entry],
..Default::default()
});
let config = CodeGenConfig {
lazy_views: true,
..Default::default()
};
let content = generate_squashed(file, &config);
assert_eq!(
content.matches("#[deprecated]pubattrs:").count(),
3,
"the owned struct, the view and the lazy view each mark the map field: {content}"
);
}
#[test]
fn has_method_of_a_deprecated_required_field_is_deprecated() {
let mut limits = make_field("limits", 1, Label::LABEL_REQUIRED, Type::TYPE_MESSAGE);
limits.type_name = Some(".deprecate.test.Limits".to_string());
let mut file = proto3_file("required.proto");
file.syntax = Some("proto2".to_string());
file.package = Some("deprecate.test".to_string());
file.message_type.push(DescriptorProto {
name: Some("Limits".to_string()),
..Default::default()
});
file.message_type.push(DescriptorProto {
name: Some("Quota".to_string()),
field: vec![
deprecated(limits),
deprecated(make_field(
"code",
2,
Label::LABEL_REQUIRED,
Type::TYPE_INT32,
)),
make_field("live", 3, Label::LABEL_REQUIRED, Type::TYPE_INT32),
],
..Default::default()
});
let config = CodeGenConfig {
lazy_views: true,
..Default::default()
};
let content = generate_squashed(file, &config);
let marked = "#[deprecated]#[allow(deprecated)]#[must_use]#[inline]pubconstfn";
for method in ["has_limits", "has_code"] {
assert_eq!(
content
.matches(&format!("{marked}{method}(&self)->bool"))
.count(),
2,
"`{method}` is marked and guarded on the view and the lazy view: {content}"
);
}
assert_eq!(
content
.matches("#[must_use]#[inline]pubconstfnhas_live(&self)->bool")
.count(),
2,
"the live field keeps its accessor on both views"
);
assert!(
!content.contains(&format!("{marked}has_live")),
"a live required field's `has_*` stays unmarked"
);
}
#[test]
fn file_without_deprecation_emits_no_attributes() {
let content = generate_squashed(clean_file(), &CodeGenConfig::default());
assert!(!content.contains("#[deprecated]"), "no option, no marker");
assert!(
!content.contains("#[allow(deprecated)]"),
"guards must not leak into messages that do not need them: {content}"
);
}
#[test]
fn caller_deprecated_attribute_is_not_duplicated() {
let config = CodeGenConfig {
field_attributes: vec![(
".deprecate.test.Widget.legacy_name".to_string(),
r#"#[deprecated(note = "use label")]"#.to_string(),
)],
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
let field_pos = content.find("publegacy_name:").expect("legacy_name field");
let start = content[..field_pos]
.rfind("#[deprecated")
.expect("a deprecated attribute above the field");
let between = &content[start..field_pos];
assert!(
between.contains(r#"note="uselabel""#),
"the caller's note survives: {between}"
);
assert_eq!(
between.matches("#[deprecated").count(),
1,
"exactly one #[deprecated] on the field, got: {between}"
);
assert_eq!(
content.matches(r#"note="uselabel""#).count(),
1,
"the note must not be duplicated onto other fields"
);
}
#[test]
fn caller_deprecated_attribute_on_other_field_still_gets_marker() {
let config = CodeGenConfig {
field_attributes: vec![(
".deprecate.test.Widget.label".to_string(),
"#[deprecated(note = \"hand\")]".to_string(),
)],
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
assert!(
content.contains("#[deprecated]publegacy_name:"),
"unmatched field keeps the option-derived marker: {content}"
);
assert!(
content.contains(r#"note="hand""#),
"the caller's attribute still lands on its own field: {content}"
);
}
#[test]
fn caller_deprecated_attribute_in_a_multi_attribute_string_is_detected() {
let config = CodeGenConfig {
field_attributes: vec![(
".deprecate.test.Widget.legacy_name".to_string(),
"#[allow(non_snake_case)] #[deprecated(note = \"use label\")]".to_string(),
)],
..Default::default()
};
let content = generate_squashed(deprecated_file(), &config);
let field_pos = content.find("publegacy_name:").expect("legacy_name field");
let start = content[..field_pos]
.rfind("#[deprecated")
.expect("a deprecated attribute above the field");
assert_eq!(
content[start..field_pos].matches("#[deprecated").count(),
1,
"the option marker must yield to a marker in a multi-attribute string, \
two would be a hard error: {}",
&content[start..field_pos]
);
}
#[test]
fn deprecated_field_setter_is_also_deprecated() {
let mut file = deprecated_file();
file.message_type[0].field[0].proto3_optional = Some(true);
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
content.contains("#[deprecated]pubfnwith_legacy_name"),
"the setter for a deprecated field must be deprecated: {content}"
);
assert!(
!content.contains("#[deprecated]pubfnwith_label"),
"the setter for a live field stays unmarked"
);
}
#[test]
fn alias_deprecated_while_primary_is_live_marks_only_the_alias() {
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
file.enum_type.push(EnumDescriptorProto {
name: Some("Size".to_string()),
value: vec![
enum_value("BIG", 1),
deprecated_value(enum_value("LARGE", 1)),
],
..Default::default()
});
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
!content.contains("#[deprecated]BIG=1"),
"the primary is live: {content}"
);
assert!(
content
.contains("#[deprecated]#[allow(non_upper_case_globals)]pubconstLARGE:Self=Self::BIG"),
"the deprecated alias carries the marker: {content}"
);
assert!(
!content
.contains("#[deprecated]#[allow(non_upper_case_globals)]pubconstBig:Self=Self::BIG"),
"the primary's idiomatic alias must not inherit from the alias: {content}"
);
}
#[test]
fn attribute_shape_detection() {
let cases: &[(&str, bool)] = &[
("#[deprecated]", true),
("#[deprecated(note = \"use x\")]", true),
("#[deprecated(note = \"a\")]\n#[allow(dead_code)]", true),
("#[allow(non_snake_case)] #[deprecated]", true),
("#[cfg_attr(feature = \"json\", deprecated)]", true),
(
"#[cfg_attr(feature = \"json\", deprecated, serde(skip))]",
true,
),
("#[deprecated_alias]", false),
("#[some_tool::deprecated]", false),
("#[cfg_attr(feature = \"json\", allow(deprecated))]", false),
("#[cfg_attr(a, cfg_attr(b, deprecated))]", true),
("#[cfg_attr(deprecated, serde(skip))]", false),
("#[schema(deprecated)]", false),
("#[serde(skip)]", false),
("#[doc = \"marked deprecated in the proto\"]", false),
("not an attribute", false),
("#![allow(deprecated)] #[deprecated]", true),
];
for (attr, expected) in cases {
assert_eq!(
crate::message::is_deprecated_attr_str(attr),
*expected,
"shape {attr:?} should be {expected}"
);
}
}
#[test]
fn message_level_deprecated_option_is_not_emitted() {
let mut msg = DescriptorProto {
name: Some("Widget".to_string()),
field: vec![make_field(
"label",
1,
Label::LABEL_OPTIONAL,
Type::TYPE_STRING,
)],
options: MessageOptions {
deprecated: Some(true),
..Default::default()
}
.into(),
..Default::default()
};
let mut file = proto3_file("deprecated.proto");
file.package = Some("deprecate.test".to_string());
msg.options = MessageOptions {
deprecated: Some(true),
..Default::default()
}
.into();
file.message_type.push(msg);
let content = generate_squashed(file, &CodeGenConfig::default());
assert!(
!content.contains("#[deprecated]pubstructWidget"),
"message-level `deprecated` is not emitted: {content}"
);
}