|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2023, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +package segment |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | +) |
| 25 | + |
| 26 | +func TestTranslateOptionsValidCases(t *testing.T) { |
| 27 | + // Checking nil options |
| 28 | + translatedOptions, err := TranslateOptions(nil) |
| 29 | + assert.NoError(t, err) |
| 30 | + assert.Len(t, translatedOptions, 0) |
| 31 | + |
| 32 | + // Checking empty options |
| 33 | + options := []string{} |
| 34 | + translatedOptions, err = TranslateOptions(options) |
| 35 | + assert.NoError(t, err) |
| 36 | + assert.Len(t, translatedOptions, 0) |
| 37 | + |
| 38 | + // Checking correct options |
| 39 | + options = []string{"IGNORE_CACHE"} |
| 40 | + translatedOptions, err = TranslateOptions(options) |
| 41 | + assert.NoError(t, err) |
| 42 | + assert.Len(t, translatedOptions, 1) |
| 43 | + assert.Equal(t, IgnoreCache, translatedOptions[0]) |
| 44 | + |
| 45 | + // Checking after appending further options |
| 46 | + options = append(options, "RESET_CACHE") |
| 47 | + translatedOptions, err = TranslateOptions(options) |
| 48 | + assert.NoError(t, err) |
| 49 | + assert.Len(t, translatedOptions, 2) |
| 50 | + assert.Equal(t, ResetCache, translatedOptions[1]) |
| 51 | +} |
| 52 | + |
| 53 | +func TestTranslateOptionsInvalidCases(t *testing.T) { |
| 54 | + // Checking empty value as option |
| 55 | + options := []string{""} |
| 56 | + translatedOptions, err := TranslateOptions(options) |
| 57 | + assert.Error(t, err) |
| 58 | + assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err) |
| 59 | + assert.Len(t, translatedOptions, 0) |
| 60 | + |
| 61 | + // Checking invalid value as option |
| 62 | + options[0] = "INVALID" |
| 63 | + translatedOptions, err = TranslateOptions(options) |
| 64 | + assert.Error(t, err) |
| 65 | + assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err) |
| 66 | + assert.Len(t, translatedOptions, 0) |
| 67 | +} |
0 commit comments