yutannihilation / savvy

A simple R extension interface using Rust
https://yutannihilation.github.io/savvy/guide/
MIT License
72 stars 4 forks source link

Extract doctest #165

Closed yutannihilation closed 7 months ago

yutannihilation commented 7 months ago

Part of https://github.com/yutannihilation/savvy/issues/20

yutannihilation commented 7 months ago

Original doc test:

/// ```
/// let mut out = savvy::OwnedIntegerSexp::new(3)?;
/// out[1] = 11;
/// assert_eq!(out.as_slice(), &[0, 12, 0]);
/// ```

Generated Rust code:

savvy_source('
use savvy::savvy;

#[savvy]
fn test_1() -> savvy::Result<()> {
    std::panic::set_hook(Box::new(|panic_info| {
        let mut msg: Vec<String> = Vec::new();
        let orig_msg = panic_info.to_string();
        let mut lines = orig_msg.lines();

        lines.next(); // remove location

        for line in lines {
            msg.push(format!("    {}", line));
        }

        savvy::r_eprintln!("CODE:
    let mut out = savvy::OwnedIntegerSexp::new(3)?;
    out[1] = 11;
    assert_eq!(out.as_slice(), &[0, 12, 0]);

ERROR:
{}
", msg.join("\n"));
    }));

    let test = || -> savvy::Result<()> {
            let mut out = savvy::OwnedIntegerSexp::new(3)?;
    out[1] = 11;
    assert_eq!(out.as_slice(), &[0, 12, 0]);
        Ok(())
    };
    let result = std::panic::catch_unwind(||test().expect("some error"));

    match result {
    Ok(_) => Ok(()),
    Err(_) => Err("test failed".into())
    }
}
', use_cache_dir = TRUE)

Result:

test_1()
#> CODE:
#>     let mut out = savvy::OwnedIntegerSexp::new(3)?;
#>     out[1] = 11;
#>     assert_eq!(out.as_slice(), &[0, 12, 0]);
#> 
#> ERROR:
#>     assertion `left == right` failed
#>       left: [0, 11, 0]
#>      right: [0, 12, 0]
#> 
#> Error: test failed
yutannihilation commented 7 months ago

Random thoughts: