Skip to content

Commit

Permalink
format_tools : fixed tests (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakapoi authored Sep 3, 2024
1 parent ba1a066 commit b96531c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 93 deletions.
98 changes: 51 additions & 47 deletions module/core/format_tools/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,70 @@ Using the `to_string_with_fallback` macro to convert values to strings with a pr
```rust
fn main()
{
// Import necessary traits and the macro from the `format_tools` crate.
use core::fmt;
use format_tools::
#[ cfg( feature = "enabled" ) ]
{
WithDebug,
WithDisplay,
to_string_with_fallback,
};

// Define a struct that implements both Debug and Display traits.
struct Both;
// Import necessary traits and the macro from the `format_tools` crate.
use core::fmt;
use format_tools::
{
WithDebug,
WithDisplay,
to_string_with_fallback,
};

// Implement the Debug trait for the Both struct.
impl fmt::Debug for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
// Define a struct that implements both Debug and Display traits.
struct Both;

// Implement the Debug trait for the Both struct.
impl fmt::Debug for Both
{
write!( f, "This is debug" )
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
}

// Implement the Display trait for the Both struct.
impl fmt::Display for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
// Implement the Display trait for the Both struct.
impl fmt::Display for Both
{
write!( f, "This is display" )
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is display" )
}
}
}

// Define a struct that implements only the Debug trait.
struct OnlyDebug;
// Define a struct that implements only the Debug trait.
struct OnlyDebug;

// Implement the Debug trait for the OnlyDebug struct.
impl fmt::Debug for OnlyDebug
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
// Implement the Debug trait for the OnlyDebug struct.
impl fmt::Debug for OnlyDebug
{
write!( f, "This is debug" )
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
}

// Example usage: Using Both which implements both Debug and Display.
let src = Both;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is display".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

// Example usage: Using OnlyDebug which implements only Debug.
let src = OnlyDebug;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is not available, so the fallback WithDebug is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is debug".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

// Example usage: Using Both which implements both Debug and Display.
let src = Both;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is display".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

// Example usage: Using OnlyDebug which implements only Debug.
let src = OnlyDebug;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is not available, so the fallback WithDebug is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is debug".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

}
}
```

Expand Down
96 changes: 50 additions & 46 deletions module/core/format_tools/examples/format_tools_trivial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,68 @@
fn main()
{
// Import necessary traits and the macro from the `format_tools` crate.
use core::fmt;
use format_tools::
#[ cfg( feature = "enabled" ) ]
{
WithDebug,
WithDisplay,
to_string_with_fallback,
};

// Define a struct that implements both Debug and Display traits.
struct Both;
// Import necessary traits and the macro from the `format_tools` crate.
use core::fmt;
use format_tools::
{
WithDebug,
WithDisplay,
to_string_with_fallback,
};

// Implement the Debug trait for the Both struct.
impl fmt::Debug for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
// Define a struct that implements both Debug and Display traits.
struct Both;

// Implement the Debug trait for the Both struct.
impl fmt::Debug for Both
{
write!( f, "This is debug" )
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
}

// Implement the Display trait for the Both struct.
impl fmt::Display for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
// Implement the Display trait for the Both struct.
impl fmt::Display for Both
{
write!( f, "This is display" )
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is display" )
}
}
}

// Define a struct that implements only the Debug trait.
struct OnlyDebug;
// Define a struct that implements only the Debug trait.
struct OnlyDebug;

// Implement the Debug trait for the OnlyDebug struct.
impl fmt::Debug for OnlyDebug
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
// Implement the Debug trait for the OnlyDebug struct.
impl fmt::Debug for OnlyDebug
{
write!( f, "This is debug" )
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
}

// Example usage: Using Both which implements both Debug and Display.
let src = Both;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is display".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

// Example usage: Using OnlyDebug which implements only Debug.
let src = OnlyDebug;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is not available, so the fallback WithDebug is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is debug".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );
// Example usage: Using Both which implements both Debug and Display.
let src = Both;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is display".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

// Example usage: Using OnlyDebug which implements only Debug.
let src = OnlyDebug;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is not available, so the fallback WithDebug is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is debug".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );

}
}

0 comments on commit b96531c

Please sign in to comment.