1
2
3
4
5 package json
6
7 import (
8 "testing"
9 )
10
11 type basicLatin2xTag struct {
12 V string `json:"$%-/"`
13 }
14
15 type basicLatin3xTag struct {
16 V string `json:"0123456789"`
17 }
18
19 type basicLatin4xTag struct {
20 V string `json:"ABCDEFGHIJKLMO"`
21 }
22
23 type basicLatin5xTag struct {
24 V string `json:"PQRSTUVWXYZ_"`
25 }
26
27 type basicLatin6xTag struct {
28 V string `json:"abcdefghijklmno"`
29 }
30
31 type basicLatin7xTag struct {
32 V string `json:"pqrstuvwxyz"`
33 }
34
35 type miscPlaneTag struct {
36 V string `json:"色は匂へど"`
37 }
38
39 type percentSlashTag struct {
40 V string `json:"text/html%"`
41 }
42
43 type punctuationTag struct {
44 V string `json:"!#$%&()*+-./:;<=>?@[]^_{|}~ "`
45 }
46
47 type dashTag struct {
48 V string `json:"-,"`
49 }
50
51 type emptyTag struct {
52 W string
53 }
54
55 type misnamedTag struct {
56 X string `jsom:"Misnamed"`
57 }
58
59 type badFormatTag struct {
60 Y string `:"BadFormat"`
61 }
62
63 type badCodeTag struct {
64 Z string `json:" !\"#&'()*+,."`
65 }
66
67 type spaceTag struct {
68 Q string `json:"With space"`
69 }
70
71 type unicodeTag struct {
72 W string `json:"Ελλάδα"`
73 }
74
75 func TestStructTagObjectKey(t *testing.T) {
76 tests := []struct {
77 CaseName
78 raw any
79 value string
80 key string
81 }{
82 {Name(""), basicLatin2xTag{"2x"}, "2x", "$%-/"},
83 {Name(""), basicLatin3xTag{"3x"}, "3x", "0123456789"},
84 {Name(""), basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"},
85 {Name(""), basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"},
86 {Name(""), basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"},
87 {Name(""), basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"},
88 {Name(""), miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"},
89 {Name(""), dashTag{"foo"}, "foo", "-"},
90 {Name(""), emptyTag{"Pour Moi"}, "Pour Moi", "W"},
91 {Name(""), misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"},
92 {Name(""), badFormatTag{"Orfevre"}, "Orfevre", "Y"},
93 {Name(""), badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
94 {Name(""), percentSlashTag{"brut"}, "brut", "text/html%"},
95 {Name(""), punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:;<=>?@[]^_{|}~ "},
96 {Name(""), spaceTag{"Perreddu"}, "Perreddu", "With space"},
97 {Name(""), unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"},
98 }
99 for _, tt := range tests {
100 t.Run(tt.Name, func(t *testing.T) {
101 b, err := Marshal(tt.raw)
102 if err != nil {
103 t.Fatalf("%s: Marshal error: %v", tt.Where, err)
104 }
105 var f any
106 err = Unmarshal(b, &f)
107 if err != nil {
108 t.Fatalf("%s: Unmarshal error: %v", tt.Where, err)
109 }
110 for k, v := range f.(map[string]any) {
111 if k == tt.key {
112 if s, ok := v.(string); !ok || s != tt.value {
113 t.Fatalf("%s: Unmarshal(%#q) value:\n\tgot: %q\n\twant: %q", tt.Where, b, s, tt.value)
114 }
115 } else {
116 t.Fatalf("%s: Unmarshal(%#q): unexpected key: %q", tt.Where, b, k)
117 }
118 }
119 })
120 }
121 }
122
View as plain text