| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | ) |
| 8 | |
| 9 | func TestFuncEntries(t *testing.T) { |
| 10 | t.Run("map to entries", testCase{ |
| 11 | s: `{"a": 1, "b": 2}.entries()`, |
| 12 | outFn: func() *model.Value { |
| 13 | r := model.NewSliceValue() |
| 14 | |
| 15 | e1 := model.NewMapValue() |
| 16 | _ = e1.SetMapKey("key", model.NewStringValue("a")) |
| 17 | _ = e1.SetMapKey("value", model.NewIntValue(1)) |
| 18 | _ = r.Append(e1) |
| 19 | |
| 20 | e2 := model.NewMapValue() |
| 21 | _ = e2.SetMapKey("key", model.NewStringValue("b")) |
| 22 | _ = e2.SetMapKey("value", model.NewIntValue(2)) |
| 23 | _ = r.Append(e2) |
| 24 | |
| 25 | return r |
| 26 | }, |
| 27 | }.run) |
| 28 | } |
| 29 | |
| 30 | func TestFuncFromEntries(t *testing.T) { |
| 31 | t.Run("entries to map", testCase{ |
| 32 | s: `[{"key": "a", "value": 1}, {"key": "b", "value": 2}].fromEntries()`, |
| 33 | outFn: func() *model.Value { |
| 34 | r := model.NewMapValue() |
| 35 | _ = r.SetMapKey("a", model.NewIntValue(1)) |
| 36 | _ = r.SetMapKey("b", model.NewIntValue(2)) |
| 37 | return r |
| 38 | }, |
| 39 | }.run) |
| 40 | t.Run("roundtrip", testCase{ |
| 41 | s: `{"x": 10, "y": 20}.entries().fromEntries()`, |
| 42 | outFn: func() *model.Value { |
| 43 | r := model.NewMapValue() |
| 44 | _ = r.SetMapKey("x", model.NewIntValue(10)) |
| 45 | _ = r.SetMapKey("y", model.NewIntValue(20)) |
| 46 | return r |
| 47 | }, |
| 48 | }.run) |
| 49 | } |