sigs.k8s.io/cluster-api@v1.7.1/internal/controllers/topology/cluster/patches/variables/value_test.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 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 variables 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 "k8s.io/utils/ptr" 24 ) 25 26 func TestParsePathSegment(t *testing.T) { 27 tests := []struct { 28 name string 29 segment string 30 wantPathSegment *pathSegment 31 wantErr bool 32 }{ 33 { 34 name: "parse basic segment", 35 segment: "propertyName", 36 wantPathSegment: &pathSegment{ 37 path: "propertyName", 38 index: nil, 39 }, 40 }, 41 { 42 name: "parse segment with index", 43 segment: "arrayProperty[5]", 44 wantPathSegment: &pathSegment{ 45 path: "arrayProperty", 46 index: ptr.To(5), 47 }, 48 }, 49 { 50 name: "fail invalid syntax: only left delimiter", 51 segment: "arrayProperty[", 52 wantErr: true, 53 }, 54 { 55 name: "fail invalid syntax: only right delimiter", 56 segment: "arrayProperty]", 57 wantErr: true, 58 }, 59 { 60 name: "fail invalid syntax: both delimiter but no index", 61 segment: "arrayProperty[]", 62 wantErr: true, 63 }, 64 { 65 name: "fail invalid syntax: negative index", 66 segment: "arrayProperty[-1]", 67 wantErr: true, 68 }, 69 } 70 for _, tt := range tests { 71 t.Run(tt.name, func(t *testing.T) { 72 g := NewWithT(t) 73 74 got, err := parsePathSegment(tt.segment) 75 if tt.wantErr { 76 g.Expect(err).To(HaveOccurred()) 77 return 78 } 79 g.Expect(err).ToNot(HaveOccurred()) 80 81 g.Expect(got).To(Equal(tt.wantPathSegment)) 82 }) 83 } 84 }