github.com/argoproj/argo-cd/v3@v3.2.1/pkg/apis/application/v1alpha1/values_test.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestValues_SetString(t *testing.T) {
    11  	testCases := []struct {
    12  		name        string
    13  		inputValue  string
    14  		expectError bool
    15  		expectValue string
    16  	}{
    17  		{
    18  			name:        "an empty string should not throw an error",
    19  			inputValue:  `""`,
    20  			expectValue: "\"\"",
    21  		},
    22  		{
    23  			name:        "a string with contents should not throw an error",
    24  			inputValue:  `"hello"`,
    25  			expectValue: "hello",
    26  		},
    27  		{
    28  			name:        "an array should throw an error",
    29  			inputValue:  "[]",
    30  			expectError: true,
    31  		},
    32  		{
    33  			name:        "a number should throw an error",
    34  			inputValue:  "42",
    35  			expectError: true,
    36  		},
    37  		{
    38  			name:        "a boolean should throw an error",
    39  			inputValue:  "false",
    40  			expectError: true,
    41  		},
    42  		{
    43  			name:        "null should throw an error",
    44  			inputValue:  "null",
    45  			expectError: true,
    46  		},
    47  		{
    48  			name:        "an empty object should not throw an error",
    49  			inputValue:  "{}",
    50  			expectValue: "{}",
    51  		},
    52  		{
    53  			name:        "an object with contents should not throw an error",
    54  			inputValue:  `{"some": "inputValue"}`,
    55  			expectValue: "some: inputValue",
    56  		},
    57  		{
    58  			name:        "a complex object should not throw an error",
    59  			inputValue:  `{"a": {"nested": "object"}, "an": ["array"], "bool": true, "number": 1, "some": "string"}`,
    60  			expectValue: "a:\n  nested: object\nan:\n- array\nbool: true\nnumber: 1\nsome: string",
    61  		},
    62  	}
    63  
    64  	for _, testCase := range testCases {
    65  		var err error
    66  		t.Run(testCase.name, func(t *testing.T) {
    67  			source := &ApplicationSourceHelm{}
    68  			err = source.SetValuesString(testCase.inputValue)
    69  
    70  			if !testCase.expectError {
    71  				assert.Equal(t, testCase.expectValue, source.ValuesString())
    72  				data, err := source.ValuesObject.MarshalJSON()
    73  				require.NoError(t, err)
    74  				err = source.ValuesObject.UnmarshalJSON(data)
    75  				require.NoError(t, err)
    76  				assert.Equal(t, testCase.expectValue, source.ValuesString())
    77  			} else {
    78  				require.Error(t, err)
    79  			}
    80  		})
    81  	}
    82  }