- Fix parser not producing the correct results - Add tests for everything - Add readme with example Ready for v1
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package treeificator
|
|
|
|
import "testing"
|
|
|
|
type testInformer struct{}
|
|
|
|
func (testinformer *testInformer) GetPrefix() string {
|
|
return "<p>"
|
|
}
|
|
|
|
func (testinformer *testInformer) GetSuffix() string {
|
|
return "</p>"
|
|
}
|
|
|
|
func (testinformer *testInformer) GetName() string {
|
|
return "tester"
|
|
}
|
|
|
|
const testString = "some<p>sitrn</p>for testing"
|
|
|
|
func TestMarshalOnlyRaw(t *testing.T) {
|
|
res := Marshal(testString)
|
|
if l := len(res.Elements); l != 1 {
|
|
t.Fatalf("Expected to have one element, got %v", l)
|
|
}
|
|
if res.NodeType.GetName() != "default" {
|
|
t.Fatalf("Expected root informer to be default, got %v instead", res.NodeType.GetName())
|
|
}
|
|
elem := res.Elements[0]
|
|
if elem.Text == nil {
|
|
t.Fatalf("Expected one raw string element with test string, got nil")
|
|
}
|
|
if *elem.Text != testString {
|
|
t.Fatalf("Expected one raw string element with test string, got %#v", *elem.Text)
|
|
}
|
|
}
|
|
|
|
func TestMarshalWithInformer(t *testing.T) {
|
|
res := Marshal(testString, &testInformer{})
|
|
if len(res.Elements) != 3 {
|
|
t.Fatalf("Expected 3 elements (text, node, text), got %#v instead", res.Elements)
|
|
}
|
|
raw1 := res.Elements[0]
|
|
subElem := res.Elements[1]
|
|
raw2 := res.Elements[2]
|
|
if raw1.Text == nil || *raw1.Text != "some" {
|
|
t.Fatalf("Expected first element to be raw text \"some\", got %#v", raw1)
|
|
}
|
|
if raw2.Text == nil || *raw2.Text != "for testing" {
|
|
t.Fatalf("Expected last element to be raw text \"for testing\", got %#v", *raw2.Text)
|
|
}
|
|
if subElem.Node == nil {
|
|
t.Fatalf("Expected 2nd element to be a node, got %#v", subElem)
|
|
}
|
|
n := subElem.Node
|
|
if name := n.NodeType.GetName(); name != "tester" {
|
|
t.Fatalf("Expected 2nd element node type name to be \"tester\", got %#v", name)
|
|
}
|
|
if len(n.Elements) != 1 {
|
|
t.Fatalf("Expected 2nd element to have one element, got %#v", n.Elements)
|
|
}
|
|
ns := n.Elements[0]
|
|
if ns.Text == nil || *ns.Text != "sitrn" {
|
|
t.Fatalf("Expected 2nd element to have one raw text of \"sitrn\", got %#v", ns.Text)
|
|
}
|
|
}
|