File: yaml_go110_test.go

package info (click to toggle)
golang-github-openshift-api 4.0%2Bgit20190508.81d064c-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 14,040 kB
  • sloc: sh: 171; makefile: 32
file content (46 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (3)
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
// +build go1.10

package yaml

import (
	"fmt"
	"testing"
)

func TestUnmarshalWithTags(t *testing.T) {
	type WithTaggedField struct {
		Field string `json:"field"`
	}

	t.Run("Known tagged field", func(t *testing.T) {
		y := []byte(`field: "hello"`)
		v := WithTaggedField{}
		if err := Unmarshal(y, &v, DisallowUnknownFields); err != nil {
			t.Errorf("unexpected error: %v", err)
		}
		if v.Field != "hello" {
			t.Errorf("v.Field=%v, want 'hello'", v.Field)
		}

	})
	t.Run("With unknown tagged field", func(t *testing.T) {
		y := []byte(`unknown: "hello"`)
		v := WithTaggedField{}
		err := Unmarshal(y, &v, DisallowUnknownFields)
		if err == nil {
			t.Errorf("want error because of unknown field, got <nil>: v=%#v", v)
		}
	})

}

func exampleUnknown() {
	type WithTaggedField struct {
		Field string `json:"field"`
	}
	y := []byte(`unknown: "hello"`)
	v := WithTaggedField{}
	fmt.Printf("%v\n", Unmarshal(y, &v, DisallowUnknownFields))
	// Ouptut:
	// unmarshaling JSON: while decoding JSON: json: unknown field "unknown"
}