...
1
2
3
4
19
20 package yaml
21
22 import (
23 "fmt"
24 "testing"
25 )
26
27 func TestUnmarshalWithTags(t *testing.T) {
28 type WithTaggedField struct {
29 Field string `json:"field"`
30 }
31
32 t.Run("Known tagged field", func(t *testing.T) {
33 y := []byte(`field: "hello"`)
34 v := WithTaggedField{}
35 if err := Unmarshal(y, &v, DisallowUnknownFields); err != nil {
36 t.Errorf("unexpected error: %v", err)
37 }
38 if v.Field != "hello" {
39 t.Errorf("v.Field=%v, want 'hello'", v.Field)
40 }
41
42 })
43 t.Run("With unknown tagged field", func(t *testing.T) {
44 y := []byte(`unknown: "hello"`)
45 v := WithTaggedField{}
46 err := Unmarshal(y, &v, DisallowUnknownFields)
47 if err == nil {
48 t.Errorf("want error because of unknown field, got <nil>: v=%#v", v)
49 }
50 })
51
52 }
53
54 func exampleUnknown() {
55 type WithTaggedField struct {
56 Field string `json:"field"`
57 }
58 y := []byte(`unknown: "hello"`)
59 v := WithTaggedField{}
60 fmt.Printf("%v\n", Unmarshal(y, &v, DisallowUnknownFields))
61
62
63 }
64
View as plain text