Debian Codegraph @

model/value_test.go

raw symbols SCIP
1 package model_test
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/tomwright/dasel/v3/model"
8 "github.com/tomwright/dasel/v3/model/orderedmap"
9 )
10
11 func TestType_String(t *testing.T) {
12 run := func(ty model.Type, exp string) func(*testing.T) {
13 return func(t *testing.T) {
14 got := ty.String()
15 if got != exp {
16 t.Errorf("expected %s, got %s", exp, got)
17 }
18 }
19 }
20 t.Run("string", run(model.TypeString, "string"))
21 t.Run("int", run(model.TypeInt, "int"))
22 t.Run("float", run(model.TypeFloat, "float"))
23 t.Run("bool", run(model.TypeBool, "bool"))
24 t.Run("map", run(model.TypeMap, "map"))
25 t.Run("slice", run(model.TypeSlice, "array"))
26 t.Run("unknown", run(model.TypeUnknown, "unknown"))
27 t.Run("null", run(model.TypeNull, "null"))
28 }
29
30 func TestValue_Len(t *testing.T) {
31 run := func(v *model.Value, exp int) func(*testing.T) {
32 return func(t *testing.T) {
33 got, err := v.Len()
34 if err != nil {
35 t.Errorf("unexpected error: %s", err)
36 return
37 }
38 if got != exp {
39 t.Errorf("expected %d, got %d", exp, got)
40 }
41 }
42 }
43 t.Run("string", func(t *testing.T) {
44 t.Run("empty", run(model.NewStringValue(""), 0))
45 t.Run("non-empty", run(model.NewStringValue("hello"), 5))
46 })
47 t.Run("slice", func(t *testing.T) {
48 t.Run("empty", run(model.NewSliceValue(), 0))
49 t.Run("non-empty", run(model.NewValue([]any{1, 2, 3}), 3))
50 })
51 t.Run("map", func(t *testing.T) {
52 t.Run("empty", run(model.NewMapValue(), 0))
53 t.Run("non-empty", run(model.NewValue(map[string]any{"one": 1, "two": 2, "three": 3}), 3))
54 })
55 }
56
57 func TestValue_IsScalar(t *testing.T) {
58 run := func(v *model.Value, exp bool) func(*testing.T) {
59 return func(t *testing.T) {
60 got := v.IsScalar()
61 if got != exp {
62 t.Errorf("expected %v, got %v", exp, got)
63 }
64 }
65 }
66 t.Run("string", run(model.NewStringValue("foo"), true))
67 t.Run("bool", run(model.NewBoolValue(true), true))
68 t.Run("int", run(model.NewIntValue(1), true))
69 t.Run("float", run(model.NewFloatValue(1.0), true))
70 t.Run("null", run(model.NewNullValue(), true))
71 t.Run("map", run(model.NewMapValue(), false))
72 t.Run("slice", run(model.NewSliceValue(), false))
73
74 t.Run("nested", func(t *testing.T) {
75 t.Run("nested string", run(model.NewNestedValue(model.NewStringValue("foo")), true))
76 t.Run("nested bool", run(model.NewNestedValue(model.NewBoolValue(true)), true))
77 t.Run("nested int", run(model.NewNestedValue(model.NewIntValue(1)), true))
78 t.Run("nested float", run(model.NewNestedValue(model.NewFloatValue(1.0)), true))
79 t.Run("nested null", run(model.NewNestedValue(model.NewNullValue()), true))
80 t.Run("nested map", run(model.NewNestedValue(model.NewMapValue()), false))
81 t.Run("nested slice", run(model.NewNestedValue(model.NewSliceValue()), false))
82
83 t.Run("double nested string", run(model.NewNestedValue(model.NewNestedValue(model.NewStringValue("foo"))), true))
84 })
85 }
86
87 func TestErrorTypes(t *testing.T) {
88 t.Run("MapKeyNotFound", func(t *testing.T) {
89 err := model.MapKeyNotFound{Key: "foo"}
90 if !strings.Contains(err.Error(), "foo") {
91 t.Errorf("expected error to contain 'foo', got %s", err.Error())
92 }
93 })
94 t.Run("SliceIndexOutOfRange", func(t *testing.T) {
95 err := model.SliceIndexOutOfRange{Index: 5}
96 if !strings.Contains(err.Error(), "5") {
97 t.Errorf("expected error to contain '5', got %s", err.Error())
98 }
99 })
100 t.Run("ErrIncompatibleTypes", func(t *testing.T) {
101 err := model.ErrIncompatibleTypes{
102 A: model.NewStringValue("hello"),
103 B: model.NewIntValue(42),
104 }
105 msg := err.Error()
106 if !strings.Contains(msg, "string") || !strings.Contains(msg, "int") {
107 t.Errorf("expected error to contain both types, got %s", msg)
108 }
109 })
110 t.Run("ErrUnexpectedType", func(t *testing.T) {
111 err := model.ErrUnexpectedType{
112 Expected: model.TypeString,
113 Actual: model.TypeInt,
114 }
115 msg := err.Error()
116 if !strings.Contains(msg, "string") || !strings.Contains(msg, "int") {
117 t.Errorf("expected error to contain expected and actual types, got %s", msg)
118 }
119 })
120 t.Run("ErrUnexpectedTypes", func(t *testing.T) {
121 err := model.ErrUnexpectedTypes{
122 Expected: []model.Type{model.TypeSlice, model.TypeMap},
123 Actual: model.TypeInt,
124 }
125 msg := err.Error()
126 if !strings.Contains(msg, "array") || !strings.Contains(msg, "map") || !strings.Contains(msg, "int") {
127 t.Errorf("expected error to contain expected list and actual, got %s", msg)
128 }
129 })
130 }
131
132 func TestErrCouldNotUnpackToType(t *testing.T) {
133 err := model.ErrCouldNotUnpackToType{}
134 got := err.Error()
135 if got == "" {
136 t.Error("expected non-empty error message")
137 }
138 }
139
140 func TestValue_Len_Error(t *testing.T) {
141 _, err := model.NewIntValue(42).Len()
142 if err == nil {
143 t.Fatal("expected error for Len on int")
144 }
145 }
146
147 func TestValue_Copy(t *testing.T) {
148 t.Run("map copy is independent", func(t *testing.T) {
149 orig := model.NewValue(orderedmap.NewMap().Set("a", "one"))
150 cp, err := orig.Copy()
151 if err != nil {
152 t.Fatal(err)
153 }
154 // Modify original
155 if err := orig.SetMapKey("a", model.NewStringValue("modified")); err != nil {
156 t.Fatal(err)
157 }
158 // Copy should be unchanged
159 val, err := cp.GetMapKey("a")
160 if err != nil {
161 t.Fatal(err)
162 }
163 got, err := val.StringValue()
164 if err != nil {
165 t.Fatal(err)
166 }
167 if got != "one" {
168 t.Errorf("expected copy to be 'one', got %s", got)
169 }
170 })
171 t.Run("non-map returns error", func(t *testing.T) {
172 _, err := model.NewIntValue(1).Copy()
173 if err == nil {
174 t.Fatal("expected error for Copy on non-map")
175 }
176 })
177 }
178
179 func TestValues_ToSliceValue(t *testing.T) {
180 vals := model.Values{model.NewIntValue(1), model.NewStringValue("hello")}
181 sv, err := vals.ToSliceValue()
182 if err != nil {
183 t.Fatal(err)
184 }
185 l, err := sv.Len()
186 if err != nil {
187 t.Fatal(err)
188 }
189 if l != 2 {
190 t.Errorf("expected length 2, got %d", l)
191 }
192 }
193
194 func TestValue_Interface(t *testing.T) {
195 t.Run("string", func(t *testing.T) {
196 v := model.NewStringValue("hello")
197 got := v.Interface()
198 if got == nil {
199 t.Fatal("expected non-nil interface")
200 }
201 })
202 t.Run("int", func(t *testing.T) {
203 v := model.NewIntValue(42)
204 got := v.Interface()
205 if got == nil {
206 t.Fatal("expected non-nil interface")
207 }
208 })
209 t.Run("null", func(t *testing.T) {
210 v := model.NewNullValue()
211 got := v.Interface()
212 if got != nil {
213 t.Fatalf("expected nil, got %v", got)
214 }
215 })
216 }
217
218 func TestValue_EqualTypeValue(t *testing.T) {
219 t.Run("same strings", func(t *testing.T) {
220 a := model.NewStringValue("hello")
221 b := model.NewStringValue("hello")
222 eq, err := a.EqualTypeValue(b)
223 if err != nil {
224 t.Fatal(err)
225 }
226 if !eq {
227 t.Error("expected equal")
228 }
229 })
230 t.Run("different strings", func(t *testing.T) {
231 a := model.NewStringValue("hello")
232 b := model.NewStringValue("world")
233 eq, err := a.EqualTypeValue(b)
234 if err != nil {
235 t.Fatal(err)
236 }
237 if eq {
238 t.Error("expected not equal")
239 }
240 })
241 t.Run("different types", func(t *testing.T) {
242 a := model.NewStringValue("1")
243 b := model.NewIntValue(1)
244 eq, err := a.EqualTypeValue(b)
245 if err != nil {
246 t.Fatal(err)
247 }
248 if eq {
249 t.Error("expected not equal for different types")
250 }
251 })
252 t.Run("same ints", func(t *testing.T) {
253 eq, err := model.NewIntValue(42).EqualTypeValue(model.NewIntValue(42))
254 if err != nil {
255 t.Fatal(err)
256 }
257 if !eq {
258 t.Error("expected equal")
259 }
260 })
261 t.Run("different ints", func(t *testing.T) {
262 eq, err := model.NewIntValue(1).EqualTypeValue(model.NewIntValue(2))
263 if err != nil {
264 t.Fatal(err)
265 }
266 if eq {
267 t.Error("expected not equal")
268 }
269 })
270 t.Run("same floats", func(t *testing.T) {
271 eq, err := model.NewFloatValue(3.14).EqualTypeValue(model.NewFloatValue(3.14))
272 if err != nil {
273 t.Fatal(err)
274 }
275 if !eq {
276 t.Error("expected equal")
277 }
278 })
279 t.Run("same bools", func(t *testing.T) {
280 eq, err := model.NewBoolValue(true).EqualTypeValue(model.NewBoolValue(true))
281 if err != nil {
282 t.Fatal(err)
283 }
284 if !eq {
285 t.Error("expected equal")
286 }
287 })
288 t.Run("both null", func(t *testing.T) {
289 eq, err := model.NewNullValue().EqualTypeValue(model.NewNullValue())
290 if err != nil {
291 t.Fatal(err)
292 }
293 if !eq {
294 t.Error("expected equal")
295 }
296 })
297 t.Run("same slices", func(t *testing.T) {
298 a := model.NewSliceValue()
299 _ = a.Append(model.NewIntValue(1))
300 b := model.NewSliceValue()
301 _ = b.Append(model.NewIntValue(1))
302 eq, err := a.EqualTypeValue(b)
303 if err != nil {
304 t.Fatal(err)
305 }
306 if !eq {
307 t.Error("expected equal")
308 }
309 })
310 t.Run("different length slices", func(t *testing.T) {
311 a := model.NewSliceValue()
312 _ = a.Append(model.NewIntValue(1))
313 b := model.NewSliceValue()
314 eq, err := a.EqualTypeValue(b)
315 if err != nil {
316 t.Fatal(err)
317 }
318 if eq {
319 t.Error("expected not equal")
320 }
321 })
322 t.Run("same maps", func(t *testing.T) {
323 a := model.NewMapValue()
324 _ = a.SetMapKey("k", model.NewStringValue("v"))
325 b := model.NewMapValue()
326 _ = b.SetMapKey("k", model.NewStringValue("v"))
327 eq, err := a.EqualTypeValue(b)
328 if err != nil {
329 t.Fatal(err)
330 }
331 if !eq {
332 t.Error("expected equal")
333 }
334 })
335 t.Run("different maps", func(t *testing.T) {
336 a := model.NewMapValue()
337 _ = a.SetMapKey("k", model.NewStringValue("v"))
338 b := model.NewMapValue()
339 _ = b.SetMapKey("k", model.NewStringValue("w"))
340 eq, err := a.EqualTypeValue(b)
341 if err != nil {
342 t.Fatal(err)
343 }
344 if eq {
345 t.Error("expected not equal")
346 }
347 })
348 }
349
350 func TestValue_GoValue_AllTypes(t *testing.T) {
351 t.Run("string", func(t *testing.T) {
352 v := model.NewStringValue("hello")
353 got, err := v.GoValue()
354 if err != nil {
355 t.Fatal(err)
356 }
357 if got != "hello" {
358 t.Errorf("expected 'hello', got %v", got)
359 }
360 })
361 t.Run("int", func(t *testing.T) {
362 v := model.NewIntValue(42)
363 got, err := v.GoValue()
364 if err != nil {
365 t.Fatal(err)
366 }
367 if got != int64(42) {
368 t.Errorf("expected 42, got %v", got)
369 }
370 })
371 t.Run("float", func(t *testing.T) {
372 v := model.NewFloatValue(3.14)
373 got, err := v.GoValue()
374 if err != nil {
375 t.Fatal(err)
376 }
377 if got != 3.14 {
378 t.Errorf("expected 3.14, got %v", got)
379 }
380 })
381 t.Run("bool", func(t *testing.T) {
382 v := model.NewBoolValue(true)
383 got, err := v.GoValue()
384 if err != nil {
385 t.Fatal(err)
386 }
387 if got != true {
388 t.Errorf("expected true, got %v", got)
389 }
390 })
391 t.Run("null", func(t *testing.T) {
392 v := model.NewNullValue()
393 got, err := v.GoValue()
394 if err != nil {
395 t.Fatal(err)
396 }
397 if got != nil {
398 t.Errorf("expected nil, got %v", got)
399 }
400 })
401 t.Run("map", func(t *testing.T) {
402 v := model.NewMapValue()
403 _ = v.SetMapKey("key", model.NewStringValue("val"))
404 got, err := v.GoValue()
405 if err != nil {
406 t.Fatal(err)
407 }
408 m, ok := got.(map[string]any)
409 if !ok {
410 t.Fatalf("expected map, got %T", got)
411 }
412 if m["key"] != "val" {
413 t.Errorf("expected 'val', got %v", m["key"])
414 }
415 })
416 t.Run("slice", func(t *testing.T) {
417 v := model.NewSliceValue()
418 _ = v.Append(model.NewIntValue(1))
419 _ = v.Append(model.NewIntValue(2))
420 got, err := v.GoValue()
421 if err != nil {
422 t.Fatal(err)
423 }
424 s, ok := got.([]any)
425 if !ok {
426 t.Fatalf("expected slice, got %T", got)
427 }
428 if len(s) != 2 {
429 t.Errorf("expected length 2, got %d", len(s))
430 }
431 })
432 }
433
434 func TestValue_Set_SetFn(t *testing.T) {
435 // When we get a map key, the returned value has a setFn that allows setting it back.
436 m := model.NewValue(orderedmap.NewMap().Set("key", "original"))
437 val, err := m.GetMapKey("key")
438 if err != nil {
439 t.Fatal(err)
440 }
441 if err := val.Set(model.NewStringValue("modified")); err != nil {
442 t.Fatal(err)
443 }
444 // Verify the map was updated
445 updated, err := m.GetMapKey("key")
446 if err != nil {
447 t.Fatal(err)
448 }
449 got, err := updated.StringValue()
450 if err != nil {
451 t.Fatal(err)
452 }
453 if got != "modified" {
454 t.Errorf("expected 'modified', got %s", got)
455 }
456 }
457
458 func TestValue_Set_Nested(t *testing.T) {
459 // Test setting a value via a nested dasel value (exercises the isDaselValue branch in Set)
460 inner := model.NewStringValue("original")
461 outer := model.NewNestedValue(inner)
462 if err := outer.Set(model.NewStringValue("updated")); err != nil {
463 t.Fatal(err)
464 }
465 }
466
467 func TestValue_Kind(t *testing.T) {
468 // Exercise the Kind method
469 v := model.NewStringValue("hello")
470 k := v.Kind()
471 if k.String() == "" {
472 t.Error("expected non-empty kind")
473 }
474 }
475
476 func TestValue_Type_AllTypes(t *testing.T) {
477 tests := []struct {
478 name string
479 val *model.Value
480 typ model.Type
481 }{
482 {"string", model.NewStringValue("hello"), model.TypeString},
483 {"int", model.NewIntValue(1), model.TypeInt},
484 {"float", model.NewFloatValue(1.0), model.TypeFloat},
485 {"bool", model.NewBoolValue(true), model.TypeBool},
486 {"map", model.NewMapValue(), model.TypeMap},
487 {"slice", model.NewSliceValue(), model.TypeSlice},
488 {"null", model.NewNullValue(), model.TypeNull},
489 }
490 for _, tt := range tests {
491 t.Run(tt.name, func(t *testing.T) {
492 if got := tt.val.Type(); got != tt.typ {
493 t.Errorf("expected %s, got %s", tt.typ, got)
494 }
495 })
496 }
497 }
498
499 func TestValue_MapLen(t *testing.T) {
500 m := model.NewValue(orderedmap.NewMap().Set("a", 1).Set("b", 2).Set("c", 3))
501 l, err := m.MapLen()
502 if err != nil {
503 t.Fatal(err)
504 }
505 if l != 3 {
506 t.Errorf("expected 3, got %d", l)
507 }
508 }
509
510 func TestValue_SliceLen_Error(t *testing.T) {
511 _, err := model.NewIntValue(42).SliceLen()
512 if err == nil {
513 t.Fatal("expected error for SliceLen on int")
514 }
515 }
516
517 func TestValue_SetSliceIndex_Error(t *testing.T) {
518 s := model.NewSliceValue()
519 _ = s.Append(model.NewIntValue(1))
520 err := s.SetSliceIndex(5, model.NewIntValue(2))
521 if err == nil {
522 t.Fatal("expected error for out of range SetSliceIndex")
523 }
524 }
525
526 func TestValue_String(t *testing.T) {
527 t.Run("string", func(t *testing.T) {
528 got := model.NewStringValue("hello").String()
529 if !strings.Contains(got, "hello") {
530 t.Errorf("expected output to contain 'hello', got %s", got)
531 }
532 })
533 t.Run("int", func(t *testing.T) {
534 got := model.NewIntValue(42).String()
535 if !strings.Contains(got, "42") {
536 t.Errorf("expected output to contain '42', got %s", got)
537 }
538 })
539 t.Run("float", func(t *testing.T) {
540 got := model.NewFloatValue(3.14).String()
541 if !strings.Contains(got, "3.14") {
542 t.Errorf("expected output to contain '3.14', got %s", got)
543 }
544 })
545 t.Run("bool", func(t *testing.T) {
546 got := model.NewBoolValue(true).String()
547 if !strings.Contains(got, "true") {
548 t.Errorf("expected output to contain 'true', got %s", got)
549 }
550 })
551 t.Run("null", func(t *testing.T) {
552 got := model.NewNullValue().String()
553 if !strings.Contains(got, "null") {
554 t.Errorf("expected output to contain 'null', got %s", got)
555 }
556 })
557 t.Run("slice", func(t *testing.T) {
558 s := model.NewSliceValue()
559 _ = s.Append(model.NewIntValue(1))
560 got := s.String()
561 if !strings.Contains(got, "array") {
562 t.Errorf("expected output to contain 'array', got %s", got)
563 }
564 })
565 t.Run("map", func(t *testing.T) {
566 m := model.NewValue(orderedmap.NewMap().Set("key", "val"))
567 got := m.String()
568 if !strings.Contains(got, "key") {
569 t.Errorf("expected output to contain 'key', got %s", got)
570 }
571 })
572 }