...

Source file src/sigs.k8s.io/cli-utils/pkg/object/field_test.go

Documentation: sigs.k8s.io/cli-utils/pkg/object

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package object
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestFieldPath(t *testing.T) {
    13  	tests := map[string]struct {
    14  		fieldPath []interface{}
    15  		expected  string
    16  	}{
    17  		"empty path": {
    18  			fieldPath: []interface{}{},
    19  			expected:  "",
    20  		},
    21  		"kind": {
    22  			fieldPath: []interface{}{"kind"},
    23  			expected:  ".kind",
    24  		},
    25  		"metadata.name": {
    26  			fieldPath: []interface{}{"metadata", "name"},
    27  			expected:  ".metadata.name",
    28  		},
    29  		"spec.versions[1].name": {
    30  			fieldPath: []interface{}{"spec", "versions", 1, "name"},
    31  			expected:  ".spec.versions[1].name",
    32  		},
    33  		"numeric": {
    34  			fieldPath: []interface{}{"spec", "123"},
    35  			expected:  `.spec["123"]`,
    36  		},
    37  		"alphanumeric, ends with number": {
    38  			fieldPath: []interface{}{"spec", "abc123"},
    39  			expected:  `.spec.abc123`,
    40  		},
    41  		"alphanumeric, ends with hyphen": {
    42  			fieldPath: []interface{}{"spec", "abc123-"},
    43  			expected:  `.spec["abc123-"]`,
    44  		},
    45  		"alphanumeric, ends with underscore": {
    46  			fieldPath: []interface{}{"spec", "abc123_"},
    47  			expected:  `.spec["abc123_"]`,
    48  		},
    49  		"alphanumeric, starts with hyphen": {
    50  			fieldPath: []interface{}{"spec", "-abc123"},
    51  			expected:  `.spec["-abc123"]`,
    52  		},
    53  		"alphanumeric, starts with underscore": {
    54  			fieldPath: []interface{}{"spec", "_abc123"},
    55  			expected:  `.spec["_abc123"]`,
    56  		},
    57  		"alphanumeric, starts with number": {
    58  			fieldPath: []interface{}{"spec", "_abc123"},
    59  			expected:  `.spec["_abc123"]`,
    60  		},
    61  		"alphanumeric, intrnal hyphen": {
    62  			fieldPath: []interface{}{"spec", "abc-123"},
    63  			expected:  `.spec.abc-123`,
    64  		},
    65  		"alphanumeric, intrnal underscore": {
    66  			fieldPath: []interface{}{"spec", "abc_123"},
    67  			expected:  `.spec.abc_123`,
    68  		},
    69  		"space": {
    70  			fieldPath: []interface{}{"spec", "abc 123"},
    71  			expected:  `.spec["abc 123"]`,
    72  		},
    73  		"tab": {
    74  			fieldPath: []interface{}{"spec", "abc\t123"},
    75  			expected:  `.spec["abc\t123"]`,
    76  		},
    77  		"linebreak": {
    78  			fieldPath: []interface{}{"spec", "abc\n123"},
    79  			expected:  `.spec["abc\n123"]`,
    80  		},
    81  		// result from invalid input doesn't matter, as long as it doesn't panic
    82  		"invalid type: float": {
    83  			fieldPath: []interface{}{"spec", float64(-1.0)},
    84  			expected:  `.spec[-1]`,
    85  		},
    86  		"invalid type: struct": {
    87  			fieldPath: []interface{}{"spec", struct{ Field string }{Field: "value"}},
    88  			expected:  `.spec[struct { Field string }{Field:"value"}]`,
    89  		},
    90  	}
    91  
    92  	for name, tc := range tests {
    93  		t.Run(name, func(t *testing.T) {
    94  			result := FieldPath(tc.fieldPath)
    95  			assert.Equal(t, tc.expected, result)
    96  		})
    97  	}
    98  }
    99  

View as plain text