File: toml_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 (79 lines) | stat: -rw-r--r-- 1,266 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
package dencoding_test

import (
	"bytes"
	"github.com/tomwright/dasel/v2/dencoding"
	"io"
	"reflect"
	"testing"
)

func TestTOMLDecoder_Decode(t *testing.T) {

	t.Run("KeyValue", func(t *testing.T) {
		b := []byte(`x = 1
a = 'hello'`)
		dec := dencoding.NewTOMLDecoder(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 := []any{
			map[string]any{
				"x": int64(1),
				"a": "hello",
			},
		}

		got := maps

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

	t.Run("Table", func(t *testing.T) {
		b := []byte(`
[user]
name = "Tom"
age = 29
`)
		dec := dencoding.NewTOMLDecoder(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{
			map[string]any{
				"user": map[string]any{
					"age":  int64(29),
					"name": "Tom",
				},
			},
		}

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