Debian Codegraph @

go/pkg/amqp/types_test.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package amqp
21
22 import (
23 "fmt"
24 "reflect"
25 "testing"
26 "time"
27
28 "github.com/apache/qpid-proton/go/pkg/internal/test"
29 )
30
31 func checkUnmarshal(marshaled []byte, v interface{}) error {
32 got, err := Unmarshal(marshaled, v)
33 if err != nil {
34 return err
35 }
36 if got != len(marshaled) {
37 return fmt.Errorf("Wanted to Unmarshal %v bytes, got %v", len(marshaled), got)
38 }
39 return nil
40 }
41
42 func ExampleAnnotationKey() {
43 var k AnnotationKey = AnnotationKeySymbol(Symbol("foo"))
44 fmt.Println(k.Get().(Symbol))
45 k = AnnotationKeyUint64(42)
46 fmt.Println(k.Get().(uint64))
47 // Output:
48 // foo
49 // 42
50 }
51
52 var timeValue = time.Now().Round(time.Millisecond)
53
54 // Values that are unchanged by a marshal/unmarshal round-trip from interface{}
55 // to interface{}
56 var rtValues = []interface{}{
57 true,
58 int8(-8), int16(-16), int32(-32), int64(-64),
59 uint8(8), uint16(16), uint32(32), uint64(64),
60 float32(0.32), float64(0.64),
61 "string", Binary("Binary"), Symbol("symbol"),
62 nil,
63 Described{"D", "V"},
64 timeValue,
65 UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
66 Char('a'),
67 Map{"V": "X"},
68 Map{},
69 List{"V", int32(1)},
70 List{},
71 []string{"a", "b", "c"},
72 []int8{},
73 AnyMap{{[]int8{1, 2, 3}, "bad-key"}, {int16(1), "duplicate-1"}, {int16(1), "duplicate-2"}},
74 }
75
76 // Go values that round-trip if unmarshalled back to the same type they were
77 // marshalled from, but unmarshal to interface{} as a different default type.
78 var oddValues = []interface{}{
79 int(-99), // int32|64 depending on platform
80 uint(99), // int32|64 depending on platform
81 []byte("byte"), // amqp.Binary
82 map[string]int{"str": 99}, // amqp.Map
83 []Map{Map{}}, // amqp.Array - the generic array
84 AnyMap(nil), // Map
85 }
86
87 var allValues = append(rtValues, oddValues...)
88
89 // %v formatted representation of allValues
90 var vstrings = []string{
91 // for rtValues
92 "true",
93 "-8", "-16", "-32", "-64",
94 "8", "16", "32", "64",
95 "0.32", "0.64",
96 "string", "Binary", "symbol",
97 "<nil>",
98 "{D V}",
99 fmt.Sprintf("%v", timeValue),
100 "UUID(01020304-0506-0708-090a-0b0c0d0e0f10)",
101 fmt.Sprintf("%v", 'a'),
102 "map[V:X]",
103 "map[]",
104 "[V 1]",
105 "[]",
106 "[a b c]",
107 "[]",
108 "[{[1 2 3] bad-key} {1 duplicate-1} {1 duplicate-2}]",
109 // for oddValues
110 "-99", "99",
111 "[98 121 116 101]", /*"byte"*/
112 "map[str:99]",
113 "[map[]]",
114 "[]",
115 }
116
117 // Round-trip encoding test
118 func TestTypesRoundTrip(t *testing.T) {
119 for _, x := range rtValues {
120 marshalled, err := Marshal(x, nil)
121 if err != nil {
122 t.Error(err)
123 }
124 var v interface{}
125 if err := checkUnmarshal(marshalled, &v); err != nil {
126 t.Error(err)
127 }
128 if err := test.Differ(x, v); err != nil {
129 t.Error(err)
130 }
131 }
132 }
133
134 // Round trip from T to T where T is the type of the value.
135 func TestTypesRoundTripAll(t *testing.T) {
136 for _, x := range allValues {
137 marshaled, err := Marshal(x, nil)
138 if err != nil {
139 t.Error(err)
140 }
141 if x == nil { // We can't create an instance of nil to unmarshal to.
142 continue
143 }
144 vp := reflect.New(reflect.TypeOf(x)) // v points to a Zero of the same type as x
145 if err := checkUnmarshal(marshaled, vp.Interface()); err != nil {
146 t.Error(err)
147 }
148 v := vp.Elem().Interface()
149 if err := test.Differ(x, v); err != nil {
150 t.Error(err)
151 }
152 }
153 }
154
155 func TestTypesPrint(t *testing.T) {
156 // Default %v representations of rtValues and oddValues
157 for i, x := range allValues {
158 if s := fmt.Sprintf("%v", x); vstrings[i] != s {
159 t.Errorf("printing %T: want %v, got %v", x, vstrings[i], s)
160 }
161 }
162 }
163
164 func TestDescribed(t *testing.T) {
165 want := Described{"D", "V"}
166 marshaled, _ := Marshal(want, nil)
167
168 // Unmarshal to Described type
169 var d Described
170 if err := checkUnmarshal(marshaled, &d); err != nil {
171 t.Error(err)
172 }
173 if err := test.Differ(want, d); err != nil {
174 t.Error(err)
175 }
176
177 // Unmarshal to interface{}
178 var i interface{}
179 if err := checkUnmarshal(marshaled, &i); err != nil {
180 t.Error(err)
181 }
182 if _, ok := i.(Described); !ok {
183 t.Errorf("Expected Described, got %T(%v)", i, i)
184 }
185 if err := test.Differ(want, i); err != nil {
186 t.Error(err)
187 }
188
189 // Unmarshal value only (drop descriptor) to the value type
190 var s string
191 if err := checkUnmarshal(marshaled, &s); err != nil {
192 t.Error(err)
193 }
194 if err := test.Differ(want.Value, s); err != nil {
195 t.Error(err)
196 }
197 }
198
199 func TestTimeConversion(t *testing.T) {
200 pt := pnTime(timeValue)
201 if err := test.Differ(timeValue, goTime(pt)); err != nil {
202 t.Error(err)
203 }
204 if err := test.Differ(pt, pnTime(goTime(pt))); err != nil {
205 t.Error(err)
206 }
207 ut := time.Unix(123, 456*1000000)
208 if err := test.Differ(123456, int(pnTime(ut))); err != nil {
209 t.Error(err)
210 }
211 if err := test.Differ(ut, goTime(123456)); err != nil {
212 t.Error(err)
213 }
214
215 // Preserve zero values
216 var tz time.Time
217 if err := test.Differ(0, int(pnTime(tz))); err != nil {
218 t.Error(err)
219 }
220 if err := test.Differ(tz, goTime(pnTime(tz))); err != nil {
221 t.Error(err)
222 }
223 }