LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package json | 5 package json |
6 | 6 |
7 import ( | 7 import ( |
8 "testing" | 8 "testing" |
9 ) | 9 ) |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 } | 49 } |
50 | 50 |
51 type misnamedTag struct { | 51 type misnamedTag struct { |
52 X string `jsom:"Misnamed"` | 52 X string `jsom:"Misnamed"` |
53 } | 53 } |
54 | 54 |
55 type badFormatTag struct { | 55 type badFormatTag struct { |
56 Y string `:"BadFormat"` | 56 Y string `:"BadFormat"` |
57 } | 57 } |
58 | 58 |
| 59 type badCodeTag struct { |
| 60 Z string `json:" !\"#&'()*+,."` |
| 61 } |
| 62 |
59 type spaceTag struct { | 63 type spaceTag struct { |
60 » Z string `json:"With space"` | 64 » Q string `json:"With space"` |
61 } | 65 } |
62 | 66 |
63 type unicodeTag struct { | 67 type unicodeTag struct { |
64 » Q string `json:"Ελλάδα"` | 68 » W string `json:"Ελλάδα"` |
65 } | 69 } |
66 | 70 |
67 var structTagObjectKeyTests = []struct { | 71 var structTagObjectKeyTests = []struct { |
68 raw interface{} | 72 raw interface{} |
69 value string | 73 value string |
70 key string | 74 key string |
71 }{ | 75 }{ |
72 {basicLatin2xTag{"2x"}, "2x", "$%-/"}, | 76 {basicLatin2xTag{"2x"}, "2x", "$%-/"}, |
73 {basicLatin3xTag{"3x"}, "3x", "0123456789"}, | 77 {basicLatin3xTag{"3x"}, "3x", "0123456789"}, |
74 {basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"}, | 78 {basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"}, |
75 {basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"}, | 79 {basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"}, |
76 {basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"}, | 80 {basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"}, |
77 {basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"}, | 81 {basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"}, |
78 {miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"}, | 82 {miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"}, |
79 {emptyTag{"Pour Moi"}, "Pour Moi", "W"}, | 83 {emptyTag{"Pour Moi"}, "Pour Moi", "W"}, |
80 {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"}, | 84 {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"}, |
81 {badFormatTag{"Orfevre"}, "Orfevre", "Y"}, | 85 {badFormatTag{"Orfevre"}, "Orfevre", "Y"}, |
| 86 {badCodeTag{"Reliable Man"}, "Reliable Man", "Z"}, |
82 {percentSlashTag{"brut"}, "brut", "text/html%"}, | 87 {percentSlashTag{"brut"}, "brut", "text/html%"}, |
83 {punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~
"}, | 88 {punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~
"}, |
84 {spaceTag{"Perreddu"}, "Perreddu", "With space"}, | 89 {spaceTag{"Perreddu"}, "Perreddu", "With space"}, |
85 {unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"}, | 90 {unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"}, |
86 } | 91 } |
87 | 92 |
88 func TestStructTagObjectKey(t *testing.T) { | 93 func TestStructTagObjectKey(t *testing.T) { |
89 for _, tt := range structTagObjectKeyTests { | 94 for _, tt := range structTagObjectKeyTests { |
90 b, err := Marshal(tt.raw) | 95 b, err := Marshal(tt.raw) |
91 if err != nil { | 96 if err != nil { |
92 t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err) | 97 t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err) |
93 } | 98 } |
94 var f interface{} | 99 var f interface{} |
95 err = Unmarshal(b, &f) | 100 err = Unmarshal(b, &f) |
96 if err != nil { | 101 if err != nil { |
97 t.Fatalf("Unmarshal(%#q) failed: %v", b, err) | 102 t.Fatalf("Unmarshal(%#q) failed: %v", b, err) |
98 } | 103 } |
99 for i, v := range f.(map[string]interface{}) { | 104 for i, v := range f.(map[string]interface{}) { |
100 switch i { | 105 switch i { |
101 case tt.key: | 106 case tt.key: |
102 if s, ok := v.(string); !ok || s != tt.value { | 107 if s, ok := v.(string); !ok || s != tt.value { |
103 t.Fatalf("Unexpected value: %#q, want %v
", s, tt.value) | 108 t.Fatalf("Unexpected value: %#q, want %v
", s, tt.value) |
104 } | 109 } |
105 default: | 110 default: |
106 t.Fatalf("Unexpected key: %#q, from %#q", i, b) | 111 t.Fatalf("Unexpected key: %#q, from %#q", i, b) |
107 } | 112 } |
108 } | 113 } |
109 } | 114 } |
110 } | 115 } |
LEFT | RIGHT |