Debian Codegraph @

tagkey_test.go

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