use std ::fs;
use std ::path ::Path;
#[ test ]
fn test_trivial_example_demonstrates_error_handling()
{
let example_path = Path ::new( "examples/error_tools_trivial.rs" );
assert!( example_path.exists(), "error_tools_trivial.rs must exist" );
let source = fs ::read_to_string( example_path )
.expect( "Failed to read error_tools_trivial.rs" );
assert!(
source.contains( "Result<" ) || source.contains( "Result <" ),
"Trivial example must demonstrate Result<T, E> return type. \
Current implementation shows no error handling."
);
let demonstrates_error_creation = source.contains( "format_err!" )
|| source.contains( "anyhow!" )
|| source.contains( "Error" )
|| source.contains( "Err(" );
assert!(
demonstrates_error_creation,
"Trivial example must demonstrate error creation or propagation. \
'Hello, world!' does not demonstrate error handling."
);
let demonstrates_error_handling = source.contains( '?' )
|| source.contains( "match" )
|| source.contains( "unwrap" )
|| source.contains( "expect" );
assert!(
demonstrates_error_handling,
"Trivial example must show basic error handling pattern. \
Current implementation is just a greeting function."
);
}
#[ test ]
fn test_trivial_example_compiles_and_runs()
{
let output = std ::process ::Command ::new( "cargo" )
.args( [ "build", "--example", "error_tools_trivial" ] )
.current_dir( env!( "CARGO_MANIFEST_DIR" ) )
.output()
.expect( "Failed to compile example" );
assert!(
output.status.success(),
"error_tools_trivial.rs must compile successfully. Error: {}",
String ::from_utf8_lossy( &output.stderr )
);
let output = std ::process ::Command ::new( "cargo" )
.args( [ "run", "--example", "error_tools_trivial" ] )
.current_dir( env!( "CARGO_MANIFEST_DIR" ) )
.output()
.expect( "Failed to run example" );
assert!(
output.status.success(),
"error_tools_trivial.rs must run successfully. Error: {}",
String ::from_utf8_lossy( &output.stderr )
);
}