|
| 1 | +package flags_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "ldcli/internal/flags" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNameToKey(t *testing.T) { |
| 14 | + t.Run("with valid input", func(t *testing.T) { |
| 15 | + tests := map[string]struct { |
| 16 | + name string |
| 17 | + expectedKey string |
| 18 | + }{ |
| 19 | + "converts camel case to kebab case": { |
| 20 | + name: "myFlag", |
| 21 | + expectedKey: "my-flag", |
| 22 | + }, |
| 23 | + "converts multiple uppercase to kebab case": { |
| 24 | + name: "myNewFlag", |
| 25 | + expectedKey: "my-new-flag", |
| 26 | + }, |
| 27 | + "converts leading capital camel case to kebab case": { |
| 28 | + name: "MyFlag", |
| 29 | + expectedKey: "my-flag", |
| 30 | + }, |
| 31 | + "converts multiple consecutive capitals to kebab case": { |
| 32 | + name: "MyFLag", |
| 33 | + expectedKey: "my-f-lag", |
| 34 | + }, |
| 35 | + "converts space with capital to kebab case": { |
| 36 | + name: "My Flag", |
| 37 | + expectedKey: "my-flag", |
| 38 | + }, |
| 39 | + "converts multiple spaces to kebab case": { |
| 40 | + name: "my flag", |
| 41 | + expectedKey: "my-flag", |
| 42 | + }, |
| 43 | + "converts tab to kebab case": { |
| 44 | + name: "my\tflag", |
| 45 | + expectedKey: "my-flag", |
| 46 | + }, |
| 47 | + "does not convert all lowercase": { |
| 48 | + name: "myflag", |
| 49 | + expectedKey: "myflag", |
| 50 | + }, |
| 51 | + "allows leading number": { |
| 52 | + name: "1Flag", |
| 53 | + expectedKey: "1-flag", |
| 54 | + }, |
| 55 | + "allows period": { |
| 56 | + name: "my.Flag", |
| 57 | + expectedKey: "my.-flag", |
| 58 | + }, |
| 59 | + "allows underscore": { |
| 60 | + name: "my_Flag", |
| 61 | + expectedKey: "my_-flag", |
| 62 | + }, |
| 63 | + "allows dash": { |
| 64 | + name: "my-Flag", |
| 65 | + expectedKey: "my-flag", |
| 66 | + }, |
| 67 | + "allows double dash with capital letter": { |
| 68 | + name: "my--Flag", |
| 69 | + expectedKey: "my--flag", |
| 70 | + }, |
| 71 | + "allows double dash": { |
| 72 | + name: "my--flag", |
| 73 | + expectedKey: "my--flag", |
| 74 | + }, |
| 75 | + } |
| 76 | + for name, tt := range tests { |
| 77 | + tt := tt |
| 78 | + t.Run(name, func(t *testing.T) { |
| 79 | + key, err := flags.NewKeyFromName(tt.name) |
| 80 | + |
| 81 | + errMsg := fmt.Sprintf("name: %s", tt.name) |
| 82 | + require.NoError(t, err, errMsg) |
| 83 | + assert.Equal(t, tt.expectedKey, key, errMsg) |
| 84 | + }) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("with invalid input", func(t *testing.T) { |
| 89 | + tests := map[string]struct { |
| 90 | + name string |
| 91 | + expectedKey string |
| 92 | + expectedErr string |
| 93 | + }{ |
| 94 | + "does not allow non-alphanumeric": { |
| 95 | + name: "my-$-flag", |
| 96 | + expectedErr: "Name must start with a letter or number and only contain letters, numbers, '.', '_' or '-'.", |
| 97 | + }, |
| 98 | + "does not allow empty name": { |
| 99 | + name: "", |
| 100 | + expectedErr: "Name must not be empty.", |
| 101 | + }, |
| 102 | + "does not allow name > 50 characters": { |
| 103 | + name: strings.Repeat("*", 51), |
| 104 | + expectedErr: "Name must be less than 50 characters.", |
| 105 | + }, |
| 106 | + } |
| 107 | + for name, tt := range tests { |
| 108 | + tt := tt |
| 109 | + t.Run(name, func(t *testing.T) { |
| 110 | + _, err := flags.NewKeyFromName(tt.name) |
| 111 | + |
| 112 | + assert.EqualError(t, err, tt.expectedErr) |
| 113 | + }) |
| 114 | + } |
| 115 | + }) |
| 116 | +} |
0 commit comments