|
| 1 | +package features |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestNewFeatureSet(t *testing.T) { |
| 8 | + fs := NewFeatureSet() |
| 9 | + if fs == nil { |
| 10 | + t.Fatal("Expected NewFeatureSet to return a non-nil pointer") |
| 11 | + } |
| 12 | + if fs.Features == nil { |
| 13 | + t.Fatal("Expected Features map to be initialized") |
| 14 | + } |
| 15 | + if len(fs.Features) != 0 { |
| 16 | + t.Fatalf("Expected Features map to be empty, got %d items", len(fs.Features)) |
| 17 | + } |
| 18 | + if fs.everythingOn { |
| 19 | + t.Fatal("Expected everythingOn to be initialized as false") |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestAddFeature(t *testing.T) { |
| 24 | + fs := NewFeatureSet() |
| 25 | + |
| 26 | + // Test adding a feature |
| 27 | + fs.AddFeature("test-feature", "A test feature", true) |
| 28 | + |
| 29 | + // Verify feature was added correctly |
| 30 | + if len(fs.Features) != 1 { |
| 31 | + t.Errorf("Expected 1 feature, got %d", len(fs.Features)) |
| 32 | + } |
| 33 | + |
| 34 | + feature, exists := fs.Features["test-feature"] |
| 35 | + if !exists { |
| 36 | + t.Fatal("Feature was not added to the map") |
| 37 | + } |
| 38 | + |
| 39 | + if feature.Name != "test-feature" { |
| 40 | + t.Errorf("Expected feature name to be 'test-feature', got '%s'", feature.Name) |
| 41 | + } |
| 42 | + |
| 43 | + if feature.Description != "A test feature" { |
| 44 | + t.Errorf("Expected feature description to be 'A test feature', got '%s'", feature.Description) |
| 45 | + } |
| 46 | + |
| 47 | + if !feature.Enabled { |
| 48 | + t.Error("Expected feature to be enabled") |
| 49 | + } |
| 50 | + |
| 51 | + // Test adding another feature |
| 52 | + fs.AddFeature("another-feature", "Another test feature", false) |
| 53 | + |
| 54 | + if len(fs.Features) != 2 { |
| 55 | + t.Errorf("Expected 2 features, got %d", len(fs.Features)) |
| 56 | + } |
| 57 | + |
| 58 | + // Test overriding existing feature |
| 59 | + fs.AddFeature("test-feature", "Updated description", false) |
| 60 | + |
| 61 | + feature, _ = fs.Features["test-feature"] |
| 62 | + if feature.Description != "Updated description" { |
| 63 | + t.Errorf("Expected feature description to be updated to 'Updated description', got '%s'", feature.Description) |
| 64 | + } |
| 65 | + |
| 66 | + if feature.Enabled { |
| 67 | + t.Error("Expected feature to be disabled after update") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestIsEnabled(t *testing.T) { |
| 72 | + fs := NewFeatureSet() |
| 73 | + |
| 74 | + // Test with non-existent feature |
| 75 | + if fs.IsEnabled("non-existent") { |
| 76 | + t.Error("Expected IsEnabled to return false for non-existent feature") |
| 77 | + } |
| 78 | + |
| 79 | + // Test with disabled feature |
| 80 | + fs.AddFeature("disabled-feature", "A disabled feature", false) |
| 81 | + if fs.IsEnabled("disabled-feature") { |
| 82 | + t.Error("Expected IsEnabled to return false for disabled feature") |
| 83 | + } |
| 84 | + |
| 85 | + // Test with enabled feature |
| 86 | + fs.AddFeature("enabled-feature", "An enabled feature", true) |
| 87 | + if !fs.IsEnabled("enabled-feature") { |
| 88 | + t.Error("Expected IsEnabled to return true for enabled feature") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestEnableFeature(t *testing.T) { |
| 93 | + fs := NewFeatureSet() |
| 94 | + |
| 95 | + // Test enabling non-existent feature |
| 96 | + err := fs.EnableFeature("non-existent") |
| 97 | + if err == nil { |
| 98 | + t.Error("Expected error when enabling non-existent feature") |
| 99 | + } |
| 100 | + |
| 101 | + // Test enabling feature |
| 102 | + fs.AddFeature("test-feature", "A test feature", false) |
| 103 | + |
| 104 | + if fs.IsEnabled("test-feature") { |
| 105 | + t.Error("Expected feature to be disabled initially") |
| 106 | + } |
| 107 | + |
| 108 | + err = fs.EnableFeature("test-feature") |
| 109 | + if err != nil { |
| 110 | + t.Errorf("Expected no error when enabling feature, got: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + if !fs.IsEnabled("test-feature") { |
| 114 | + t.Error("Expected feature to be enabled after EnableFeature call") |
| 115 | + } |
| 116 | + |
| 117 | + // Test enabling already enabled feature |
| 118 | + err = fs.EnableFeature("test-feature") |
| 119 | + if err != nil { |
| 120 | + t.Errorf("Expected no error when enabling already enabled feature, got: %v", err) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestEnableFeatures(t *testing.T) { |
| 125 | + fs := NewFeatureSet() |
| 126 | + |
| 127 | + // Prepare features |
| 128 | + fs.AddFeature("feature1", "Feature 1", false) |
| 129 | + fs.AddFeature("feature2", "Feature 2", false) |
| 130 | + |
| 131 | + // Test enabling multiple features |
| 132 | + err := fs.EnableFeatures([]string{"feature1", "feature2"}) |
| 133 | + if err != nil { |
| 134 | + t.Errorf("Expected no error when enabling features, got: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if !fs.IsEnabled("feature1") { |
| 138 | + t.Error("Expected feature1 to be enabled") |
| 139 | + } |
| 140 | + |
| 141 | + if !fs.IsEnabled("feature2") { |
| 142 | + t.Error("Expected feature2 to be enabled") |
| 143 | + } |
| 144 | + |
| 145 | + // Test with non-existent feature in the list |
| 146 | + err = fs.EnableFeatures([]string{"feature1", "non-existent"}) |
| 147 | + if err == nil { |
| 148 | + t.Error("Expected error when enabling list with non-existent feature") |
| 149 | + } |
| 150 | + |
| 151 | + // Test with empty list |
| 152 | + err = fs.EnableFeatures([]string{}) |
| 153 | + if err != nil { |
| 154 | + t.Errorf("Expected no error with empty feature list, got: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + // Test enabling everything through EnableFeatures |
| 158 | + fs = NewFeatureSet() |
| 159 | + err = fs.EnableFeatures([]string{"everything"}) |
| 160 | + if err != nil { |
| 161 | + t.Errorf("Expected no error when enabling 'everything', got: %v", err) |
| 162 | + } |
| 163 | + |
| 164 | + if !fs.everythingOn { |
| 165 | + t.Error("Expected everythingOn to be true after enabling 'everything' via EnableFeatures") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestEnableEverything(t *testing.T) { |
| 170 | + fs := NewFeatureSet() |
| 171 | + |
| 172 | + // Add a disabled feature |
| 173 | + fs.AddFeature("test-feature", "A test feature", false) |
| 174 | + |
| 175 | + // Verify it's disabled |
| 176 | + if fs.IsEnabled("test-feature") { |
| 177 | + t.Error("Expected feature to be disabled initially") |
| 178 | + } |
| 179 | + |
| 180 | + // Enable "everything" |
| 181 | + err := fs.EnableFeature("everything") |
| 182 | + if err != nil { |
| 183 | + t.Errorf("Expected no error when enabling 'everything', got: %v", err) |
| 184 | + } |
| 185 | + |
| 186 | + // Verify everythingOn was set |
| 187 | + if !fs.everythingOn { |
| 188 | + t.Error("Expected everythingOn to be true after enabling 'everything'") |
| 189 | + } |
| 190 | + |
| 191 | + // Verify the previously disabled feature is now enabled |
| 192 | + if !fs.IsEnabled("test-feature") { |
| 193 | + t.Error("Expected feature to be enabled when everythingOn is true") |
| 194 | + } |
| 195 | + |
| 196 | + // Verify a non-existent feature is also enabled |
| 197 | + if !fs.IsEnabled("non-existent") { |
| 198 | + t.Error("Expected non-existent feature to be enabled when everythingOn is true") |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +func TestIsEnabledWithEverythingOn(t *testing.T) { |
| 203 | + fs := NewFeatureSet() |
| 204 | + |
| 205 | + // Enable "everything" |
| 206 | + err := fs.EnableFeature("everything") |
| 207 | + if err != nil { |
| 208 | + t.Errorf("Expected no error when enabling 'everything', got: %v", err) |
| 209 | + } |
| 210 | + |
| 211 | + // Test that any feature name returns true with IsEnabled |
| 212 | + if !fs.IsEnabled("some-feature") { |
| 213 | + t.Error("Expected IsEnabled to return true for any feature when everythingOn is true") |
| 214 | + } |
| 215 | + |
| 216 | + if !fs.IsEnabled("another-feature") { |
| 217 | + t.Error("Expected IsEnabled to return true for any feature when everythingOn is true") |
| 218 | + } |
| 219 | +} |
0 commit comments