use std::env; use std::path::PathBuf; use std::process::Command; fn run_c_example(name: &'static str, expected_out: &[u8]) { let cargo = env::var("MAKE").unwrap_or("make".to_string()); let pkg_dir = env!("CARGO_MANIFEST_DIR"); let examples_dir = PathBuf::from(pkg_dir).join("examples"); let make_arg = format!("run-{}-c", name); let output = Command::new(cargo) .current_dir(examples_dir) .args(&["-s", &make_arg]) .output() .expect("success"); assert!( output.status.success(), "failed to execute the C example '{}': {}", name, String::from_utf8_lossy(&output.stderr), ); assert_eq!( output.stdout.as_slice(), expected_out, "unexpected stdout from example: {}", String::from_utf8_lossy(&output.stdout), ); } #[test] fn test_run_hello_example() { run_c_example( "hello", br#"==== C hello ==== Initializing... Loading binary... Compiling module... Creating callback... Instantiating module... Extracting export... Calling export... Calling back... > Hello World! Shutting down... Done. ==== Done ==== "#, ); } #[test] fn test_run_memory_example() { run_c_example( "memory", br#"==== C memory ==== Initializing... Loading binary... Compiling module... Instantiating module... Extracting exports... Checking memory... Mutating memory... Growing memory... Creating stand-alone memory... Shutting down... Done. ==== Done ==== "#, ); } #[test] fn test_run_global_example() { run_c_example( "global", br#"==== C global ==== Initializing... Loading binary... Compiling module... Creating globals... Instantiating module... Extracting exports... Accessing globals... Shutting down... Done. ==== Done ==== "#, ); } #[test] fn test_run_callback_example() { run_c_example( "callback", br#"==== C callback ==== Initializing... Loading binary... Compiling module... Creating callback... Instantiating module... Extracting export... Calling export... Calling back... > 7 Calling back closure... > 42 Printing result... > 49 Shutting down... Done. ==== Done ==== "#, ); } #[test] fn test_run_reflect_example() { run_c_example( "reflect", br#"==== C reflect ==== Initializing... Loading binary... Compiling module... Instantiating module... Extracting export... > export 0 "func" >> initial: func i32 f64 f32 -> i32 >> current: func i32 f64 f32 -> i32 >> in-arity: 3, out-arity: 1 > export 1 "global" >> initial: global const f64 >> current: global const f64 > export 2 "table" >> initial: table 0d 50d funcref >> current: table 0d 50d funcref > export 3 "memory" >> initial: memory 1d >> current: memory 1d Shutting down... Done. ==== Done ==== "#, ); } #[test] fn test_run_start_example() { run_c_example( "start", br#"==== C start ==== Initializing... Loading binary... Compiling module... Instantiating module... Printing message... > wasm trap: unreachable, source location: @002e Printing origin... > Empty origin. Printing trace... > Empty trace. Shutting down... Done. ==== Done ==== "#, ); } #[test] fn test_run_trap_example() { run_c_example( "trap", br#"==== C trap ==== Initializing... Loading binary... Compiling module... Creating callback... Instantiating module... Extracting exports... Calling export 0... Calling back... Printing message... > callback abort Printing origin... > Empty origin. Printing trace... > Empty trace. Calling export 1... Printing message... > wasm trap: unreachable, source location: @0065 Printing origin... > Empty origin. Printing trace... > Empty trace. Shutting down... Done. ==== Done ==== "#, ); }