Debian Codegraph @

model/go_value_test.go

raw symbols SCIP
1 package model_test
2
3 import (
4 "github.com/google/go-cmp/cmp"
5 "github.com/tomwright/dasel/v3/model"
6 "testing"
7 )
8
9 type goValueTestCase struct {
10 in *model.Value
11 inFn func() *model.Value
12 exp any
13 }
14
15 func (tc goValueTestCase) run(t *testing.T) {
16 if tc.inFn != nil {
17 tc.in = tc.inFn()
18 }
19 out, err := tc.in.GoValue()
20 if err != nil {
21 t.Fatalf("unexpected error: %v", err)
22 }
23 if !cmp.Equal(tc.exp, out) {
24 t.Errorf("unexpected result: %s", cmp.Diff(tc.exp, out))
25 }
26 }
27
28 func TestValue_GoValue(t *testing.T) {
29 t.Run("null", goValueTestCase{
30 in: model.NewNullValue(),
31 exp: nil,
32 }.run)
33 t.Run("int", goValueTestCase{
34 in: model.NewIntValue(42),
35 exp: int64(42),
36 }.run)
37 t.Run("float", goValueTestCase{
38 in: model.NewFloatValue(3.14),
39 exp: 3.14,
40 }.run)
41 t.Run("string", goValueTestCase{
42 in: model.NewStringValue("hello"),
43 exp: "hello",
44 }.run)
45 t.Run("bool", goValueTestCase{
46 in: model.NewBoolValue(true),
47 exp: true,
48 }.run)
49 t.Run("slice", goValueTestCase{
50 inFn: func() *model.Value {
51 s := model.NewSliceValue()
52 _ = s.Append(model.NewIntValue(1))
53 _ = s.Append(model.NewIntValue(2))
54 _ = s.Append(model.NewIntValue(3))
55 return s
56 },
57 exp: []any{int64(1), int64(2), int64(3)},
58 }.run)
59 t.Run("map", goValueTestCase{
60 inFn: func() *model.Value {
61 m := model.NewMapValue()
62 _ = m.SetMapKey("a", model.NewStringValue("apple"))
63 return m
64 },
65 exp: map[string]any{
66 "a": "apple",
67 },
68 }.run)
69 }