-
Notifications
You must be signed in to change notification settings - Fork 0
Add error messages to the panic in tests and use should_panic more effectively #66
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ pub use bauble_macros::Bauble; | |
|
|
||
| pub use builtin::Ref; | ||
| pub use context::{BaubleContext, BaubleContextBuilder, FileId, PathReference, Source}; | ||
| pub use error::{BaubleError, BaubleErrors, CustomError, Level, print_errors}; | ||
| pub use error::{BaubleError, BaubleErrors, CustomError, Level}; | ||
| pub use spanned::{Span, SpanExt, Spanned}; | ||
| pub use traits::{ | ||
| Bauble, BaubleAllocator, DefaultAllocator, ToRustError, ToRustErrorKind, VariantKind, | ||
|
|
@@ -72,8 +72,8 @@ pub mod private { | |
| let (objects, errors) = ctx.write().unwrap().load_all(); | ||
|
|
||
| if !errors.is_empty() { | ||
| crate::print_errors(Err::<(), _>(errors), &ctx.read().unwrap()); | ||
| panic!("Error converting"); | ||
| let error_msg = errors.try_to_string(&ctx.read().unwrap()).unwrap(); | ||
| panic!("Error converting: \n{error_msg}"); | ||
| } | ||
|
|
||
| // Test round-trip of objects through source format | ||
|
|
@@ -107,11 +107,12 @@ pub mod private { | |
| .reload_paths(re_path_sources.iter().map(|(p, s)| (p.borrow(), s))); | ||
|
|
||
| if !errors.is_empty() { | ||
| crate::print_errors(Err::<(), _>(errors), &ctx.read().unwrap()); | ||
| let mut error_msg = errors.try_to_string(&ctx.read().unwrap()).unwrap(); | ||
| for (path, re_source) in re_path_sources { | ||
| eprintln!("In file \"{path}\": {re_source}"); | ||
| use std::fmt::Write; | ||
| writeln!(&mut error_msg, "In file \"{path}\": {re_source}").unwrap(); | ||
| } | ||
| panic!("Error re-converting"); | ||
| panic!("Error re-converting: \n{error_msg}"); | ||
| } | ||
|
|
||
| assert_eq!(objects, re_objects); | ||
|
|
@@ -121,6 +122,30 @@ pub mod private { | |
| compare_objects(objects, ctx); | ||
| compare_objects(re_objects, ctx); | ||
| } | ||
|
|
||
| /// Converts bauble object to the type of the `expected_value` and then asserts that they are | ||
| /// the same. | ||
| /// | ||
| /// Panics if converting object fails. | ||
| pub fn expected_from_bauble<T>( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't reuse this in |
||
| expected_value: T, | ||
| value: crate::Object, | ||
| ctx: &std::sync::RwLock<crate::BaubleContext>, | ||
| ) where | ||
| T: PartialEq + for<'a> crate::Bauble<'a> + std::fmt::Debug, | ||
| { | ||
| match <T as crate::Bauble>::from_bauble(value.value, &crate::DefaultAllocator) { | ||
| Ok(read_value) => { | ||
| assert_eq!(read_value, expected_value); | ||
| } | ||
| Err(error) => { | ||
| let error_msg = crate::BaubleErrors::from(error) | ||
| .try_to_string(&ctx.read().unwrap()) | ||
| .unwrap(); | ||
| panic!("Error converting object to rust value: \n{error_msg}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO(@docs) | ||
|
|
@@ -147,16 +172,7 @@ macro_rules! bauble_test { | |
|
|
||
| $( | ||
| let value = objects.next().expect("Not as many objects as test expr in bauble test?"); | ||
| // Infer type for `read_value` to be the same as `test_value`. | ||
| let [test_value, read_value] = [ | ||
| $test_value, | ||
| $crate::print_errors( | ||
| $crate::Bauble::from_bauble(value.value, &$crate::DefaultAllocator), | ||
| &ctx.read().unwrap() | ||
| ).unwrap(), | ||
| ]; | ||
|
|
||
| assert_eq!(read_value, test_value); | ||
| $crate::private::expected_from_bauble($test_value, value, &ctx); | ||
| )* | ||
|
|
||
| if objects.next().is_some() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this can ever fail, but I wanted to keep any potential panicking out the main
baubleAPI. So this returns a result.