|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestGetSupportedTemplatesForLanguage_Deterministic(t *testing.T) { |
| 10 | + // Run the function multiple times to ensure consistent ordering |
| 11 | + const iterations = 10 |
| 12 | + language := LanguageTypeScript |
| 13 | + |
| 14 | + var firstResult TemplateKeyValues |
| 15 | + for i := 0; i < iterations; i++ { |
| 16 | + result := GetSupportedTemplatesForLanguage(language) |
| 17 | + |
| 18 | + if i == 0 { |
| 19 | + firstResult = result |
| 20 | + } else { |
| 21 | + // Verify that each iteration produces the same order |
| 22 | + assert.Equal(t, len(firstResult), len(result), "All iterations should return the same number of templates") |
| 23 | + for j := range result { |
| 24 | + assert.Equal(t, firstResult[j].Key, result[j].Key, "Template at index %d should be consistent across iterations", j) |
| 25 | + assert.Equal(t, firstResult[j].Value, result[j].Value, "Template value at index %d should be consistent across iterations", j) |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestTemplateKeyValues_GetTemplateDisplayValues(t *testing.T) { |
| 32 | + templates := TemplateKeyValues{ |
| 33 | + {Key: "sample-app", Value: "Sample App - Implements basic Kernel apps"}, |
| 34 | + {Key: "advanced-sample", Value: "Advanced Sample - Implements sample actions with advanced Kernel configs"}, |
| 35 | + } |
| 36 | + |
| 37 | + displayValues := templates.GetTemplateDisplayValues() |
| 38 | + |
| 39 | + assert.Len(t, displayValues, 2) |
| 40 | + assert.Equal(t, "Sample App - Implements basic Kernel apps", displayValues[0]) |
| 41 | + assert.Equal(t, "Advanced Sample - Implements sample actions with advanced Kernel configs", displayValues[1]) |
| 42 | +} |
| 43 | + |
| 44 | +func TestTemplateKeyValues_GetTemplateKeyFromValue(t *testing.T) { |
| 45 | + templates := TemplateKeyValues{ |
| 46 | + {Key: "sample-app", Value: "Sample App - Implements basic Kernel apps"}, |
| 47 | + {Key: "advanced-sample", Value: "Advanced Sample - Implements sample actions with advanced Kernel configs"}, |
| 48 | + } |
| 49 | + |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + selectedValue string |
| 53 | + wantKey string |
| 54 | + wantErr bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "Valid value returns correct key", |
| 58 | + selectedValue: "Sample App - Implements basic Kernel apps", |
| 59 | + wantKey: "sample-app", |
| 60 | + wantErr: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "Invalid value returns error", |
| 64 | + selectedValue: "Non-existent template", |
| 65 | + wantKey: "", |
| 66 | + wantErr: true, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + key, err := templates.GetTemplateKeyFromValue(tt.selectedValue) |
| 73 | + |
| 74 | + if tt.wantErr { |
| 75 | + assert.Error(t, err) |
| 76 | + } else { |
| 77 | + assert.NoError(t, err) |
| 78 | + assert.Equal(t, tt.wantKey, key) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestTemplateKeyValues_ContainsKey(t *testing.T) { |
| 85 | + templates := TemplateKeyValues{ |
| 86 | + {Key: "sample-app", Value: "Sample App - Implements basic Kernel apps"}, |
| 87 | + {Key: "advanced-sample", Value: "Advanced Sample - Implements sample actions with advanced Kernel configs"}, |
| 88 | + } |
| 89 | + |
| 90 | + tests := []struct { |
| 91 | + name string |
| 92 | + key string |
| 93 | + want bool |
| 94 | + }{ |
| 95 | + { |
| 96 | + name: "Existing key returns true", |
| 97 | + key: "sample-app", |
| 98 | + want: true, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "Non-existing key returns false", |
| 102 | + key: "non-existent", |
| 103 | + want: false, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + got := templates.ContainsKey(tt.key) |
| 110 | + assert.Equal(t, tt.want, got) |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments