xmlx/xmlx_test.go

122 lines
2.3 KiB
Go
Raw Normal View History

// 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/
package xmlx
import "testing"
func TestLoadLocal(t *testing.T) {
2010-05-06 03:36:48 +00:00
doc := New()
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
}
if len(doc.Root.Children) == 0 {
2010-05-06 03:36:48 +00:00
t.Errorf("Root node has no children.")
return
}
}
func TestWildcard(t *testing.T) {
doc := New()
if err := doc.LoadFile("test2.xml", nil); err != nil {
2011-11-02 15:50:45 +00:00
t.Error(err.Error())
return
}
list := doc.SelectNodes("ns", "*")
if len(list) != 1 {
t.Errorf("Wrong number of child elements. Expected 4, got %d.", len(list))
return
}
}
func _TestLoadRemote(t *testing.T) {
2010-05-06 03:36:48 +00:00
doc := New()
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
}
if len(doc.Root.Children) == 0 {
2010-05-06 03:36:48 +00:00
t.Errorf("Root node has no children.")
return
}
}
func TestSave(t *testing.T) {
2010-05-06 03:36:48 +00:00
doc := New()
if err := doc.LoadFile("test.xml", nil); err != nil {
2010-05-06 03:36:48 +00:00
t.Errorf("LoadFile(): %s", err)
return
}
if err := doc.SaveFile("test1.xml"); err != nil {
2010-05-06 03:36:48 +00:00
t.Errorf("SaveFile(): %s", err)
return
}
}
func TestNodeSearch(t *testing.T) {
2010-05-06 03:36:48 +00:00
doc := New()
if err := doc.LoadFile("test1.xml", nil); err != nil {
2010-05-06 03:36:48 +00:00
t.Errorf("LoadFile(): %s", err)
return
}
if node := doc.SelectNode("", "item"); node == nil {
2010-05-06 03:36:48 +00:00
t.Errorf("SelectNode(): No node found.")
return
}
2010-05-06 03:36:48 +00:00
nodes := doc.SelectNodes("", "item")
if len(nodes) == 0 {
2010-05-06 03:36:48 +00:00
t.Errorf("SelectNodes(): no nodes found.")
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()
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
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
}
}