@@ -437,61 +437,96 @@ fn always_fails() -> Result<()> {
437437# always_fails().unwrap_err();
438438```
439439
440- ## Integrations with other crates
440+ ## Conversion from ` Result::Err ` and ` Option::None `
441441
442- GoogleTest Rust includes integrations with the
443- [ Anyhow] ( https://crates.io/crates/anyhow ) and
444- [ Proptest] ( https://crates.io/crates/proptest ) crates to simplify turning
445- errors from those crates into test failures.
442+ To simplify error management during a test arrangement, [ ` Result<T> ` ]
443+ provides a few conversion utilities.
446444
447- To use this, activate the ` anyhow ` , respectively ` proptest ` feature in
448- GoogleTest Rust and invoke the extension method [ ` into_test_result() ` ] on a
449- ` Result ` value in your test. For example:
445+ If your setup function returns ` std::result::Result<T, E> ` where ` E: std::error::Error ` ,
446+ the ` std::result::Result<T, E> ` can simply be handled with the ` ? ` operator. If an ` Err(e) `
447+ is returned, the test will report a failure at the line where the ` ? ` operator has been
448+ applied (or the lowest caller without ` #[track_caller] ` ).
450449
451450```
452451# use googletest::prelude::*;
453- # #[cfg(feature = "anyhow")]
454- # use anyhow::anyhow;
455- # #[cfg(feature = "anyhow")]
456- # /* The attribute macro would prevent the function from being compiled in a doctest.
457- #[test]
458- # */
459- fn has_anyhow_failure() -> Result<()> {
460- Ok(just_return_error().into_test_result()?)
452+ struct PngImage { h: i32, w: i32 /* ... */ }
453+ impl PngImage {
454+ fn new_from_file(file_name: &str) -> std::result::Result<Self, std::io::Error> {
455+ Err(std::io::Error::new(std::io::ErrorKind::Other, "oh no!"))
456+
457+ }
458+ fn rotate(&mut self) { std::mem::swap(&mut self.h, &mut self.w);}
459+ fn dimensions(&self) -> (i32, i32) { (self.h, self.w)}
461460}
462461
463- # #[cfg(feature = "anyhow")]
464- fn just_return_error() -> anyhow::Result<()> {
465- anyhow::Result::Err(anyhow!("This is an error"))
462+ fn test_png_image_dimensions() -> googletest::Result<()> {
463+ // Arrange
464+ let mut png = PngImage::new_from_file("example.png")?;
465+ verify_eq!(png.dimensions(), (128, 64))?;
466+
467+ // Act
468+ png.rotate();
469+
470+ // Assert
471+ expect_eq!(png.dimensions(), (64, 128));
472+ Ok(())
466473}
467- # #[cfg(feature = "anyhow")]
468- # has_anyhow_failure ().unwrap_err();
474+
475+ # test_png_image_dimensions ().unwrap_err();
469476```
470477
471- One can convert Proptest test failures into GoogleTest test failures when the
472- test is invoked with
473- [ ` TestRunner::run ` ] ( https://docs.rs/proptest/latest/proptest/test_runner/struct.TestRunner.html#method.run ) :
478+ If your setup function returns ` Option<T> ` or ` std::result::Result<T, E> ` where
479+ ` E: !std::error::Error ` , then you can convert these types with ` into_test_result() `
480+ from the ` IntoTestResult ` extension trait.
474481
475482```
476483# use googletest::prelude::*;
477- # #[cfg(feature = "proptest")]
478- # use proptest::test_runner::{Config, TestRunner};
479- # #[cfg(feature = "proptest")]
484+ # struct PngImage;
485+ # static PNG_BINARY: [u8;0] = [];
486+
487+ impl PngImage {
488+ fn new_from_binary(bin: &[u8]) -> std::result::Result<Self, String> {
489+ Err("Parsing failed".into())
490+ }
491+ }
492+
480493# /* The attribute macro would prevent the function from being compiled in a doctest.
481- #[test]
494+ #[googletest:: test]
482495# */
483- fn numbers_are_greater_than_zero() -> Result<()> {
484- let mut runner = TestRunner::new(Config::default());
485- runner.run(&(1..100i32), |v| Ok(verify_that!(v, gt(0))?)).into_test_result()
496+ fn test_png_image_binary() -> googletest::Result<()> {
497+ // Arrange
498+ let png_image = PngImage::new_from_binary(&PNG_BINARY).into_test_result()?;
499+ /* ... */
500+ # Ok(())
501+ }
502+ # test_png_image_binary().unwrap_err();
503+
504+ impl PngImage {
505+ fn new_from_cache(key: u64) -> Option<Self> {
506+ None
507+ }
486508}
487- # #[cfg(feature = "proptest")]
488- # numbers_are_greater_than_zero().unwrap();
509+
510+ # /* The attribute macro would prevent the function from being compiled in a doctest.
511+ #[googletest::test]
512+ # */
513+ fn test_png_from_cache() -> googletest::Result<()> {
514+ // Arrange
515+ let png_image = PngImage::new_from_cache(123).into_test_result()?;
516+ /* ... */
517+ # Ok(())
518+ }
519+ # test_png_from_cache().unwrap_err();
489520```
490521
491- Similarly, when the ` proptest ` feature is enabled, GoogleTest assertion failures
492- can automatically be converted into Proptest
522+
523+ ## Integrations with other crates
524+
525+ GoogleTest Rust includes integrations with the
526+ [ Proptest] ( https://crates.io/crates/proptest ) crates to simplify turning
527+ GoogleTest assertion failures into Proptest
493528[ ` TestCaseError ` ] ( https://docs.rs/proptest/latest/proptest/test_runner/enum.TestCaseError.html )
494- through the ` ? ` operator as the example above shows .
529+ through the ` ? ` operator.
495530
496531[ `and_log_failure()` ] : GoogleTestSupport::and_log_failure
497532[ `into_test_result()` ] : IntoTestResult::into_test_result
0 commit comments