Skip to content

Commit 4dcb06a

Browse files
committed
Adding basic flag parser test
1 parent d2e8e46 commit 4dcb06a

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

run/flagparser_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package run_test
2+
3+
import (
4+
"os"
5+
"reflect"
6+
"testing"
7+
"time"
8+
9+
"github.com/nextmv-io/sdk/run"
10+
)
11+
12+
type ParallelSolveOptions struct {
13+
Iterations int `json:"iterations" usage:"maximum number of iterations, -1 assumes no limit; iterations are counted after start solutions are generated" default:"-1"`
14+
Duration time.Duration `json:"duration" usage:"maximum duration of the solver" default:"5s"`
15+
ParallelRuns int `json:"parallel_runs" usage:"maximum number of parallel runs, -1 results in using all available resources" default:"-1"`
16+
StartSolutions int `json:"start_solutions" usage:"number of solutions to generate on top of those passed in; one solution generated with sweep algorithm, the rest generated randomly" default:"-1"`
17+
RunDeterministically bool `json:"run_deterministically" usage:"run the parallel solver deterministically"`
18+
}
19+
20+
type SampleOption struct {
21+
Solve ParallelSolveOptions `json:"solve,omitempty"`
22+
Custom struct {
23+
Solve struct {
24+
Plateau struct {
25+
Auto bool `json:"auto" usage:"whether to enable auto plateau detection"`
26+
} `json:"plateau"`
27+
} `json:"solve"`
28+
} `json:"custom,omitempty"`
29+
}
30+
31+
func Test_FlagParser(t *testing.T) {
32+
// Simulate command line arguments
33+
os.Args = []string{
34+
"cmd",
35+
"-solve.iterations=10",
36+
"-custom.solve.plateau.auto=true",
37+
"-solve.duration=5m",
38+
}
39+
defer func() { os.Args = os.Args[:1] }()
40+
41+
_, option, err := run.FlagParser[SampleOption, run.CLIRunnerConfig]()
42+
if err != nil {
43+
t.Fatalf("unexpected error: %v", err)
44+
}
45+
46+
expectedOption := SampleOption{
47+
Solve: ParallelSolveOptions{
48+
Iterations: 10,
49+
Duration: 5 * time.Minute,
50+
ParallelRuns: -1,
51+
StartSolutions: -1,
52+
RunDeterministically: false,
53+
},
54+
Custom: struct {
55+
Solve struct {
56+
Plateau struct {
57+
Auto bool `json:"auto" usage:"whether to enable auto plateau detection"`
58+
} `json:"plateau"`
59+
} `json:"solve"`
60+
}{
61+
Solve: struct {
62+
Plateau struct {
63+
Auto bool `json:"auto" usage:"whether to enable auto plateau detection"`
64+
} `json:"plateau"`
65+
}{
66+
Plateau: struct {
67+
Auto bool `json:"auto" usage:"whether to enable auto plateau detection"`
68+
}{
69+
Auto: true,
70+
},
71+
},
72+
},
73+
}
74+
if !reflect.DeepEqual(option, expectedOption) {
75+
t.Errorf("expected option %+v, got %+v", expectedOption, option)
76+
}
77+
}

0 commit comments

Comments
 (0)