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) {
|
2009-11-23 04:16:27 +00:00
|
|
|
doc := New();
|
|
|
|
err := doc.LoadFile("test.xml");
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(doc.Root.Children) == 0 {
|
2009-11-23 04:49:44 +00:00
|
|
|
t.Errorf("Root node has no children.");
|
2009-11-23 04:16:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-25 01:50:06 +00:00
|
|
|
func TestLoadRemote(t *testing.T) {
|
|
|
|
doc := New();
|
|
|
|
err := doc.LoadUri("http://tldp.org/authors/template/Sample-HOWTO.xml");
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(doc.Root.Children) == 0 {
|
|
|
|
t.Errorf("Root node has no children.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-23 04:16:27 +00:00
|
|
|
func TestSave(t *testing.T) {
|
|
|
|
doc := New();
|
|
|
|
err := doc.LoadFile("test.xml");
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("LoadFile(): %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = doc.SaveFile("test1.xml");
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("SaveFile(): %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeSearch(t *testing.T) {
|
|
|
|
doc := New();
|
|
|
|
err := doc.LoadFile("test.xml");
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("LoadFile(): %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node := doc.SelectNode("", "item");
|
|
|
|
if node == nil {
|
|
|
|
t.Errorf("SelectNode(): No node found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := doc.SelectNodes("", "item");
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
t.Errorf("SelectNodes(): no nodes found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-02 18:38:35 +00:00
|
|
|
type Image struct {
|
|
|
|
Title string;
|
|
|
|
Url string;
|
|
|
|
Link string;
|
|
|
|
Width string;
|
|
|
|
Height string;
|
|
|
|
Description string;
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
doc := New();
|
|
|
|
err := doc.LoadFile("test.xml");
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("LoadFile(): %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node := doc.SelectNode("", "image");
|
|
|
|
if node == nil {
|
|
|
|
t.Errorf("SelectNode(): No node found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
img := Image{};
|
|
|
|
err = node.Unmarshal(&img);
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unmarshal(): %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if img.Title != "WriteTheWeb" {
|
|
|
|
t.Errorf("Image.Title has incorrect value. Got '%s', expected 'WriteTheWeb'.", img.Title);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|