| 1 | package model_testscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.25 errors/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.25 testing/" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 8 | "github.com/tomwright/dasel/v3/model/orderedmapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/" |
| 9 | ) |
| 10 | |
| 11 | func TestMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestMap().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 12 | standardMap := func() *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value# { |
| 13 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(map[string]interface{}{ |
| 14 | "foo": "foo1", |
| 15 | "bar": "bar1", |
| 16 | }) |
| 17 | } |
| 18 | |
| 19 | dencodingMap := func() *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value# { |
| 20 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(orderedmapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/.NewMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/NewMap().(). |
| 21 | Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("foo", "foo1"). |
| 22 | Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("bar", "bar1")) |
| 23 | } |
| 24 | |
| 25 | modelMap := func() *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value# { |
| 26 | res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 27 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("foo", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().("foo1")); err != nil { |
| 28 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("unexpected error: %s", err) |
| 29 | } |
| 30 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("bar", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().("bar1")); err != nil { |
| 31 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("unexpected error: %s", err) |
| 32 | } |
| 33 | return res |
| 34 | } |
| 35 | |
| 36 | runTests := func(v func() *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 37 | return func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 38 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("IsMap", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 39 | v := v() |
| 40 | if !v.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().() { |
| 41 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected value to be a map") |
| 42 | } |
| 43 | }) |
| 44 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("GetMapKey", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 45 | v := v() |
| 46 | foo, err := v.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("foo") |
| 47 | if err != nil { |
| 48 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 49 | return |
| 50 | } |
| 51 | got, err := foo.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().() |
| 52 | if err != nil { |
| 53 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 54 | return |
| 55 | } |
| 56 | if got != "foo1" { |
| 57 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected foo1, got %s", got) |
| 58 | } |
| 59 | }) |
| 60 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("SetMapKey", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 61 | v := v() |
| 62 | if err := v.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("baz", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().("baz1")); err != nil { |
| 63 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 64 | return |
| 65 | } |
| 66 | baz, err := v.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("baz") |
| 67 | if err != nil { |
| 68 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 69 | return |
| 70 | } |
| 71 | got, err := baz.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().() |
| 72 | if err != nil { |
| 73 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 74 | return |
| 75 | } |
| 76 | if got != "baz1" { |
| 77 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected baz1, got %s", got) |
| 78 | } |
| 79 | }) |
| 80 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("MapKeys", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 81 | v := v() |
| 82 | keys, err := v.MapKeysscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeys().() |
| 83 | if err != nil { |
| 84 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 85 | return |
| 86 | } |
| 87 | if len(keys) != 2 { |
| 88 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 2 keys, got %d", len(keys)) |
| 89 | } |
| 90 | exp := []string{"foo", "bar"} |
| 91 | for _, k := range exp { |
| 92 | var found bool |
| 93 | for _, e := range keys { |
| 94 | if e == k { |
| 95 | found = true |
| 96 | break |
| 97 | } |
| 98 | } |
| 99 | if !found { |
| 100 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected key %s not found", k) |
| 101 | } |
| 102 | } |
| 103 | }) |
| 104 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("RangeMap", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 105 | v := v() |
| 106 | var keys []string |
| 107 | err := v.RangeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeMap().(func(k string, v *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error { |
| 108 | keys = append(keys, k) |
| 109 | return nil |
| 110 | }) |
| 111 | if err != nil { |
| 112 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 113 | return |
| 114 | } |
| 115 | if len(keys) != 2 { |
| 116 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 2 keys, got %d", len(keys)) |
| 117 | } |
| 118 | exp := []string{"foo", "bar"} |
| 119 | for _, k := range exp { |
| 120 | var found bool |
| 121 | for _, e := range keys { |
| 122 | if e == k { |
| 123 | found = true |
| 124 | break |
| 125 | } |
| 126 | } |
| 127 | if !found { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected key %s not found", k) |
| 129 | } |
| 130 | } |
| 131 | }) |
| 132 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("DeleteMapKey", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 133 | v := v() |
| 134 | if _, err := v.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("foo"); err != nil { |
| 135 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 136 | return |
| 137 | } |
| 138 | if err := v.DeleteMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#DeleteMapKey().("foo"); err != nil { |
| 139 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err) |
| 140 | return |
| 141 | } |
| 142 | _, err := v.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("foo") |
| 143 | if !errorsscip-go gomod github.com/golang/go/src go1.25 errors/.Asscip-go gomod github.com/golang/go/src go1.25 errors/As().(err, &modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.MapKeyNotFoundscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/MapKeyNotFound#{}) { |
| 144 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected key not found error, got %s", err) |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("standard map", runTests(standardMap)) |
| 151 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("dencoding map", runTests(dencodingMap)) |
| 152 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("model map", runTests(modelMap)) |
| 153 | } |
| 154 | |
| 155 | func TestMapCopyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestMapCopy().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 156 | orig := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(orderedmapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/.NewMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/NewMap().().Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("a", "one").Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("b", "two")) |
| 157 | cp, err := orig.MapCopyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapCopy().() |
| 158 | if err != nil { |
| 159 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 160 | } |
| 161 | // Modify original |
| 162 | if err := orig.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("a", modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewStringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewStringValue().("modified")); err != nil { |
| 163 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 164 | } |
| 165 | // Copy should be unchanged |
| 166 | val, err := cp.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("a") |
| 167 | if err != nil { |
| 168 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 169 | } |
| 170 | got, err := val.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().() |
| 171 | if err != nil { |
| 172 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 173 | } |
| 174 | if got != "one" { |
| 175 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 'one', got %s", got) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestMapKeyExistsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestMapKeyExists().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 180 | m := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(orderedmapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/.NewMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/NewMap().().Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("exists", "val")) |
| 181 | |
| 182 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("existing key", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 183 | ok, err := m.MapKeyExistsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyExists().("exists") |
| 184 | if err != nil { |
| 185 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 186 | } |
| 187 | if !ok { |
| 188 | t.Errorscip-go gomod github.com/golang/go/src go1.25 testing/common#Error().("expected key to exist") |
| 189 | } |
| 190 | }) |
| 191 | t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("missing key", func(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 192 | ok, err := m.MapKeyExistsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyExists().("missing") |
| 193 | if err != nil { |
| 194 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 195 | } |
| 196 | if ok { |
| 197 | t.Errorscip-go gomod github.com/golang/go/src go1.25 testing/common#Error().("expected key to not exist") |
| 198 | } |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | func TestMapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestMapKeyValues().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) { |
| 203 | m := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(orderedmapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/.NewMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/NewMap().().Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("a", "one").Setscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model/orderedmap`/Map#Set().("b", "two")) |
| 204 | kvs, err := m.MapKeyValuesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyValues().() |
| 205 | if err != nil { |
| 206 | t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err) |
| 207 | } |
| 208 | if len(kvs) != 2 { |
| 209 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("expected 2 key-value pairs, got %d", len(kvs)) |
| 210 | } |
| 211 | if kvs[0].Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key. != "a" || kvs[1].Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key. != "b" { |
| 212 | t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected keys: %s, %s", kvs[0].Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key., kvs[1].Keyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/KeyValue#Key.) |
| 213 | } |
| 214 | } |