summaryrefslogtreecommitdiff
path: root/libgo/go/xml/embed_test.go
blob: abfe781acdf18a8a453e39bded11128bda843d35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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)
	}
}