Skip to content

Feature request: Assist - Generate test stub #9566

Open
@Walther

Description

@Walther

Similar to #3639

Main idea, briefly:

After writing a function declaration

pub fn weird_algorithm(start: usize) -> String {
  todo!()
}

It could be nice to have an assist for generating a test stub, for example

#[test]
fn test_case_1() {
    let result = weird_algorithm(start);
    let expected = "";
    assert_eq!(result, expected);
}

It would then be easy for the user to fill in the desired parameter and result values.

This could be especially helpful for functions with more parameters, and possibly complex structs as input. Imagine e.g.

pub struct Ray {
    pub origin: Vec3,
    pub direction: Vec3,
    pub time: Float,
}

impl Ray {
// ...
    pub fn point_at_parameter(&self, t: Float) -> Vec3 {
        self.origin + t * self.direction
    }
// ...
}

and autogenerating e.g. something along the lines of

#[test]
fn test_case_1() {
    let origin = Vec3::new();
    let direction = Vec3::new();
    let time = 0.0;
    let ray = Ray {
        origin,
        direction,
        time,
    };
    let t = 0.0;
    let result = ray.point_at_parameter(t);
    let expected = Vec3::new();
    assert_eq!(result, expected);
}

Of course there would be a lot of details to figure out here - how much would we want to unroll parameters into new variables on separate lines vs. keeping them inline, would we want the autogenerated code to prefill values like 0.0 above, do we want to assume initializers (like the Vec3::new() above) and prefill those for the more complex structs, or do we want to just use () like e.g. the current "Fill struct fields" autogen does as the simplest possible prefill, etc. Please don't judge these code examples as exact suggestions - feel free to figure out improved ideas.

On a conceptual level, I feel like the autogenerated code doesn't have to be perfect. Writing the initial case for the first time can be a bit annoying / tedious, but for the next test cases it's fairly easy to copy-paste the entire test and modify some values. Getting a placeholder generated with somewhat correct parameters prefilled would be a nice quality-of-life improvement. And of course making it as useful as possible could be a nice stretch goal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-assistsS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions