2010-09-26 20:59:14 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2010, Jim Teeuwen.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
This code is subject to a 1-clause BSD license.
|
|
|
|
The contents of which can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2010-05-26 00:24:44 +00:00
|
|
|
if err := doc.LoadFile("test.xml"); err != nil {
|
2011-01-18 20:31:56 +00:00
|
|
|
t.Error(err.String())
|
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-01-18 20:31:56 +00:00
|
|
|
func TestLoadRemote(t *testing.T) {
|
2010-05-06 03:36:48 +00:00
|
|
|
doc := New()
|
2009-11-25 01:50:06 +00:00
|
|
|
|
2011-01-18 20:31:56 +00:00
|
|
|
if err := doc.LoadUri("http://rss.cnn.com/rss/cnn_latest.rss"); err != nil {
|
|
|
|
t.Error(err.String())
|
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
|
|
|
|
2010-05-26 00:24:44 +00:00
|
|
|
if err := doc.LoadFile("test.xml"); 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 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
|
|
|
|
2010-05-26 00:24:44 +00:00
|
|
|
if err := doc.LoadFile("test.xml"); 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
|
|
|
}
|
|
|
|
|
2010-05-06 03:36:48 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-02 18:38:35 +00:00
|
|
|
type Image struct {
|
2010-05-06 03:36:48 +00:00
|
|
|
Title string
|
|
|
|
Url string
|
|
|
|
Link string
|
|
|
|
Width string
|
|
|
|
Height string
|
|
|
|
Description string
|
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("test.xml")
|
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
|
|
|
}
|
|
|
|
|
2010-05-06 03:36:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|