|
5 | 5 | package user
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "reflect" |
8 | 9 | "testing"
|
9 | 10 |
|
10 | 11 | "github.com/ghodss/yaml"
|
@@ -43,3 +44,104 @@ click_admins:
|
43 | 44 | // the initial yaml and the serialized/de-serialized rolesFileContent should be the same
|
44 | 45 | require.Equal(t, r, rAgain)
|
45 | 46 | }
|
| 47 | + |
| 48 | +func TestRolesFileContent_MergeWith(t *testing.T) { |
| 49 | + type args struct { |
| 50 | + other RolesFileContent |
| 51 | + } |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + r RolesFileContent |
| 55 | + args args |
| 56 | + want RolesFileContent |
| 57 | + assert func(r, other, result RolesFileContent) |
| 58 | + }{ |
| 59 | + |
| 60 | + { |
| 61 | + name: "when r is nil", |
| 62 | + r: nil, |
| 63 | + args: args{ |
| 64 | + other: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 65 | + }, |
| 66 | + want: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "when other is nil", |
| 70 | + r: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 71 | + args: args{ |
| 72 | + other: nil, |
| 73 | + }, |
| 74 | + want: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "when both are nil", |
| 78 | + r: nil, |
| 79 | + args: args{ |
| 80 | + other: nil, |
| 81 | + }, |
| 82 | + want: RolesFileContent(map[string]interface{}{}), |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "when r is empty", |
| 86 | + r: RolesFileContent(map[string]interface{}{}), |
| 87 | + args: args{ |
| 88 | + other: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 89 | + }, |
| 90 | + want: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "when other is empty", |
| 94 | + r: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 95 | + args: args{ |
| 96 | + other: RolesFileContent(map[string]interface{}{}), |
| 97 | + }, |
| 98 | + want: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "when r has more items", |
| 102 | + r: RolesFileContent(map[string]interface{}{"a": "b", "d": "e"}), |
| 103 | + args: args{ |
| 104 | + other: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 105 | + }, |
| 106 | + want: RolesFileContent(map[string]interface{}{"a": "c", "d": "e"}), |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "when other has more items", |
| 110 | + r: RolesFileContent(map[string]interface{}{"a": "b"}), |
| 111 | + args: args{ |
| 112 | + other: RolesFileContent(map[string]interface{}{"a": "c", "d": "e"}), |
| 113 | + }, |
| 114 | + want: RolesFileContent(map[string]interface{}{"a": "c", "d": "e"}), |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "does give priority to other", |
| 118 | + r: RolesFileContent(map[string]interface{}{"a": "b"}), |
| 119 | + args: args{ |
| 120 | + other: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 121 | + }, |
| 122 | + want: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "does not mutate in place", |
| 126 | + r: RolesFileContent(map[string]interface{}{"a": "b"}), |
| 127 | + args: args{ |
| 128 | + other: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 129 | + }, |
| 130 | + want: RolesFileContent(map[string]interface{}{"a": "c"}), |
| 131 | + assert: func(r, other, result RolesFileContent) { |
| 132 | + require.Equal(t, r, RolesFileContent(map[string]interface{}{"a": "b"})) |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + got := tt.r.MergeWith(tt.args.other) |
| 139 | + if !reflect.DeepEqual(got, tt.want) { |
| 140 | + t.Errorf("MergeWith() = %v, want %v", got, tt.want) |
| 141 | + } |
| 142 | + if tt.assert != nil { |
| 143 | + tt.assert(tt.r, tt.args.other, got) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments