sigs.k8s.io/cluster-api@v1.7.1/internal/contract/types_test.go (about)

     1  /*
     2  Copyright 2022 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 contract
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  func TestPath_Append(t *testing.T) {
    26  	g := NewWithT(t)
    27  
    28  	got0 := Path{}.Append("foo")
    29  	g.Expect(got0).To(Equal(Path{"foo"}))
    30  	g.Expect(got0.String()).To(Equal("foo"))
    31  
    32  	got1 := Path{"foo"}.Append("bar")
    33  	g.Expect(got1).To(Equal(Path{"foo", "bar"}))
    34  	g.Expect(got1.String()).To(Equal("foo.bar"))
    35  }
    36  
    37  func TestPath_IsParenOf(t *testing.T) {
    38  	tests := []struct {
    39  		name  string
    40  		p     Path
    41  		other Path
    42  		want  bool
    43  	}{
    44  		{
    45  			name:  "True for parent path",
    46  			p:     Path{"foo"},
    47  			other: Path{"foo", "bar"},
    48  			want:  true,
    49  		},
    50  		{
    51  			name:  "False for same path",
    52  			p:     Path{"foo"},
    53  			other: Path{"foo"},
    54  			want:  false,
    55  		},
    56  		{
    57  			name:  "False for child path",
    58  			p:     Path{"foo", "bar"},
    59  			other: Path{"foo"},
    60  			want:  false,
    61  		},
    62  		{
    63  			name:  "False for not overlapping path",
    64  			p:     Path{"foo", "bar"},
    65  			other: Path{"baz"},
    66  			want:  false,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			g := NewWithT(t)
    72  
    73  			got := tt.p.IsParentOf(tt.other)
    74  			g.Expect(got).To(Equal(tt.want))
    75  		})
    76  	}
    77  }
    78  
    79  func TestPath_Equal(t *testing.T) {
    80  	tests := []struct {
    81  		name  string
    82  		p     Path
    83  		other Path
    84  		want  bool
    85  	}{
    86  		{
    87  			name:  "False for parent path",
    88  			p:     Path{"foo"},
    89  			other: Path{"foo", "bar"},
    90  			want:  false,
    91  		},
    92  		{
    93  			name:  "True for same path",
    94  			p:     Path{"foo"},
    95  			other: Path{"foo"},
    96  			want:  true,
    97  		},
    98  		{
    99  			name:  "False for child path",
   100  			p:     Path{"foo", "bar"},
   101  			other: Path{"foo"},
   102  			want:  false,
   103  		},
   104  		{
   105  			name:  "False for not overlapping path",
   106  			p:     Path{"foo", "bar"},
   107  			other: Path{"baz"},
   108  			want:  false,
   109  		},
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			g := NewWithT(t)
   114  
   115  			got := tt.p.Equal(tt.other)
   116  			g.Expect(got).To(Equal(tt.want))
   117  		})
   118  	}
   119  }
   120  
   121  func TestPath_Overlaps(t *testing.T) {
   122  	tests := []struct {
   123  		name  string
   124  		p     Path
   125  		other Path
   126  		want  bool
   127  	}{
   128  		{
   129  			name:  "True for parent path",
   130  			p:     Path{"foo"},
   131  			other: Path{"foo", "bar"},
   132  			want:  true,
   133  		},
   134  		{
   135  			name:  "True for same path",
   136  			p:     Path{"foo"},
   137  			other: Path{"foo"},
   138  			want:  true,
   139  		},
   140  		{
   141  			name:  "True for child path",
   142  			p:     Path{"foo", "bar"},
   143  			other: Path{"foo"},
   144  			want:  true,
   145  		},
   146  		{
   147  			name:  "False for not overlapping path",
   148  			p:     Path{"foo", "bar"},
   149  			other: Path{"baz"},
   150  			want:  false,
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			g := NewWithT(t)
   156  
   157  			got := tt.p.Overlaps(tt.other)
   158  			g.Expect(got).To(Equal(tt.want))
   159  		})
   160  	}
   161  }