~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

Viewing changes to src/pkg/encoding/json/encode_test.go

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
186
186
                t.Errorf("got %q, want %q", got, want)
187
187
        }
188
188
}
 
189
 
 
190
type IntType int
 
191
 
 
192
type MyStruct struct {
 
193
        IntType
 
194
}
 
195
 
 
196
func TestAnonymousNonstruct(t *testing.T) {
 
197
        var i IntType = 11
 
198
        a := MyStruct{i}
 
199
        const want = `{"IntType":11}`
 
200
 
 
201
        b, err := Marshal(a)
 
202
        if err != nil {
 
203
                t.Fatalf("Marshal: %v", err)
 
204
        }
 
205
        if got := string(b); got != want {
 
206
                t.Errorf("got %q, want %q", got, want)
 
207
        }
 
208
}
 
209
 
 
210
type BugA struct {
 
211
        S string
 
212
}
 
213
 
 
214
type BugB struct {
 
215
        BugA
 
216
        S string
 
217
}
 
218
 
 
219
type BugC struct {
 
220
        S string
 
221
}
 
222
 
 
223
// Legal Go: We never use the repeated embedded field (S).
 
224
type BugX struct {
 
225
        A int
 
226
        BugA
 
227
        BugB
 
228
}
 
229
 
 
230
// Issue 5245.
 
231
func TestEmbeddedBug(t *testing.T) {
 
232
        v := BugB{
 
233
                BugA{"A"},
 
234
                "B",
 
235
        }
 
236
        b, err := Marshal(v)
 
237
        if err != nil {
 
238
                t.Fatal("Marshal:", err)
 
239
        }
 
240
        want := `{"S":"B"}`
 
241
        got := string(b)
 
242
        if got != want {
 
243
                t.Fatalf("Marshal: got %s want %s", got, want)
 
244
        }
 
245
        // Now check that the duplicate field, S, does not appear.
 
246
        x := BugX{
 
247
                A: 23,
 
248
        }
 
249
        b, err = Marshal(x)
 
250
        if err != nil {
 
251
                t.Fatal("Marshal:", err)
 
252
        }
 
253
        want = `{"A":23}`
 
254
        got = string(b)
 
255
        if got != want {
 
256
                t.Fatalf("Marshal: got %s want %s", got, want)
 
257
        }
 
258
}
 
259
 
 
260
type BugD struct { // Same as BugA after tagging.
 
261
        XXX string `json:"S"`
 
262
}
 
263
 
 
264
// BugD's tagged S field should dominate BugA's.
 
265
type BugY struct {
 
266
        BugA
 
267
        BugD
 
268
}
 
269
 
 
270
// Test that a field with a tag dominates untagged fields.
 
271
func TestTaggedFieldDominates(t *testing.T) {
 
272
        v := BugY{
 
273
                BugA{"BugA"},
 
274
                BugD{"BugD"},
 
275
        }
 
276
        b, err := Marshal(v)
 
277
        if err != nil {
 
278
                t.Fatal("Marshal:", err)
 
279
        }
 
280
        want := `{"S":"BugD"}`
 
281
        got := string(b)
 
282
        if got != want {
 
283
                t.Fatalf("Marshal: got %s want %s", got, want)
 
284
        }
 
285
}
 
286
 
 
287
// There are no tags here, so S should not appear.
 
288
type BugZ struct {
 
289
        BugA
 
290
        BugC
 
291
        BugY // Contains a tagged S field through BugD; should not dominate.
 
292
}
 
293
 
 
294
func TestDuplicatedFieldDisappears(t *testing.T) {
 
295
        v := BugZ{
 
296
                BugA{"BugA"},
 
297
                BugC{"BugC"},
 
298
                BugY{
 
299
                        BugA{"nested BugA"},
 
300
                        BugD{"nested BugD"},
 
301
                },
 
302
        }
 
303
        b, err := Marshal(v)
 
304
        if err != nil {
 
305
                t.Fatal("Marshal:", err)
 
306
        }
 
307
        want := `{}`
 
308
        got := string(b)
 
309
        if got != want {
 
310
                t.Fatalf("Marshal: got %s want %s", got, want)
 
311
        }
 
312
}