| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "slices" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/spf13/cobra" |
| 11 | "gotest.tools/v3/assert" |
| 12 | ) |
| 13 | |
| 14 | func TestSetFlagCompletion(t *testing.T) { |
| 15 | tests := []struct { |
| 16 | name string |
| 17 | toComplete string |
| 18 | wantMatch []string |
| 19 | wantNil bool |
| 20 | }{ |
| 21 | { |
| 22 | name: "Empty input returns properties", |
| 23 | toComplete: "", |
| 24 | wantMatch: []string{".vmType=", ".arch="}, |
| 25 | }, |
| 26 | { |
| 27 | name: "Partial property filter", |
| 28 | toComplete: ".vm", |
| 29 | wantMatch: []string{".vmType=", ".vmOpts="}, |
| 30 | }, |
| 31 | { |
| 32 | name: "Exact property with equals returns all enum values", |
| 33 | toComplete: ".vmType=", |
| 34 | wantMatch: []string{".vmType=qemu", ".vmType=vz", ".vmType=wsl2"}, |
| 35 | }, |
| 36 | { |
| 37 | name: "Nested property filter", |
| 38 | toComplete: ".ssh.", |
| 39 | wantMatch: []string{".ssh.localPort=", ".ssh.forwardAgent="}, |
| 40 | }, |
| 41 | { |
| 42 | name: "Partial value filter", |
| 43 | toComplete: ".vmType=q", |
| 44 | wantMatch: []string{".vmType=qemu"}, |
| 45 | }, |
| 46 | { |
| 47 | name: "Unknown property after equals returns nil", |
| 48 | toComplete: ".unknown=", |
| 49 | wantNil: true, |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | for _, tt := range tests { |
| 54 | t.Run(tt.name, func(t *testing.T) { |
| 55 | got, _ := setFlagCompletion(&cobra.Command{}, nil, tt.toComplete) |
| 56 | |
| 57 | if tt.wantNil { |
| 58 | assert.Assert(t, got == nil, "expected nil completions") |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | for _, want := range tt.wantMatch { |
| 63 | assert.Assert(t, slices.Contains(got, want), "expected to find %q in completions, but didn't. Got: %v", want, got) |
| 64 | } |
| 65 | }) |
| 66 | } |
| 67 | } |