rust-lang / rust-lang/rust-analyzer

Feature request: Assist - Generate test stub

Open
#9,566 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Similar to https://github.com/rust-analyzer/rust-analyzer/issues/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the requested assist with rust-analyzer issue 3639 and the existing “Fill struct fields” autogen mentioned here. Define the supported parameter and return-value placeholders and the desired scope, then verify that the generated test stub follows those decisions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.