knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmap/map_test.go (about)

     1  /*
     2  Copyright 2021 The Knative 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 kmap
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  )
    24  
    25  func TestUnion(t *testing.T) {
    26  	tests := []struct {
    27  		name string
    28  		in   []map[string]string
    29  		want map[string]string
    30  	}{{
    31  		name: "nothing at all",
    32  		want: map[string]string{},
    33  	}, {
    34  		name: "empty in",
    35  		in:   []map[string]string{},
    36  		want: map[string]string{},
    37  	}, {
    38  		name: "nil in list",
    39  		in:   []map[string]string{nil},
    40  		want: map[string]string{},
    41  	}, {
    42  		name: "empty in recursive",
    43  		in:   []map[string]string{{}},
    44  		want: map[string]string{},
    45  	}, {
    46  		name: "nil and something",
    47  		in:   []map[string]string{nil, {"wish": "you", "were": "here"}},
    48  		want: map[string]string{"wish": "you", "were": "here"},
    49  	}, {
    50  		name: "something and nil",
    51  		in:   []map[string]string{{"the-dark": "side"}},
    52  		want: map[string]string{"the-dark": "side"},
    53  	}, {
    54  		name: "2 in",
    55  		in:   []map[string]string{{"another": "brick"}, {"in": "the-wall"}},
    56  		want: map[string]string{"in": "the-wall", "another": "brick"},
    57  	}, {
    58  		name: "2 in, latter wins",
    59  		in:   []map[string]string{{"another": "brick", "in": "the-wall-pt-I"}, {"in": "the-wall-pt-II"}},
    60  		want: map[string]string{"in": "the-wall-pt-II", "another": "brick"},
    61  	}, {
    62  		name: "3 in, latter wins",
    63  		in: []map[string]string{
    64  			{"another": "brick", "in": "the-wall-pt-I"},
    65  			{"in": "the-wall-pt-II"},
    66  			{"in": "the-wall-pt-III", "hey": "you"},
    67  		},
    68  		want: map[string]string{"in": "the-wall-pt-III", "another": "brick", "hey": "you"},
    69  	}}
    70  
    71  	for _, test := range tests {
    72  		t.Run(test.name, func(t *testing.T) {
    73  			got := Union(test.in...)
    74  			if diff := cmp.Diff(test.want, got); diff != "" {
    75  				t.Error("Union (-want, +got) =", diff)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  func TestFilter(t *testing.T) {
    82  	tests := []struct {
    83  		name   string
    84  		in     map[string]string
    85  		filter func(string) bool
    86  		want   map[string]string
    87  	}{{
    88  		name: "nil in",
    89  		want: map[string]string{},
    90  	}, {
    91  		name: "empty in",
    92  		in:   map[string]string{},
    93  		want: map[string]string{},
    94  	}, {
    95  		name:   "no in, with filter",
    96  		in:     map[string]string{},
    97  		filter: func(string) bool { return false },
    98  		want:   map[string]string{},
    99  	}, {
   100  		name: "pass through",
   101  		in:   map[string]string{"the-dark": "side"},
   102  		want: map[string]string{"the-dark": "side"},
   103  	}, {
   104  		name:   "filter all",
   105  		in:     map[string]string{"the-dark": "side", "of-there": "moon"},
   106  		filter: func(string) bool { return true },
   107  		want:   map[string]string{},
   108  	}, {
   109  		name:   "all together now",
   110  		in:     map[string]string{"another": "brick", "in": "the-wall"},
   111  		filter: func(s string) bool { return s == "in" },
   112  		want:   map[string]string{"another": "brick"},
   113  	}}
   114  
   115  	for _, test := range tests {
   116  		t.Run(test.name, func(t *testing.T) {
   117  			got := Filter(test.in, test.filter)
   118  			if diff := cmp.Diff(test.want, got); diff != "" {
   119  				t.Error("Filter (-want, +got) =", diff)
   120  			}
   121  		})
   122  	}
   123  }
   124  
   125  func TestCopy(t *testing.T) {
   126  	tests := []struct {
   127  		name string
   128  		in   map[string]string
   129  		want map[string]string
   130  	}{{
   131  		name: "nil in",
   132  		want: map[string]string{},
   133  	}, {
   134  		name: "empty in",
   135  		in:   map[string]string{},
   136  		want: map[string]string{},
   137  	}, {
   138  		name: "copy",
   139  		in:   map[string]string{"the-dark": "side"},
   140  		want: map[string]string{"the-dark": "side"},
   141  	}}
   142  
   143  	for _, test := range tests {
   144  		t.Run(test.name, func(t *testing.T) {
   145  			got := Copy(test.in)
   146  			if diff := cmp.Diff(test.want, got); diff != "" {
   147  				t.Error("Copy(-want, +got) =", diff)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func TestExcludeKeys(t *testing.T) {
   154  	tests := []struct {
   155  		name    string
   156  		in      map[string]string
   157  		exclude []string
   158  		want    map[string]string
   159  	}{{
   160  		name: "nil in",
   161  		want: map[string]string{},
   162  	}, {
   163  		name: "no excluded keys",
   164  		in:   map[string]string{"k": "v"},
   165  		want: map[string]string{"k": "v"},
   166  	}, {
   167  		name:    "exclude single key",
   168  		in:      map[string]string{"k": "v"},
   169  		exclude: []string{"k"},
   170  		want:    map[string]string{},
   171  	}, {
   172  		name:    "exclude multiple keys",
   173  		in:      map[string]string{"k": "v", "k2": "v2", "k3": "v3"},
   174  		exclude: []string{"k2", "k3"},
   175  		want:    map[string]string{"k": "v"},
   176  	}, {
   177  		name:    "exclude key not present",
   178  		in:      map[string]string{"k": "v", "k2": "v2", "k3": "v3"},
   179  		exclude: []string{"key"},
   180  		want:    map[string]string{"k": "v", "k2": "v2", "k3": "v3"},
   181  	}}
   182  
   183  	for _, test := range tests {
   184  		t.Run(test.name, func(t *testing.T) {
   185  			got := ExcludeKeys(test.in, test.exclude...)
   186  			if diff := cmp.Diff(test.want, got); diff != "" {
   187  				t.Error("ExcludeKeys (-want, +got) =", diff)
   188  			}
   189  			got = ExcludeKeyList(test.in, test.exclude)
   190  			if diff := cmp.Diff(test.want, got); diff != "" {
   191  				t.Error("ExcludeKeyList (-want, +got) =", diff)
   192  			}
   193  		})
   194  	}
   195  }