File: yaml_decoder_test.go

package info (click to toggle)
dasel 2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,844 kB
  • sloc: sh: 53; python: 21; makefile: 21; xml: 20
file content (154 lines) | stat: -rw-r--r-- 2,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package dencoding_test

import (
	"bytes"
	"io"
	"reflect"
	"testing"

	"github.com/tomwright/dasel/v2/dencoding"
)

func TestYAMLDecoder_Decode(t *testing.T) {

	t.Run("Basic", func(t *testing.T) {

		b := []byte(`
x: 1
a: hello
---
x: 2
a: there
---
a: Tom
x: 3
---`)
		dec := dencoding.NewYAMLDecoder(bytes.NewReader(b))

		maps := make([]any, 0)
		for {
			var v any
			if err := dec.Decode(&v); err != nil {
				if err == io.EOF {
					break
				}
				t.Errorf("unexpected error: %v", err)
				return
			}
			maps = append(maps, v)
		}

		exp := [][]dencoding.KeyValue{
			{
				{Key: "x", Value: int64(1)},
				{Key: "a", Value: "hello"},
			},
			{
				{Key: "x", Value: int64(2)},
				{Key: "a", Value: "there"},
			},
			{
				{Key: "a", Value: "Tom"},
				{Key: "x", Value: int64(3)},
			},
		}

		got := make([][]dencoding.KeyValue, 0)
		for _, v := range maps {
			if m, ok := v.(*dencoding.Map); ok {
				got = append(got, m.KeyValues())
			}
		}

		if !reflect.DeepEqual(exp, got) {
			t.Errorf("expected %v, got %v", exp, got)
		}
	})

	// https://github.com/TomWright/dasel/issues/278
	t.Run("Issue278", func(t *testing.T) {
		b := []byte(`
key1: [value1,value2,value3,value4,value5]
key2: value6
`)
		dec := dencoding.NewYAMLDecoder(bytes.NewReader(b))

		got := make([]any, 0)
		for {
			var v any
			if err := dec.Decode(&v); err != nil {
				if err == io.EOF {
					break
				}
				t.Errorf("unexpected error: %v", err)
				return
			}
			got = append(got, v)
		}

		exp := []any{
			dencoding.NewMap().
				Set("key1", []any{"value1", "value2", "value3", "value4", "value5"}).
				Set("key2", "value6"),
		}

		if !reflect.DeepEqual(exp, got) {
			t.Errorf("expected %v, got %v", exp, got)
		}
	})

	t.Run("YamlAliases", func(t *testing.T) {
		b := []byte(`foo: &foofoo
  bar: 1
  baz: &baz "baz"
spam:
  ham: "eggs"
  bar: 0
  <<: *foofoo
  baz: "bazbaz"

baz: *baz
`)

		dec := dencoding.NewYAMLDecoder(bytes.NewReader(b))

		got := make([]any, 0)
		for {
			var v any
			if err := dec.Decode(&v); err != nil {
				if err == io.EOF {
					break
				}
				t.Errorf("unexpected error: %v", err)
				return
			}
			got = append(got, v)
		}

		exp := dencoding.NewMap().
			Set("foo", dencoding.NewMap().
				Set("bar", int64(1)).
				Set("baz", "baz")).
			Set("spam", dencoding.NewMap().
				Set("ham", "eggs").
				Set("bar", int64(1)).
				Set("baz", "bazbaz")).
			Set("baz", "baz")

		if len(got) != 1 {
			t.Errorf("expected result len of %d, got %d", 1, len(got))
			return
		}

		gotMap, ok := got[0].(*dencoding.Map)
		if !ok {
			t.Errorf("expected result to be of type %T, got %T", exp, got[0])
			return
		}

		if !reflect.DeepEqual(exp, gotMap) {
			t.Errorf("expected %v, got %v", exp, gotMap)
		}
	})

}