OSDN Git Service

bba5730353d27f0e26d3cc4f7444da89bdb7a21b
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / json / tagkey_test.go
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%"` // http://golang.org/issue/2718
41 }
42
43 type emptyTag struct {
44         W string
45 }
46
47 type misnamedTag struct {
48         X string `jsom:"Misnamed"`
49 }
50
51 type badFormatTag struct {
52         Y string `:"BadFormat"`
53 }
54
55 type badCodeTag struct {
56         Z string `json:" !\"#&'()*+,."`
57 }
58
59 var structTagObjectKeyTests = []struct {
60         raw   interface{}
61         value string
62         key   string
63 }{
64         {basicLatin2xTag{"2x"}, "2x", "$%-/"},
65         {basicLatin3xTag{"3x"}, "3x", "0123456789"},
66         {basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"},
67         {basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"},
68         {basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"},
69         {basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"},
70         {miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"},
71         {emptyTag{"Pour Moi"}, "Pour Moi", "W"},
72         {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"},
73         {badFormatTag{"Orfevre"}, "Orfevre", "Y"},
74         {badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
75         {percentSlashTag{"brut"}, "brut", "text/html%"},
76 }
77
78 func TestStructTagObjectKey(t *testing.T) {
79         for _, tt := range structTagObjectKeyTests {
80                 b, err := Marshal(tt.raw)
81                 if err != nil {
82                         t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err)
83                 }
84                 var f interface{}
85                 err = Unmarshal(b, &f)
86                 if err != nil {
87                         t.Fatalf("Unmarshal(%#q) failed: %v", b, err)
88                 }
89                 for i, v := range f.(map[string]interface{}) {
90                         switch i {
91                         case tt.key:
92                                 if s, ok := v.(string); !ok || s != tt.value {
93                                         t.Fatalf("Unexpected value: %#q, want %v", s, tt.value)
94                                 }
95                         default:
96                                 t.Fatalf("Unexpected key: %#q, from %#q", i, b)
97                         }
98                 }
99         }
100 }