diff options
author | Raif S. Naffah <go@naffah-raif.name> | 2010-04-18 15:22:36 -0700 |
---|---|---|
committer | Raif S. Naffah <go@naffah-raif.name> | 2010-04-18 15:22:36 -0700 |
commit | 585e169ff2ad541242b78b4a5756b424cf6927e4 (patch) | |
tree | 265701a8b771685a9732ddc4ca29c9b6ea7e23c2 /src/pkg/xml/embed_test.go | |
parent | 90d447a14df68497f355a20e8816aa221dd1208c (diff) | |
download | golang-585e169ff2ad541242b78b4a5756b424cf6927e4.tar.gz |
reflect: add FieldByNameFunc
xml: add support for XML marshalling embedded structs.
R=rsc
CC=golang-dev
http://codereview.appspot.com/837042
Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/xml/embed_test.go')
-rw-r--r-- | src/pkg/xml/embed_test.go | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/pkg/xml/embed_test.go b/src/pkg/xml/embed_test.go new file mode 100644 index 000000000..abfe781ac --- /dev/null +++ b/src/pkg/xml/embed_test.go @@ -0,0 +1,124 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xml + +import "testing" + +type C struct { + Name string + Open bool +} + +type A struct { + XMLName Name "http://domain a" + C + B B + FieldA string +} + +type B struct { + XMLName Name "b" + C + FieldB string +} + +const _1a = ` +<?xml version="1.0" encoding="UTF-8"?> +<a xmlns="http://domain"> + <name>KmlFile</name> + <open>1</open> + <b> + <name>Absolute</name> + <open>0</open> + <fieldb>bar</fieldb> + </b> + <fielda>foo</fielda> +</a> +` + +// Tests that embedded structs are marshalled. +func TestEmbedded1(t *testing.T) { + var a A + if e := Unmarshal(StringReader(_1a), &a); e != nil { + t.Fatalf("Unmarshal: %s", e) + } + if a.FieldA != "foo" { + t.Fatalf("Unmarshal: expected 'foo' but found '%s'", a.FieldA) + } + if a.Name != "KmlFile" { + t.Fatalf("Unmarshal: expected 'KmlFile' but found '%s'", a.Name) + } + if !a.Open { + t.Fatal("Unmarshal: expected 'true' but found otherwise") + } + if a.B.FieldB != "bar" { + t.Fatalf("Unmarshal: expected 'bar' but found '%s'", a.B.FieldB) + } + if a.B.Name != "Absolute" { + t.Fatalf("Unmarshal: expected 'Absolute' but found '%s'", a.B.Name) + } + if a.B.Open { + t.Fatal("Unmarshal: expected 'false' but found otherwise") + } +} + +type A2 struct { + XMLName Name "http://domain a" + XY string + Xy string +} + +const _2a = ` +<?xml version="1.0" encoding="UTF-8"?> +<a xmlns="http://domain"> + <xy>foo</xy> +</a> +` + +// Tests that conflicting field names get excluded. +func TestEmbedded2(t *testing.T) { + var a A2 + if e := Unmarshal(StringReader(_2a), &a); e != nil { + t.Fatalf("Unmarshal: %s", e) + } + if a.XY != "" { + t.Fatalf("Unmarshal: expected empty string but found '%s'", a.XY) + } + if a.Xy != "" { + t.Fatalf("Unmarshal: expected empty string but found '%s'", a.Xy) + } +} + +type A3 struct { + XMLName Name "http://domain a" + xy string +} + +// Tests that private fields are not set. +func TestEmbedded3(t *testing.T) { + var a A3 + if e := Unmarshal(StringReader(_2a), &a); e != nil { + t.Fatalf("Unmarshal: %s", e) + } + if a.xy != "" { + t.Fatalf("Unmarshal: expected empty string but found '%s'", a.xy) + } +} + +type A4 struct { + XMLName Name "http://domain a" + Any string +} + +// Tests that private fields are not set. +func TestEmbedded4(t *testing.T) { + var a A4 + if e := Unmarshal(StringReader(_2a), &a); e != nil { + t.Fatalf("Unmarshal: %s", e) + } + if a.Any != "foo" { + t.Fatalf("Unmarshal: expected 'foo' but found '%s'", a.Any) + } +} |