| 1 | package model_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | ) |
| 8 | |
| 9 | type setTestCase struct { |
| 10 | valueFn func() *model.Value |
| 11 | value *model.Value |
| 12 | newValueFn func() *model.Value |
| 13 | newValue *model.Value |
| 14 | } |
| 15 | |
| 16 | func (tc setTestCase) run(t *testing.T) { |
| 17 | val := tc.value |
| 18 | if tc.valueFn != nil { |
| 19 | val = tc.valueFn() |
| 20 | } |
| 21 | newVal := tc.newValue |
| 22 | if tc.newValueFn != nil { |
| 23 | newVal = tc.newValueFn() |
| 24 | } |
| 25 | if err := val.Set(newVal); err != nil { |
| 26 | t.Errorf("unexpected error: %s", err) |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | eq, err := val.EqualTypeValue(newVal) |
| 31 | if err != nil { |
| 32 | t.Errorf("unexpected error: %s", err) |
| 33 | return |
| 34 | } |
| 35 | if !eq { |
| 36 | t.Errorf("expected values to be equal") |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestValue_Set(t *testing.T) { |
| 41 | testCases := []struct { |
| 42 | name string |
| 43 | stringValue func() *model.Value |
| 44 | intValue func() *model.Value |
| 45 | floatValue func() *model.Value |
| 46 | boolValue func() *model.Value |
| 47 | mapValue func() *model.Value |
| 48 | sliceValue func() *model.Value |
| 49 | nullValue func() *model.Value |
| 50 | }{ |
| 51 | { |
| 52 | name: "model constructor", |
| 53 | stringValue: func() *model.Value { |
| 54 | return model.NewStringValue("hello") |
| 55 | }, |
| 56 | intValue: func() *model.Value { |
| 57 | return model.NewIntValue(1) |
| 58 | }, |
| 59 | floatValue: func() *model.Value { |
| 60 | return model.NewFloatValue(1) |
| 61 | }, |
| 62 | boolValue: func() *model.Value { |
| 63 | return model.NewBoolValue(true) |
| 64 | }, |
| 65 | mapValue: func() *model.Value { |
| 66 | res := model.NewMapValue() |
| 67 | if err := res.SetMapKey("greeting", model.NewStringValue("hello")); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | return res |
| 71 | }, |
| 72 | sliceValue: func() *model.Value { |
| 73 | res := model.NewSliceValue() |
| 74 | if err := res.Append(model.NewStringValue("hello")); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | return res |
| 78 | }, |
| 79 | nullValue: func() *model.Value { |
| 80 | return model.NewNullValue() |
| 81 | }, |
| 82 | }, |
| 83 | { |
| 84 | name: "go types non ptr", |
| 85 | stringValue: func() *model.Value { |
| 86 | v := "hello" |
| 87 | return model.NewValue(v) |
| 88 | }, |
| 89 | intValue: func() *model.Value { |
| 90 | v := int64(1) |
| 91 | return model.NewValue(v) |
| 92 | }, |
| 93 | floatValue: func() *model.Value { |
| 94 | v := 1.0 |
| 95 | return model.NewValue(v) |
| 96 | }, |
| 97 | boolValue: func() *model.Value { |
| 98 | v := true |
| 99 | return model.NewValue(v) |
| 100 | }, |
| 101 | mapValue: func() *model.Value { |
| 102 | v := map[string]interface{}{ |
| 103 | "greeting": "hello", |
| 104 | } |
| 105 | return model.NewValue(v) |
| 106 | }, |
| 107 | sliceValue: func() *model.Value { |
| 108 | v := []interface{}{ |
| 109 | "hello", |
| 110 | } |
| 111 | return model.NewValue(v) |
| 112 | }, |
| 113 | nullValue: func() *model.Value { |
| 114 | return model.NewValue(nil) |
| 115 | }, |
| 116 | }, |
| 117 | { |
| 118 | name: "go types ptr", |
| 119 | stringValue: func() *model.Value { |
| 120 | v := "hello" |
| 121 | return model.NewValue(&v) |
| 122 | }, |
| 123 | intValue: func() *model.Value { |
| 124 | v := int64(1) |
| 125 | return model.NewValue(&v) |
| 126 | }, |
| 127 | floatValue: func() *model.Value { |
| 128 | v := 1.0 |
| 129 | return model.NewValue(&v) |
| 130 | }, |
| 131 | boolValue: func() *model.Value { |
| 132 | v := true |
| 133 | return model.NewValue(&v) |
| 134 | }, |
| 135 | mapValue: func() *model.Value { |
| 136 | v := map[string]interface{}{ |
| 137 | "greeting": "hello", |
| 138 | } |
| 139 | return model.NewValue(&v) |
| 140 | }, |
| 141 | sliceValue: func() *model.Value { |
| 142 | v := []interface{}{ |
| 143 | "hello", |
| 144 | } |
| 145 | return model.NewValue(&v) |
| 146 | }, |
| 147 | nullValue: func() *model.Value { |
| 148 | var x any |
| 149 | return model.NewValue(&x) |
| 150 | }, |
| 151 | }, |
| 152 | } |
| 153 | |
| 154 | for _, testCase := range testCases { |
| 155 | tc := testCase |
| 156 | t.Run(tc.name, func(t *testing.T) { |
| 157 | t.Run("string", setTestCase{ |
| 158 | valueFn: tc.stringValue, |
| 159 | newValue: model.NewStringValue("world"), |
| 160 | }.run) |
| 161 | t.Run("int", setTestCase{ |
| 162 | valueFn: tc.intValue, |
| 163 | newValue: model.NewIntValue(2), |
| 164 | }.run) |
| 165 | t.Run("float", setTestCase{ |
| 166 | valueFn: tc.floatValue, |
| 167 | newValue: model.NewFloatValue(2), |
| 168 | }.run) |
| 169 | t.Run("bool", setTestCase{ |
| 170 | valueFn: tc.boolValue, |
| 171 | newValue: model.NewBoolValue(false), |
| 172 | }.run) |
| 173 | t.Run("map", setTestCase{ |
| 174 | valueFn: tc.mapValue, |
| 175 | newValueFn: func() *model.Value { |
| 176 | res := model.NewMapValue() |
| 177 | if err := res.SetMapKey("greeting", model.NewStringValue("world")); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | return res |
| 181 | }, |
| 182 | }.run) |
| 183 | t.Run("slice", setTestCase{ |
| 184 | valueFn: tc.sliceValue, |
| 185 | newValueFn: func() *model.Value { |
| 186 | res := model.NewSliceValue() |
| 187 | if err := res.Append(model.NewStringValue("world")); err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | return res |
| 191 | }, |
| 192 | }.run) |
| 193 | t.Run("string over int", setTestCase{ |
| 194 | valueFn: tc.intValue, |
| 195 | newValue: model.NewStringValue("world"), |
| 196 | }.run) |
| 197 | t.Run("int over float", setTestCase{ |
| 198 | valueFn: tc.floatValue, |
| 199 | newValue: model.NewIntValue(2), |
| 200 | }.run) |
| 201 | t.Run("float over bool", setTestCase{ |
| 202 | valueFn: tc.boolValue, |
| 203 | newValue: model.NewFloatValue(2), |
| 204 | }.run) |
| 205 | t.Run("bool over map", setTestCase{ |
| 206 | valueFn: tc.mapValue, |
| 207 | newValue: model.NewBoolValue(true), |
| 208 | }.run) |
| 209 | t.Run("map over slice", setTestCase{ |
| 210 | valueFn: tc.sliceValue, |
| 211 | newValueFn: func() *model.Value { |
| 212 | res := model.NewMapValue() |
| 213 | if err := res.SetMapKey("greeting", model.NewStringValue("world")); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | return res |
| 217 | }, |
| 218 | }.run) |
| 219 | t.Run("string over slice", setTestCase{ |
| 220 | valueFn: tc.sliceValue, |
| 221 | newValue: model.NewStringValue("world"), |
| 222 | }.run) |
| 223 | t.Run("slice over map", setTestCase{ |
| 224 | valueFn: tc.mapValue, |
| 225 | newValueFn: func() *model.Value { |
| 226 | res := model.NewSliceValue() |
| 227 | if err := res.Append(model.NewStringValue("world")); err != nil { |
| 228 | t.Fatal(err) |
| 229 | } |
| 230 | return res |
| 231 | }, |
| 232 | }.run) |
| 233 | t.Run("string over null", setTestCase{ |
| 234 | valueFn: tc.nullValue, |
| 235 | newValue: model.NewStringValue("world"), |
| 236 | }.run) |
| 237 | }) |
| 238 | } |
| 239 | } |