|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2021, 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 decide |
| 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{"DISABLE_DECISION_EVENT", "ENABLED_FLAGS_ONLY"} |
| 40 | + translatedOptions, err = TranslateOptions(options) |
| 41 | + assert.NoError(t, err) |
| 42 | + assert.Len(t, translatedOptions, 2) |
| 43 | + assert.Equal(t, DisableDecisionEvent, translatedOptions[0]) |
| 44 | + assert.Equal(t, EnabledFlagsOnly, translatedOptions[1]) |
| 45 | + |
| 46 | + // Checking after appending further options |
| 47 | + options = append(options, "IGNORE_USER_PROFILE_SERVICE", "EXCLUDE_VARIABLES", "INCLUDE_REASONS") |
| 48 | + translatedOptions, err = TranslateOptions(options) |
| 49 | + assert.NoError(t, err) |
| 50 | + assert.Len(t, translatedOptions, 5) |
| 51 | + assert.Equal(t, IgnoreUserProfileService, translatedOptions[2]) |
| 52 | + assert.Equal(t, ExcludeVariables, translatedOptions[3]) |
| 53 | + assert.Equal(t, IncludeReasons, translatedOptions[4]) |
| 54 | +} |
| 55 | + |
| 56 | +func TestTranslateOptionsInvalidCases(t *testing.T) { |
| 57 | + // Checking empty value as option |
| 58 | + options := []string{""} |
| 59 | + translatedOptions, err := TranslateOptions(options) |
| 60 | + assert.Error(t, err) |
| 61 | + assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err) |
| 62 | + assert.Len(t, translatedOptions, 0) |
| 63 | + |
| 64 | + // Checking invalid value as option |
| 65 | + options[0] = "INVALID" |
| 66 | + translatedOptions, err = TranslateOptions(options) |
| 67 | + assert.Error(t, err) |
| 68 | + assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err) |
| 69 | + assert.Len(t, translatedOptions, 0) |
| 70 | +} |
0 commit comments