github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/encoding/json/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 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%"` // https://golang.org/issue/2718 41 } 42 43 type punctuationTag struct { 44 V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // https://golang.org/issue/3546 45 } 46 47 type emptyTag struct { 48 W string 49 } 50 51 type misnamedTag struct { 52 X string `jsom:"Misnamed"` 53 } 54 55 type badFormatTag struct { 56 Y string `:"BadFormat"` 57 } 58 59 type badCodeTag struct { 60 Z string `json:" !\"#&'()*+,."` 61 } 62 63 type spaceTag struct { 64 Q string `json:"With space"` 65 } 66 67 type unicodeTag struct { 68 W string `json:"Ελλάδα"` 69 } 70 71 var structTagObjectKeyTests = []struct { 72 raw interface{} 73 value string 74 key string 75 }{ 76 {basicLatin2xTag{"2x"}, "2x", "$%-/"}, 77 {basicLatin3xTag{"3x"}, "3x", "0123456789"}, 78 {basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"}, 79 {basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"}, 80 {basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"}, 81 {basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"}, 82 {miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"}, 83 {emptyTag{"Pour Moi"}, "Pour Moi", "W"}, 84 {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"}, 85 {badFormatTag{"Orfevre"}, "Orfevre", "Y"}, 86 {badCodeTag{"Reliable Man"}, "Reliable Man", "Z"}, 87 {percentSlashTag{"brut"}, "brut", "text/html%"}, 88 {punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~"}, 89 {spaceTag{"Perreddu"}, "Perreddu", "With space"}, 90 {unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"}, 91 } 92 93 func TestStructTagObjectKey(t *testing.T) { 94 for _, tt := range structTagObjectKeyTests { 95 b, err := Marshal(tt.raw) 96 if err != nil { 97 t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err) 98 } 99 var f interface{} 100 err = Unmarshal(b, &f) 101 if err != nil { 102 t.Fatalf("Unmarshal(%#q) failed: %v", b, err) 103 } 104 for i, v := range f.(map[string]interface{}) { 105 switch i { 106 case tt.key: 107 if s, ok := v.(string); !ok || s != tt.value { 108 t.Fatalf("Unexpected value: %#q, want %v", s, tt.value) 109 } 110 default: 111 t.Fatalf("Unexpected key: %#q, from %#q", i, b) 112 } 113 } 114 } 115 }