Kriskras99 commented on code in PR #514:
URL: https://github.com/apache/avro-rs/pull/514#discussion_r2988902070
##########
avro_test_helper/src/logger.rs:
##########
@@ -53,47 +53,89 @@ fn test_logger() -> &'static TestLogger {
})
}
+/// Clears all log messages of this thread.
pub fn clear_log_messages() {
- LOG_MESSAGES.with(|msgs| match msgs.try_borrow_mut() {
- Ok(mut log_messages) => log_messages.clear(),
- Err(err) => panic!("Failed to clear log messages: {err:?}"),
- });
+ LOG_MESSAGES.with_borrow_mut(Vec::clear);
}
+/// Asserts that the message is not the last in the log for this thread.
+///
+/// # Panics
+/// Will panic if the provided message is an exact match for the last log
message.
+///
+/// # Example
+/// ```should_panic
+/// use apache_avro_test_helper::logger::assert_not_logged;
+///
+/// log::error!("Something went wrong");
+/// log::error!("Unexpected Error");
+///
+/// // This will not panic as it is not an exact match
+/// assert_not_logged("No Unexpected Error");
+///
+/// // This will not panic as it is not the last log message
+/// assert_not_logged("Something went wrong");
+///
+/// // This will panic
+/// assert_not_logged("Unexpected Error");
+/// ```
#[track_caller]
pub fn assert_not_logged(unexpected_message: &str) {
- LOG_MESSAGES.with(|msgs| match msgs.borrow().last() {
- Some(last_log) if last_log == unexpected_message => {
- panic!("The following log message should not have been logged:
'{unexpected_message}'")
- }
- _ => (),
+ LOG_MESSAGES.with_borrow(|msgs| {
+ let is_logged = msgs.iter().any(|msg| msg == unexpected_message);
+ assert!(
+ !is_logged,
+ "The following log message should not have been logged:
'{unexpected_message}'"
+ );
});
}
+/// Asserts that the message has been logged and removes it from the log of
this thread.
+///
+/// # Panics
+/// Will panic if the message does not appear in the log.
+///
+/// # Example
+/// ```should_panic
+/// use apache_avro_test_helper::logger::assert_logged;
+///
Review Comment:
```suggestion
/// # use apache_avro_test_helper::logger::assert_logged;
/// #
```
Hide imports from user
##########
avro_test_helper/src/logger.rs:
##########
@@ -53,47 +53,89 @@ fn test_logger() -> &'static TestLogger {
})
}
+/// Clears all log messages of this thread.
pub fn clear_log_messages() {
- LOG_MESSAGES.with(|msgs| match msgs.try_borrow_mut() {
- Ok(mut log_messages) => log_messages.clear(),
- Err(err) => panic!("Failed to clear log messages: {err:?}"),
- });
+ LOG_MESSAGES.with_borrow_mut(Vec::clear);
}
+/// Asserts that the message is not the last in the log for this thread.
+///
+/// # Panics
+/// Will panic if the provided message is an exact match for the last log
message.
+///
+/// # Example
+/// ```should_panic
+/// use apache_avro_test_helper::logger::assert_not_logged;
+///
+/// log::error!("Something went wrong");
+/// log::error!("Unexpected Error");
+///
+/// // This will not panic as it is not an exact match
+/// assert_not_logged("No Unexpected Error");
+///
+/// // This will not panic as it is not the last log message
+/// assert_not_logged("Something went wrong");
+///
+/// // This will panic
+/// assert_not_logged("Unexpected Error");
+/// ```
Review Comment:
```suggestion
/// Asserts that the message is not in the log for this thread.
///
/// # Panics
/// Will panic if the provided message is an exact match for any message in
the log.
///
/// # Example
/// ```should_panic
/// # use apache_avro_test_helper::logger::assert_not_logged;
/// #
/// log::error!("Unexpected Error");
///
/// // This will not panic as it is not an exact match
/// assert_not_logged("No Unexpected Error");
///
/// // This will panic
/// assert_not_logged("Unexpected Error");
/// ```
```
##########
avro_derive/src/lib.rs:
##########
@@ -448,15 +448,16 @@ fn type_to_field_default_expr(ty: &Type) ->
Result<TokenStream, Vec<syn::Error>>
}
/// Stolen from serde
-fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
+fn to_compile_errors(errors: &[syn::Error]) -> proc_macro2::TokenStream {
Review Comment:
You could also do
```suggestion
fn to_compile_errors(errors: impl AsRef<[syn::Error]>) ->
proc_macro2::TokenStream {
```
then the `.unwrap_or_else(|errs| to_compile_errors(errs.as_slice()))` at
line 52 can stay `.unwrap_or_else(to_compile_errors)`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]