2011-03-19 13:50:46 +00:00
|
|
|
|
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
|
|
|
// license. Its contents can be found at:
|
|
|
|
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
2010-09-26 20:59:14 +00:00
|
|
|
|
|
2009-11-23 04:16:27 +00:00
|
|
|
|
package xmlx
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
2009-11-25 01:50:06 +00:00
|
|
|
|
func TestLoadLocal(t *testing.T) {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
doc := New()
|
2009-11-23 04:16:27 +00:00
|
|
|
|
|
2012-02-29 10:21:35 +00:00
|
|
|
|
if err := doc.LoadFile("test.xml", nil); err != nil {
|
2011-11-02 15:50:45 +00:00
|
|
|
|
t.Error(err.Error())
|
2010-05-06 03:36:48 +00:00
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(doc.Root.Children) == 0 {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("Root node has no children.")
|
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 03:17:00 +00:00
|
|
|
|
func TestWildcard(t *testing.T) {
|
|
|
|
|
doc := New()
|
|
|
|
|
|
2012-02-29 10:21:35 +00:00
|
|
|
|
if err := doc.LoadFile("test2.xml", nil); err != nil {
|
2011-11-02 15:50:45 +00:00
|
|
|
|
t.Error(err.Error())
|
2011-07-13 03:17:00 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 21:27:00 +00:00
|
|
|
|
list := doc.SelectNodes("ns", "*")
|
|
|
|
|
|
2013-10-25 21:31:10 +00:00
|
|
|
|
if len(list) != 1 {
|
|
|
|
|
t.Errorf("Wrong number of child elements. Expected 1, got %d.", len(list))
|
2012-07-29 19:28:03 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestWildcardRecursive(t *testing.T) {
|
|
|
|
|
doc := New()
|
|
|
|
|
|
|
|
|
|
if err := doc.LoadFile("test2.xml", nil); err != nil {
|
|
|
|
|
t.Error(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list := doc.SelectNodesRecursive("ns", "*")
|
|
|
|
|
|
|
|
|
|
if len(list) != 7 {
|
|
|
|
|
t.Errorf("Wrong number of child elements. Expected 7, got %d.", len(list))
|
2011-07-13 03:17:00 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func _TestLoadRemote(t *testing.T) {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
doc := New()
|
2009-11-25 01:50:06 +00:00
|
|
|
|
|
2012-02-29 10:21:35 +00:00
|
|
|
|
if err := doc.LoadUri("http://blog.golang.org/feeds/posts/default", nil); err != nil {
|
2011-11-02 15:50:45 +00:00
|
|
|
|
t.Error(err.Error())
|
2010-05-06 03:36:48 +00:00
|
|
|
|
return
|
2009-11-25 01:50:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(doc.Root.Children) == 0 {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("Root node has no children.")
|
|
|
|
|
return
|
2009-11-25 01:50:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 04:16:27 +00:00
|
|
|
|
func TestSave(t *testing.T) {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
doc := New()
|
2009-11-23 04:16:27 +00:00
|
|
|
|
|
2012-02-29 10:21:35 +00:00
|
|
|
|
if err := doc.LoadFile("test.xml", nil); err != nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("LoadFile(): %s", err)
|
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-25 22:46:56 +00:00
|
|
|
|
IndentPrefix = "\t"
|
2010-05-26 00:24:44 +00:00
|
|
|
|
if err := doc.SaveFile("test1.xml"); err != nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("SaveFile(): %s", err)
|
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNodeSearch(t *testing.T) {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
doc := New()
|
2009-11-23 04:16:27 +00:00
|
|
|
|
|
2012-02-29 10:21:35 +00:00
|
|
|
|
if err := doc.LoadFile("test1.xml", nil); err != nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("LoadFile(): %s", err)
|
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 00:24:44 +00:00
|
|
|
|
if node := doc.SelectNode("", "item"); node == nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("SelectNode(): No node found.")
|
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 21:27:00 +00:00
|
|
|
|
nodes := doc.SelectNodes("", "item")
|
2009-11-23 04:16:27 +00:00
|
|
|
|
if len(nodes) == 0 {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("SelectNodes(): no nodes found.")
|
|
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
|
}
|
2013-10-25 20:44:32 +00:00
|
|
|
|
|
|
|
|
|
ch := doc.SelectNode("", "channel")
|
2014-01-29 21:27:00 +00:00
|
|
|
|
// Test that SelectNodes properly selects multiple nodes
|
2013-10-25 21:31:10 +00:00
|
|
|
|
links := ch.SelectNodes("", "link")
|
|
|
|
|
if len(links) != 8 {
|
2014-01-29 21:27:00 +00:00
|
|
|
|
t.Errorf("SelectNodes(): Expected 8, Got %d", len(links))
|
2013-10-25 20:44:32 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-02 18:38:35 +00:00
|
|
|
|
type Image struct {
|
2012-02-29 10:42:39 +00:00
|
|
|
|
Title string `xml:"title"`
|
|
|
|
|
Url string `xml:"url"`
|
|
|
|
|
Link string `xml:"link"`
|
|
|
|
|
Description string `xml:"description"`
|
|
|
|
|
Width int `xml:"width"`
|
|
|
|
|
Height int `xml:"height"`
|
2009-12-02 18:38:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
doc := New()
|
2012-02-29 10:21:35 +00:00
|
|
|
|
err := doc.LoadFile("test1.xml", nil)
|
2009-12-02 18:38:35 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("LoadFile(): %s", err)
|
|
|
|
|
return
|
2009-12-02 18:38:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 03:36:48 +00:00
|
|
|
|
node := doc.SelectNode("", "image")
|
2009-12-02 18:38:35 +00:00
|
|
|
|
if node == nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("SelectNode(): No node found.")
|
|
|
|
|
return
|
2009-12-02 18:38:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-29 10:42:39 +00:00
|
|
|
|
var img Image
|
2010-08-22 03:07:38 +00:00
|
|
|
|
if err = node.Unmarshal(&img); err != nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("Unmarshal(): %s", err)
|
|
|
|
|
return
|
2009-12-02 18:38:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if img.Title != "WriteTheWeb" {
|
2010-05-06 03:36:48 +00:00
|
|
|
|
t.Errorf("Image.Title has incorrect value. Got '%s', expected 'WriteTheWeb'.", img.Title)
|
|
|
|
|
return
|
2009-12-02 18:38:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-03 22:35:22 +00:00
|
|
|
|
|
2013-09-10 21:18:13 +00:00
|
|
|
|
func TestStringNamespaces(t *testing.T) {
|
2013-09-03 22:35:22 +00:00
|
|
|
|
doc := New()
|
|
|
|
|
err := doc.LoadFile("test3.xml", nil)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("LoadFile(): %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := `<root xmlns:foo="http:/example.org/foo">
|
2013-09-10 21:18:13 +00:00
|
|
|
|
<child foo:bar="1">
|
|
|
|
|
<grandchild xmlns:foo="">
|
|
|
|
|
<great-grandchild bar="2">
 </great-grandchild>
|
|
|
|
|
</grandchild>
|
|
|
|
|
</child>
|
2013-09-03 22:35:22 +00:00
|
|
|
|
</root>
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
if got := doc.Root.String(); got != expected {
|
|
|
|
|
t.Fatalf("expected: %s\ngot: %s\n", expected, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-10 21:18:13 +00:00
|
|
|
|
|
|
|
|
|
func TestStringEscaping(t *testing.T) {
|
|
|
|
|
doc := New()
|
|
|
|
|
err := doc.LoadFile("test4.xml", nil)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("LoadFile(): %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := `<body>  <https://example.com/file/fm/SU0vRk0xLzIwMTMwOTEwLzA1MDA0MS5ybXdhdGVzdEByZXV0ZXJzLmNvbTEzNzg4NDU1OTk4OTA/Screen%20Shot%202013-09-10%20at%2021.33.54.png> File Attachment:-Screen Shot 2013-09-10 at 21.33.54.png </body>
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
if got := doc.Root.String(); got != expected {
|
|
|
|
|
t.Fatalf("expected: %s\ngot: %s\n", expected, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-30 05:09:47 +00:00
|
|
|
|
|
|
|
|
|
func TestElementNodeValueFetch(t *testing.T) {
|
|
|
|
|
data := `<car><color>
|
|
|
|
|
r<cool />
|
|
|
|
|
ed</color><brand>BMW</brand><price>50
|
|
|
|
|
<cheap />.25</price><count>6
|
|
|
|
|
<small />2
|
|
|
|
|
</count><available>
|
|
|
|
|
Tr
|
|
|
|
|
<found />
|
|
|
|
|
ue</available></car>`
|
|
|
|
|
doc := New()
|
|
|
|
|
|
|
|
|
|
if err := doc.LoadString(data, nil); nil != err {
|
|
|
|
|
t.Fatalf("LoadString(): %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
carN := doc.SelectNode("", "car")
|
|
|
|
|
if v := carN.S("", "brand"); v != "BMW" {
|
|
|
|
|
t.Errorf("Failed to get brand as string, got: '%s', wanted: 'BMW'", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.S("", "color"); v != "red" {
|
|
|
|
|
t.Errorf("Failed to get color as string, got: '%s', wanted: 'red'", v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v := carN.I("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using I, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.I8("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using I8, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.I16("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using I16, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.I32("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using I32, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.I64("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using I64, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.U("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using U, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.U8("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using U8, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.U16("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using U16, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.U32("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using U32, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.U64("", "count"); v != 62 {
|
|
|
|
|
t.Errorf("Failed to get count using U64, got: %v, wanted: 62", v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v := carN.F32("", "price"); v != 50.25 {
|
|
|
|
|
t.Errorf("Failed to get price using F32, got: %v, wanted: 50.25", v)
|
|
|
|
|
}
|
|
|
|
|
if v := carN.F64("", "price"); v != 50.25 {
|
|
|
|
|
t.Errorf("Failed to get price using F64, got: %v, wanted: 50.25", v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if v := carN.B("", "available"); v != true {
|
|
|
|
|
t.Errorf("Failed to get availability using B, got: %v, wanted: true", v)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-05 16:13:46 +00:00
|
|
|
|
|
|
|
|
|
func TestElementNodeValueFetchAndSet(t *testing.T) {
|
|
|
|
|
IndentPrefix = ""
|
|
|
|
|
|
|
|
|
|
data := `<car><color>
|
|
|
|
|
r<cool />
|
|
|
|
|
ed</color><brand>BM<awesome />W</brand><price>50
|
|
|
|
|
<cheap />.25</price><count>6
|
|
|
|
|
<small />2
|
|
|
|
|
</count><available>
|
|
|
|
|
Tr
|
|
|
|
|
<found />
|
|
|
|
|
ue</available></car>`
|
|
|
|
|
doc := New()
|
|
|
|
|
|
|
|
|
|
if err := doc.LoadString(data, nil); nil != err {
|
|
|
|
|
t.Fatalf("LoadString(): %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
carN := doc.SelectNode("", "car")
|
|
|
|
|
if carN == nil {
|
|
|
|
|
t.Fatalf("Failed to get the car, got nil, wanted Node{car}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
colorNode := carN.SelectNode("", "color")
|
|
|
|
|
if colorNode == nil {
|
|
|
|
|
t.Fatalf("Failed to get the color, got nil, wanted Node{color}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
colorVal := colorNode.GetValue()
|
|
|
|
|
if colorVal != "red" {
|
|
|
|
|
t.Fatalf("Failed to get the color, got %v, wanted 'red'", colorVal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
colorNode.SetValue("blue")
|
|
|
|
|
|
|
|
|
|
expected := `<car><color>blue</color><brand>BM<awesome />W</brand><price>50
|
|
|
|
|
<cheap />.25</price><count>6
|
|
|
|
|
<small />2
|
|
|
|
|
</count><available>
|
|
|
|
|
Tr
|
|
|
|
|
<found />
|
|
|
|
|
ue</available></car>`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if got := doc.Root.String(); got != expected {
|
|
|
|
|
t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// now set the brand
|
|
|
|
|
brandNode := carN.SelectNode("", "brand")
|
|
|
|
|
if brandNode == nil {
|
|
|
|
|
t.Fatalf("Failed to get the color, got nil, wanted Node{brand}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
brandVal := brandNode.GetValue()
|
|
|
|
|
if brandVal != "BMW" {
|
|
|
|
|
t.Fatalf("Failed to get the brand, got %v, wanted 'BMW'", brandVal)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
brandNode.SetValue("Trabant")
|
|
|
|
|
|
|
|
|
|
// Notice, we lose the <awesome /> tag in BMW, that's intentional
|
|
|
|
|
expected = `<car><color>blue</color><brand>Trabant</brand><price>50
|
|
|
|
|
<cheap />.25</price><count>6
|
|
|
|
|
<small />2
|
|
|
|
|
</count><available>
|
|
|
|
|
Tr
|
|
|
|
|
<found />
|
|
|
|
|
ue</available></car>`
|
|
|
|
|
|
|
|
|
|
if got := doc.Root.String(); got != expected {
|
|
|
|
|
t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
|
|
|
|
|
}
|
|
|
|
|
}
|