|
| 1 | +package subenum_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/TMHSDigital/subenum/pkg/subenum" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPermutations(t *testing.T) { |
| 11 | + got := subenum.Permutations([]string{"API.us.", "", "api.us"}, []string{"dev", "api"}) |
| 12 | + want := []string{"api-dev.us", "api1.us", "api2.us", "api3.us", "dev-api.us", "dev.api.us"} |
| 13 | + if !slices.Equal(got, want) { |
| 14 | + t.Fatalf("Permutations = %q, want %q", got, want) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestPermutationsNilWordsUsesBuiltinList(t *testing.T) { |
| 19 | + got := subenum.Permutations([]string{"api"}, nil) |
| 20 | + for _, name := range []string{"api-staging", "prod-api", "dev.api", "api-v2", "api2"} { |
| 21 | + if !slices.Contains(got, name) { |
| 22 | + t.Errorf("Permutations(nil words) is missing %q", name) |
| 23 | + } |
| 24 | + } |
| 25 | + if !slices.IsSorted(got) || len(slices.Compact(slices.Clone(got))) != len(got) { |
| 26 | + t.Errorf("Permutations(nil words) is not sorted and duplicate-free: %q", got) |
| 27 | + } |
| 28 | + if empty := subenum.Permutations([]string{"api"}, []string{}); len(empty) == 0 || slices.Contains(empty, "api-dev") { |
| 29 | + t.Errorf("Permutations(empty words) = %q, want only numbered variants", empty) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestPermutationsDropsInvalidLabels(t *testing.T) { |
| 34 | + // "-x" makes "-x.api", whose first label starts with a hyphen. |
| 35 | + if got := subenum.Permutations([]string{"api"}, []string{"-x"}); slices.Contains(got, "-x.api") { |
| 36 | + t.Errorf("Permutations kept the invalid name -x.api: %q", got) |
| 37 | + } |
| 38 | + if got := subenum.Permutations(nil, nil); len(got) != 0 { |
| 39 | + t.Errorf("Permutations(nil, nil) = %q, want none", got) |
| 40 | + } |
| 41 | +} |
0 commit comments