klen / nvim-test

A Neovim wrapper for running tests
MIT License
176 stars 26 forks source link

rust tests with `#[test]` #23

Open gbataille opened 1 year ago

gbataille commented 1 year ago

Hi,

I have been trying to get nvim-test to run rust tests that do not have test in the name but that are annotated with #[test]


fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    fn not_a_test() {
        assert_eq!(1, 2);
    }

    #[test]
    fn test_add3() {
        assert_eq!(add(1, 2), 3);
    }

    #[test]
    fn test_add5() {
        assert_eq!(add(3, 2), 5);
    }

    fn foo() {
        println!("{}", 'a' as u32);
    }

    #[test]
    fn test_ascii() {
        println!("{}", 'a' as u32);
        println!("{}", 'A' as u32);
    }
    #[test]
    fn bar() {
        println!("{}", 'a' as u32);
        println!("{}", 'A' as u32);
    }
}

mod foo {
    fn foobar() {
        println!("{}", 'a' as u32);
        println!("{}", 'A' as u32);
    }
}

I'm brand new to treesitter, and lua (and to rust too actually). Took me a while because you need an expression that matches a sibling from the function you are trying to find. I think you can't solve it by just fiddling with the treesitter expression and use the generic find_nearest_test of the Runner but I'm not sure at all.

I have tried a first implementation that works on the above example but I'm not very happy with it. I don't like the booleans that track the state of whether I have already found my targets and such, and I also guess that it might not work with other test patterns.

Questions

(I have now seen that there are specs test, I'll need to try and run them too, I have not done that yet)

gbataille commented 1 year ago

just found another bug with the way the rust tester is implemented https://github.com/klen/nvim-test/issues/28