Skip to content

Commit

Permalink
test(custom_debug): Merge skip_debug into custom_debug (#1178)
Browse files Browse the repository at this point in the history
- Rename `skip_debug` into `custom_debug`
- Move related tests to the module
- Make config dependency explicit in `build.rs`
  • Loading branch information
caspermeijn authored Nov 1, 2024
1 parent c29047e commit 387e9cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ fn main() {
config.field_attribute("Foo.Custom.Attrs.AnotherEnum.D", "/// The D docs");
config.field_attribute("Foo.Custom.Attrs.Msg.field.a", "/// Oneof A docs");
config.field_attribute("Foo.Custom.Attrs.Msg.field.b", "/// Oneof B docs");
config.skip_debug(["custom_debug.Msg"]);

config.file_descriptor_set_path(
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"))
Expand Down Expand Up @@ -103,7 +102,8 @@ fn main() {
.compile_protos(&[src.join("default_string_escape.proto")], includes)
.unwrap();

config
prost_build::Config::new()
.skip_debug(["custom_debug.Msg"])
.compile_protos(&[src.join("custom_debug.proto")], includes)
.unwrap();

Expand Down
22 changes: 13 additions & 9 deletions tests/src/skip_debug.rs → tests/src/custom_debug.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
//! Tests for skipping the default Debug implementation.
use std::fmt;
include!(concat!(env!("OUT_DIR"), "/custom_debug.rs"));

use prost::alloc::format;
#[cfg(not(feature = "std"))]
use prost::alloc::string::String;
use alloc::format;
use alloc::string::String;
use alloc::string::ToString;
use core::fmt;

use crate::custom_debug::{msg, AnEnum, Msg};
use crate::message_encoding::BasicEnumeration;
impl fmt::Debug for Msg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Msg {..}")
}
}

/// A special case with a tuple struct
#[test]
fn tuple_struct_custom_debug() {
#[derive(Clone, PartialEq, prost::Message)]
#[prost(skip_debug)]
struct NewType(#[prost(enumeration = "BasicEnumeration", tag = "5")] i32);
struct NewType(#[prost(enumeration = "AnEnum", tag = "5")] i32);
impl fmt::Debug for NewType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("NewType(custom_debug)")
}
}
assert_eq!(
format!("{:?}", NewType(BasicEnumeration::TWO as i32)),
format!("{:?}", NewType(AnEnum::B as i32)),
"NewType(custom_debug)"
);
assert_eq!(format!("{:?}", NewType(42)), "NewType(custom_debug)");
Expand Down Expand Up @@ -59,7 +63,7 @@ impl fmt::Debug for MessageWithOneofCustomDebug {
/// Enumerations inside oneofs
#[test]
fn oneof_with_enum_custom_debug() {
let of = OneofWithEnumCustomDebug::Enumeration(BasicEnumeration::TWO as i32);
let of = OneofWithEnumCustomDebug::Enumeration(AnEnum::B as i32);
assert_eq!(format!("{:?}", of), "OneofWithEnumCustomDebug {..}");
let msg = MessageWithOneofCustomDebug { of: Some(of) };
assert_eq!(format!("{:?}", msg), "MessageWithOneofCustomDebug {..}");
Expand Down
17 changes: 3 additions & 14 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ mod no_shadowed_types;
#[cfg(test)]
mod no_unused_results;
#[cfg(test)]
#[cfg(feature = "std")]
mod skip_debug;
#[cfg(test)]
mod submessage_without_package;
#[cfg(test)]
mod type_names;

#[cfg(test)]
mod boxed_field;

#[cfg(test)]
mod custom_debug;

mod test_enum_named_option_value {
include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs"));
}
Expand Down Expand Up @@ -89,17 +89,6 @@ pub mod recursive_oneof {
include!(concat!(env!("OUT_DIR"), "/recursive_oneof.rs"));
}

#[cfg(feature = "std")]
pub mod custom_debug {
use std::fmt;
include!(concat!(env!("OUT_DIR"), "/custom_debug.rs"));
impl fmt::Debug for Msg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Msg {..}")
}
}
}

/// This tests the custom attributes support by abusing docs.
///
/// Docs really are full-blown attributes. So we use them to ensure we can place them on everything
Expand Down

0 comments on commit 387e9cf

Please sign in to comment.