github.com/facebookincubator/ttpforge@v1.0.13-0.20240405153150-5ae801628835/pkg/args/choices_test.go (about)

     1  /*
     2  Copyright © 2023-present, Meta Platforms, Inc. and affiliates
     3  Permission is hereby granted, free of charge, to any person obtaining a copy
     4  of this software and associated documentation files (the "Software"), to deal
     5  in the Software without restriction, including without limitation the rights
     6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  copies of the Software, and to permit persons to whom the Software is
     8  furnished to do so, subject to the following conditions:
     9  The above copyright notice and this permission notice shall be included in
    10  all copies or substantial portions of the Software.
    11  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    17  THE SOFTWARE.
    18  */
    19  
    20  package args
    21  
    22  import (
    23  	"testing"
    24  )
    25  
    26  func TestValidateArgsChoices(t *testing.T) {
    27  
    28  	testCases := []validateTestCase{
    29  		{
    30  			name: "Choices: Argument Value is a Valid Choice",
    31  			specs: []Spec{
    32  				{
    33  					Name:    "alpha",
    34  					Choices: []string{"foo", "bar"},
    35  				},
    36  			},
    37  			argKvStrs: []string{
    38  				"alpha=bar",
    39  			},
    40  			expectedResult: map[string]any{
    41  				"alpha": "bar",
    42  			},
    43  			wantError: false,
    44  		},
    45  		{
    46  			name: "Choices: Default Value is Not a Valid Choice",
    47  			specs: []Spec{
    48  				{
    49  					Name:    "alpha",
    50  					Choices: []string{"foo", "bar"},
    51  					Default: "baz",
    52  				},
    53  			},
    54  			argKvStrs: []string{
    55  				"alpha=bar",
    56  			},
    57  			wantError: true,
    58  		},
    59  		{
    60  			name: "Choices: Argument Value is Not a Valid Choice",
    61  			specs: []Spec{
    62  				{
    63  					Name:    "alpha",
    64  					Choices: []string{"foo", "bar"},
    65  				},
    66  			},
    67  			argKvStrs: []string{
    68  				"alpha=baz",
    69  			},
    70  			wantError: true,
    71  		},
    72  		{
    73  			name: "Choices: Testing Type Generality (int)",
    74  			specs: []Spec{
    75  				{
    76  					Name:    "alpha",
    77  					Type:    "int",
    78  					Choices: []string{"1", "2"},
    79  				},
    80  			},
    81  			argKvStrs: []string{
    82  				"alpha=1",
    83  			},
    84  			expectedResult: map[string]any{
    85  				"alpha": 1,
    86  			},
    87  			wantError: false,
    88  		},
    89  		{
    90  			name: "Choices: Choice Configuration Contains Value with Incorrect Type",
    91  			specs: []Spec{
    92  				{
    93  					Name:    "alpha",
    94  					Type:    "int",
    95  					Choices: []string{"CECI_NEST_PAS_UNE_INT", "1", "2"},
    96  				},
    97  			},
    98  			argKvStrs: []string{
    99  				"alpha=2",
   100  			},
   101  			wantError: true,
   102  		},
   103  		{
   104  			name: "Choices: Wrong Type in Choice Configuration and Wrong Argument Type",
   105  			specs: []Spec{
   106  				{
   107  					Name:    "alpha",
   108  					Type:    "int",
   109  					Choices: []string{"1", "2", "CECI_NEST_PAS_UNE_INT"},
   110  				},
   111  			},
   112  			argKvStrs: []string{
   113  				"alpha=CECI_NEST_PAS_UNE_INT",
   114  			},
   115  			wantError: true,
   116  		},
   117  	}
   118  
   119  	for _, tc := range testCases {
   120  		t.Run(tc.name, func(t *testing.T) {
   121  			checkValidateTestCase(t, tc)
   122  		})
   123  	}
   124  
   125  }