Debian Codegraph @

model/value_slice_test.go

raw symbols SCIP
1 package model_testscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/
2
3 import (
4 "testingscip-go gomod github.com/golang/go/src go1.25 testing/"
5
6 "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/"
7 )
8
9 func TestSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestSlice().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
10 standardSlice := 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# {
11 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().([]any{"foo", "bar"})
12 }
13
14 modelSlice := 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# {
15 res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().()
16 if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(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().("foo")); err != nil {
17 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("unexpected error: %s", err)
18 }
19 if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(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().("bar")); err != nil {
20 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("unexpected error: %s", err)
21 }
22 return res
23 }
24
25 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#) {
26 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#) {
27 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("IsSlice", 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#) {
28 v := v()
29 if !v.IsSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSlice().() {
30 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected value to be a slice")
31 }
32 })
33 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("GetSliceIndex", 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#) {
34 v := v()
35 foo, err := v.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(0)
36 if err != nil {
37 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
38 return
39 }
40 got, err := foo.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
41 if err != nil {
42 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
43 return
44 }
45 if got != "foo" {
46 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected foo, got %s", got)
47 }
48 })
49 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("SetSliceIndex", 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#) {
50 v := v()
51 if err := v.SetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetSliceIndex().(0, 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().("baz")); err != nil {
52 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
53 return
54 }
55 baz, err := v.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(0)
56 if err != nil {
57 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
58 return
59 }
60 got, err := baz.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
61 if err != nil {
62 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
63 return
64 }
65 if got != "baz" {
66 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected baz, got %s", got)
67 }
68 })
69 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("Len", 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#) {
70 v := v()
71 got, err := v.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
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 != 2 {
77 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected len of 2, got %d", got)
78 }
79 })
80 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("RangeSlice", 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 var keys []int
83 var vals []string
84 err := v.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(k int, 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 {
85 keys = append(keys, k)
86 s, err := v.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
87 if err != nil {
88 return err
89 }
90 vals = append(vals, s)
91 return nil
92 })
93 if err != nil {
94 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
95 return
96 }
97 if len(keys) != 2 {
98 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 2 keys, got %d", len(keys))
99 }
100 if len(vals) != 2 {
101 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 2 vals, got %d", len(keys))
102 }
103 exp := []string{"foo", "bar"}
104
105 for k, e := range exp {
106 if keys[k] != k {
107 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected key %d, got %d", k, keys[k])
108 }
109 if vals[k] != e {
110 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected val %s, got %s", e, vals[k])
111 }
112 }
113 })
114 //t.Run("DeleteMapKey", func(t *testing.T) {
115 // v := v()
116 // if _, err := v.GetSliceIndex(1); err != nil {
117 // t.Errorf("unexpected error: %s", err)
118 // return
119 // }
120 // if err := v.DeleteSliceIndex(1); err != nil {
121 // t.Errorf("unexpected error: %s", err)
122 // return
123 // }
124 // _, err := v.GetSliceIndex(1)
125 // notFoundErr := &model.SliceIndexOutOfRange{}
126 // if !errors.As(err, &notFoundErr) {
127 // t.Errorf("expected index not found error, got %s", err)
128 // }
129 //})
130 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("SliceIndexRange", 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#) {
131 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("last element", 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#) {
132 v := v()
133 s, err := v.SliceIndexRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceIndexRange().(-1, -1)
134 if 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 length, err := s.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
139 if err != nil {
140 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
141 return
142 }
143 if length != 1 {
144 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected length of 1, got %d", length)
145 }
146
147 val, err := s.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(0)
148 if err != nil {
149 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
150 return
151 }
152 got, err := val.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
153 if err != nil {
154 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
155 return
156 }
157 if got != "bar" {
158 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected bar, got %s", got)
159 }
160 })
161 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("first element", 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#) {
162 v := v()
163 s, err := v.SliceIndexRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceIndexRange().(0, 0)
164 if err != nil {
165 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
166 return
167 }
168 length, err := s.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
169 if err != nil {
170 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
171 return
172 }
173 if length != 1 {
174 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected length of 1, got %d", length)
175 }
176
177 val, err := s.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(0)
178 if err != nil {
179 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
180 return
181 }
182 got, err := val.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
183 if err != nil {
184 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("unexpected error: %s", err)
185 return
186 }
187 if got != "foo" {
188 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected foo, got %s", got)
189 }
190 })
191 })
192 }
193 }
194
195 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("standard slice", runTests(standardSlice))
196 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("model slice", runTests(modelSlice))
197 }
198
199 func TestSliceIndexRange_Reversescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestSliceIndexRange_Reverse().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
200 s := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().()
201 _ = s.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(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().("a"))
202 _ = s.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(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().("b"))
203 _ = s.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(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().("c"))
204
205 got, err := s.SliceIndexRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceIndexRange().(2, 0)
206 if err != nil {
207 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err)
208 }
209 l, err := got.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
210 if err != nil {
211 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err)
212 }
213 if l != 3 {
214 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("expected 3 elements, got %d", l)
215 }
216 first, _ := got.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(0)
217 sv, _ := first.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
218 if sv != "c" {
219 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 'c' first, got %s", sv)
220 }
221 }
222
223 func TestSlice_Append_BranchValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestSlice_Append_BranchValue().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
224 branch := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().()
225 _ = branch.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(1))
226 _ = branch.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(2))
227 branch.MarkAsBranchscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MarkAsBranch().()
228
229 target := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().()
230 if err := target.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(branch); err != nil {
231 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err)
232 }
233 l, err := target.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().()
234 if err != nil {
235 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err)
236 }
237 // Branch items should be appended individually
238 if l != 2 {
239 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("expected 2 items from branch append, got %d", l)
240 }
241 }
242
243 func TestSlice_GetSliceIndex_OutOfRangescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model_test`/TestSlice_GetSliceIndex_OutOfRange().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
244 s := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().()
245 _ = s.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(1))
246
247 _, err := s.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(5)
248 if err == nil {
249 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().("expected error for out of range index")
250 }
251
252 _, err = s.GetSliceIndexscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetSliceIndex().(-1)
253 if err == nil {
254 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().("expected error for negative index")
255 }
256 }