Fixed a needless conversion to byte and string.
Added comment to show the purpose. Added testcase.
This commit is contained in:
parent
17e1b69620
commit
305e0dc0da
4
node.go
4
node.go
|
@ -66,9 +66,11 @@ func (this *Node) GetValue() string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetValue sets the value of the node to the given parameter.
|
||||||
|
// It deletes all children of the node so the old data does not get back at node.GetValue
|
||||||
func (this *Node) SetValue(val string) {
|
func (this *Node) SetValue(val string) {
|
||||||
t := NewNode(NT_TEXT)
|
t := NewNode(NT_TEXT)
|
||||||
t.Value = string([]byte(val))
|
t.Value = val
|
||||||
t.Parent = this
|
t.Parent = this
|
||||||
this.Children = []*Node{t} // brutally replace all other children
|
this.Children = []*Node{t} // brutally replace all other children
|
||||||
}
|
}
|
||||||
|
|
44
xmlx_test.go
44
xmlx_test.go
|
@ -4,7 +4,10 @@
|
||||||
|
|
||||||
package xmlx
|
package xmlx
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
"encoding/xml"
|
||||||
|
)
|
||||||
|
|
||||||
func TestLoadLocal(t *testing.T) {
|
func TestLoadLocal(t *testing.T) {
|
||||||
doc := New()
|
doc := New()
|
||||||
|
@ -252,6 +255,45 @@ func TestElementNodeValueFetch(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// node.SetValue(x); x == node.GetValue
|
||||||
|
func TestElementNodeValueFetchAndSetIdentity (t *testing.T) {
|
||||||
|
// Setup: <root><text>xyzzy</text></root>
|
||||||
|
// The xmlx parser creates a nameless NT_TEXT node containing the value 'xyzzy'
|
||||||
|
rootN := NewNode(NT_ROOT)
|
||||||
|
rootN.Name = xml.Name{Space: "", Local: "root"}
|
||||||
|
textN := NewNode(NT_ELEMENT)
|
||||||
|
textN.Name = xml.Name{Space: "", Local: "text"}
|
||||||
|
namelessN := NewNode(NT_TEXT)
|
||||||
|
namelessN.Value = "xyzzy"
|
||||||
|
rootN.AddChild(textN)
|
||||||
|
textN.AddChild(namelessN)
|
||||||
|
|
||||||
|
targetN := rootN.SelectNode("", "text") // selects textN
|
||||||
|
if (targetN != textN) {
|
||||||
|
t.Errorf("Failed to get the correct textN, got %#v", targetN)
|
||||||
|
}
|
||||||
|
|
||||||
|
// targetN.Value is empty (as the value lives in the childNode)
|
||||||
|
if (targetN.Value != "") {
|
||||||
|
t.Errorf("Failed to prepare correctly, TargetN.Value is not empty, it contains %#v", targetN.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test correct retrieval
|
||||||
|
if v := rootN.S("", "text"); v != "xyzzy" {
|
||||||
|
t.Errorf("Failed to get value as string, got: '%s', wanted: 'xyzzy'", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the value of the nameless child
|
||||||
|
targetN.SetValue("plugh")
|
||||||
|
|
||||||
|
// Test correct retrieval
|
||||||
|
if v := rootN.S("", "text"); v != "plugh" {
|
||||||
|
t.Errorf("Failed to get value as string, got: '%s', wanted: 'plugh'", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test as it could be used to read in a XML file, change some values and write it out again.
|
||||||
|
// For example, a HTML/XML proxy service.
|
||||||
func TestElementNodeValueFetchAndSet(t *testing.T) {
|
func TestElementNodeValueFetchAndSet(t *testing.T) {
|
||||||
IndentPrefix = ""
|
IndentPrefix = ""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue