|
| 1 | +// |
| 2 | +// |
| 3 | +// Copyright Red Hat |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package overriding |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestUnionStrings(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + a []string |
| 28 | + b []string |
| 29 | + expected []string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "both empty", |
| 33 | + a: []string{}, |
| 34 | + b: []string{}, |
| 35 | + expected: []string{}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "first is nil, second is empty", |
| 39 | + a: nil, |
| 40 | + b: []string{}, |
| 41 | + expected: []string{}, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "first is empty, second is nil", |
| 45 | + a: []string{}, |
| 46 | + b: nil, |
| 47 | + expected: []string{}, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "both are nil", |
| 51 | + a: nil, |
| 52 | + b: nil, |
| 53 | + expected: []string{}, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "no overlap", |
| 57 | + a: []string{"x", "y"}, |
| 58 | + b: []string{"a", "b"}, |
| 59 | + expected: []string{"x", "y", "a", "b"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "partial overlap", |
| 63 | + a: []string{"a", "b"}, |
| 64 | + b: []string{"b", "c"}, |
| 65 | + expected: []string{"a", "b", "c"}, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "duplicate in same slice", |
| 69 | + a: []string{"a", "a", "b"}, |
| 70 | + b: []string{"b", "c"}, |
| 71 | + expected: []string{"a", "b", "c"}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "only duplicates", |
| 75 | + a: []string{"a", "b"}, |
| 76 | + b: []string{"a", "b"}, |
| 77 | + expected: []string{"a", "b"}, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "preserves order", |
| 81 | + a: []string{"c", "a"}, |
| 82 | + b: []string{"b", "a", "d"}, |
| 83 | + expected: []string{"c", "a", "b", "d"}, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + actual := UnionStrings(tt.a, tt.b) |
| 90 | + |
| 91 | + if actual == nil { |
| 92 | + t.Errorf("expected non-nil slice, got nil") |
| 93 | + } |
| 94 | + |
| 95 | + if !reflect.DeepEqual(actual, tt.expected) { |
| 96 | + t.Errorf("UnionStrings(%v, %v) = %v; want %v", tt.a, tt.b, actual, tt.expected) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments