Skip to content

Commit d472286

Browse files
committed
Improve gen.go
1 parent 713a26a commit d472286

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

test/gen/gen.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
">=",
2929
"<=",
3030
"..",
31+
"??",
3132
"+",
3233
"-",
3334
"*",
@@ -218,7 +219,12 @@ func memberNode(depth int) string {
218219
}
219220

220221
func unaryNode(depth int) string {
221-
return random([]string{"-", "!", "not"})
222+
op := random([]string{"-", "!", "not"})
223+
// Use a simple formatting to ensure valid unary expression syntax
224+
if op == "not" {
225+
return fmt.Sprintf("not %v", node(depth-1))
226+
}
227+
return fmt.Sprintf("%s%v", op, node(depth-1))
222228
}
223229

224230
func binaryNode(depth int) string {
@@ -364,9 +370,31 @@ func sequenceNode(depth int) string {
364370
}
365371

366372
func variableNode(depth int) string {
367-
e := node(depth - 1)
368-
if !strings.Contains(e, "foobar") {
369-
return "~!@"
373+
// Generate 1-3 variable declarations with diverse names and then make sure
374+
// at least one of them is used in the final expression.
375+
namesPool := []string{"x", "y", "z", "foo", "bar", "foobar", "tmp"}
376+
nDecls := oneOf(list[int]{
377+
{1, 60},
378+
{2, 30},
379+
{3, 10},
380+
})
381+
382+
var decls []string
383+
var chosen []string
384+
for i := 0; i < nDecls; i++ {
385+
name := random(namesPool)
386+
chosen = append(chosen, name)
387+
decls = append(decls, fmt.Sprintf("let %s = %v", name, node(depth-1)))
370388
}
371-
return fmt.Sprintf("let foobar = %v; %v", node(depth-1), e)
389+
390+
// Build a usage expression that references declared vars to guarantee coverage.
391+
use := oneOf(list[string]{
392+
{chosen[0], 50},
393+
{fmt.Sprintf("%s + %v", chosen[0], node(depth-1)), 30},
394+
{fmt.Sprintf("%v + %s", node(depth-1), chosen[0]), 30},
395+
{fmt.Sprintf("if %v { %s } else { %v }", node(depth-1), chosen[0], node(depth-1)), 20},
396+
{fmt.Sprintf("%s ? %v : %v", chosen[0], node(depth-1), node(depth-1)), 10},
397+
})
398+
399+
return fmt.Sprintf("%s; %s", strings.Join(decls, "; "), use)
372400
}

0 commit comments

Comments
 (0)