github.com/3JoB/go-json@v0.10.4/tagkey_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package json_test 6 7 import ( 8 "testing" 9 10 "github.com/3JoB/go-json" 11 ) 12 13 type basicLatin2xTag struct { 14 V string `json:"$%-/"` 15 } 16 17 type basicLatin3xTag struct { 18 V string `json:"0123456789"` 19 } 20 21 type basicLatin4xTag struct { 22 V string `json:"ABCDEFGHIJKLMO"` 23 } 24 25 type basicLatin5xTag struct { 26 V string `json:"PQRSTUVWXYZ_"` 27 } 28 29 type basicLatin6xTag struct { 30 V string `json:"abcdefghijklmno"` 31 } 32 33 type basicLatin7xTag struct { 34 V string `json:"pqrstuvwxyz"` 35 } 36 37 type miscPlaneTag struct { 38 V string `json:"色は匂へど"` 39 } 40 41 type percentSlashTag struct { 42 V string `json:"text/html%"` // https://golang.org/issue/2718 43 } 44 45 type punctuationTag struct { 46 V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // https://golang.org/issue/3546 47 } 48 49 type dashTag struct { 50 V string `json:"-,"` 51 } 52 53 type emptyTag struct { 54 W string 55 } 56 57 type misnamedTag struct { 58 X string `jsom:"Misnamed"` 59 } 60 61 type badFormatTag struct { 62 Y string `:"BadFormat"` 63 } 64 65 type badCodeTag struct { 66 Z string `json:" !\"#&'()*+,."` 67 } 68 69 type spaceTag struct { 70 Q string `json:"With space"` 71 } 72 73 type unicodeTag struct { 74 W string `json:"Ελλάδα"` 75 } 76 77 var structTagObjectKeyTests = []struct { 78 raw any 79 value string 80 key string 81 }{ 82 {raw: basicLatin2xTag{V: "2x"}, value: "2x", key: "$%-/"}, 83 {raw: basicLatin3xTag{V: "3x"}, value: "3x", key: "0123456789"}, 84 {raw: basicLatin4xTag{V: "4x"}, value: "4x", key: "ABCDEFGHIJKLMO"}, 85 {raw: basicLatin5xTag{V: "5x"}, value: "5x", key: "PQRSTUVWXYZ_"}, 86 {raw: basicLatin6xTag{V: "6x"}, value: "6x", key: "abcdefghijklmno"}, 87 {raw: basicLatin7xTag{V: "7x"}, value: "7x", key: "pqrstuvwxyz"}, 88 {raw: miscPlaneTag{V: "いろはにほへと"}, value: "いろはにほへと", key: "色は匂へど"}, 89 {raw: dashTag{V: "foo"}, value: "foo", key: "-"}, 90 {raw: emptyTag{W: "Pour Moi"}, value: "Pour Moi", key: "W"}, 91 {raw: misnamedTag{X: "Animal Kingdom"}, value: "Animal Kingdom", key: "X"}, 92 {raw: badFormatTag{Y: "Orfevre"}, value: "Orfevre", key: "Y"}, 93 {raw: badCodeTag{Z: "Reliable Man"}, value: "Reliable Man", key: "Z"}, 94 {raw: percentSlashTag{V: "brut"}, value: "brut", key: "text/html%"}, 95 {raw: punctuationTag{V: "Union Rags"}, value: "Union Rags", key: "!#$%&()*+-./:<=>?@[]^_{|}~"}, 96 {raw: spaceTag{Q: "Perreddu"}, value: "Perreddu", key: "With space"}, 97 {raw: unicodeTag{W: "Loukanikos"}, value: "Loukanikos", key: "Ελλάδα"}, 98 } 99 100 func TestStructTagObjectKey(t *testing.T) { 101 for _, tt := range structTagObjectKeyTests { 102 b, err := json.Marshal(tt.raw) 103 if err != nil { 104 t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err) 105 } 106 var f any 107 err = json.Unmarshal(b, &f) 108 if err != nil { 109 t.Fatalf("Unmarshal(%#q) failed: %v", b, err) 110 } 111 for i, v := range f.(map[string]any) { 112 switch i { 113 case tt.key: 114 if s, ok := v.(string); !ok || s != tt.value { 115 t.Fatalf("Unexpected value: %#q, want %v", s, tt.value) 116 } 117 default: 118 t.Fatalf("Unexpected key: %#q, from %#q", i, b) 119 } 120 } 121 } 122 }