Skip to content

fix(ai): handle custom LLM models that emit test-case declarations#201

Open
DavidHoenisch wants to merge 1 commit intocweill:developfrom
DavidHoenisch:develop
Open

fix(ai): handle custom LLM models that emit test-case declarations#201
DavidHoenisch wants to merge 1 commit intocweill:developfrom
DavidHoenisch:develop

Conversation

@DavidHoenisch
Copy link
Copy Markdown

When using an override Ollama model (e.g. llama3.2:latest) the LLM often outputs the generated test cases as a Go variable declaration instead of a bare array literal or a complete test function:

tests := []struct {
    name string
    args args
    want int
}{
    {name: "case 1", args: args{...}, want: 42},
}

Two parser bugs caused these responses to be rejected:

  1. parseGoTestCases() only recognised bare composite literals or complete func Test... blocks. The tests := form fell through to parseTestCaseArray, which wrapped it in another literal producing invalid Go.

  2. ensureTrailingComma() saw the declaration ended with '}' and was not a func Test, so it appended a comma: "tests := []struct{}{...}," which is invalid Go syntax.

Both issues caused GenerateTestCases() to return an error. The error output (which contained the LLM's generated cases) was printed to stderr/stdout, while the actual test file only received the TODO fallback comment.

Fix:

  • Detect tests := / tests= declarations in parseGoTestCases and wrap them in a func init() so the existing AST parser can extract cases.
  • Teach ensureTrailingComma to skip tests := and var tests declarations.

Verified with llama3.2:latest and qwen2.5-coder:0.5b.

When using a custom Ollama model (e.g. llama3.2:latest) the LLM often
outputs the generated test cases as a Go variable declaration instead
of a bare array literal or a complete test function:

    tests := []struct {
        name string
        args args
        want int
    }{
        {name: "case 1", args: args{...}, want: 42},
    }

Two parser bugs caused these responses to be rejected:

1. parseGoTestCases() only recognised bare composite literals or
   complete func Test... blocks. The tests := form fell through to
   parseTestCaseArray, which wrapped it in another literal producing
   invalid Go.

2. ensureTrailingComma() saw the declaration ended with '}' and was
   not a func Test, so it appended a comma: "tests := []struct{}{...},"
   which is invalid Go syntax.

Both issues caused GenerateTestCases() to return an error. The error
output (which contained the LLM's generated cases) was printed to
stderr/stdout, while the actual test file only received the TODO
fallback comment.

Fix:
- Detect tests := / tests= declarations in parseGoTestCases and wrap
  them in a func init() so the existing AST parser can extract cases.
- Teach ensureTrailingComma to skip tests := and var tests declarations.

Verified with llama3.2:latest and qwen2.5-coder:0.5b.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant