OSDN Git Service

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