PageRenderTime 37ms CodeModel.GetById 17ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/html/transform/transform_test.go

https://code.google.com/p/go-html-transform/
Go | 211 lines | 183 code | 21 blank | 7 comment | 15 complexity | 41312ac61c663cc46db08c96881263fd MD5 | raw file
  1/*
  2 Copyright 2010 Jeremy Wall (jeremy@marzhillstudios.com)
  3 Use of this source code is governed by the Artistic License 2.0.
  4 That License is included in the LICENSE file.
  5*/
  6package transform
  7
  8import (
  9	"code.google.com/p/go-html-transform/h5"
 10	"testing"
 11)
 12
 13func assertEqual(t *testing.T, val interface{}, expected interface{}) {
 14	if val != expected {
 15		t.Errorf("NotEqual Expected: [%s] Actual: [%s]",
 16			expected, val)
 17	}
 18}
 19
 20func assertNotNil(t *testing.T, val interface{}) {
 21	if val == nil {
 22		t.Errorf("Value is Nil")
 23	}
 24}
 25
 26func TestNewTransformer(t *testing.T) {
 27	tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html>")
 28	tf := New(tree)
 29	// hacky way of comparing an uncomparable type
 30	assertEqual(t, tf.Doc().Type, tree.Top().Type)
 31}
 32
 33func TestTransformApply(t *testing.T) {
 34	tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html>")
 35	tf := New(tree)
 36	n := h5.Text("bar")
 37	tf.Apply(AppendChildren(n), "body")
 38	newDoc := tf.String()
 39	assertEqual(t, newDoc, "<html><head></head><body><div id=\"foo\"></div>bar</body></html>")
 40}
 41
 42func TestTransformApplyAll(t *testing.T) {
 43	tree, _ := h5.NewFromString("<html><head></head><body><ul><li>foo</ul></body></html>")
 44	tf := New(tree)
 45	n := h5.Text("bar")
 46	n2 := h5.Text("quux")
 47	t1, _ := Trans(AppendChildren(n), "body li")
 48	t2, _ := Trans(AppendChildren(n2), "body li")
 49	tf.ApplyAll(t1, t2)
 50	assertEqual(t, tf.String(), "<html><head></head><body><ul><li>foobarquux</li></ul></body></html>")
 51}
 52
 53func TestTransformApplyMulti(t *testing.T) {
 54	tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html>")
 55	tf := New(tree)
 56	tf.Apply(AppendChildren(h5.Text("")), "body")
 57	tf.Apply(TransformAttrib("id", func(val string) string {
 58		t.Logf("Rewriting Url")
 59		return "bar"
 60	}),
 61		"div")
 62	newDoc := tf.String()
 63	assertEqual(t, newDoc, "<html><head></head><body><div id=\"bar\"></div></body></html>")
 64}
 65
 66func TestAppendChildren(t *testing.T) {
 67	node := h5.Anchor("", "")
 68	child := h5.Text("foo ")
 69	child2 := h5.Text("bar")
 70	AppendChildren(child, child2)(node)
 71	assertEqual(t, h5.NewTree(node).String(), "<a>foo bar</a>")
 72}
 73
 74func TestRemoveChildren(t *testing.T) {
 75	node := h5.Anchor("", "foo")
 76	RemoveChildren()(node)
 77	assertEqual(t, h5.NewTree(node).String(), "<a></a>")
 78}
 79
 80func TestReplaceChildren(t *testing.T) {
 81	node := h5.Anchor("", "foo")
 82	assertEqual(t, h5.NewTree(node).String(), "<a>foo</a>")
 83	child := h5.Text("baz ")
 84	child2 := h5.Text("quux")
 85	ReplaceChildren(child, child2)(node)
 86	assertEqual(t, h5.NewTree(node).String(), "<a>baz quux</a>")
 87}
 88
 89func TestReplace(t *testing.T) {
 90	defer func() {
 91		if err := recover(); err != nil {
 92			t.Error("TestReplace paniced")
 93		}
 94	}()
 95	node := h5.Div("", nil, h5.Div("", nil, h5.Text("foo")))
 96	replacement := h5.Div("", nil, h5.Text("bar"))
 97	Replace(replacement)(node.FirstChild)
 98	assertEqual(t, h5.NewTree(node).String(),
 99		"<div><div>bar</div></div>")
100}
101
102func TestReplaceSplice(t *testing.T) {
103	defer func() {
104		if err := recover(); err != nil {
105			t.Error("TestReplaceSplice paniced")
106		}
107	}()
108	node := h5.Div("foo", nil,
109		h5.Text("foo"),
110		h5.Element("span", nil, h5.Text("bar")),
111	)
112	node2 := h5.Element("span", nil, h5.Text("foo"))
113	Replace(node2)(node.FirstChild)
114	assertEqual(t, h5.NewTree(node).String(),
115		"<div id=\"foo\"><span>foo</span><span>bar</span></div>")
116}
117
118func TestReplaceSpliceOnRootNode(t *testing.T) {
119	defer func() {
120		if err := recover(); err == nil {
121			t.Error("TestReplaceSpliceOnRootNode didn't panic")
122		}
123	}()
124	tree, _ := h5.NewFromString("<div id=\"foo\">foo<span>bar</span></div><")
125	doc := tree.Top()
126	ns, _ := h5.NewFromString("<span>foo</span>")
127	f := Replace(ns.Top())
128	f(doc)
129	assertEqual(t, h5.Data(doc.FirstChild), "span")
130	assertEqual(t, h5.Data(doc.FirstChild.FirstChild), "foo")
131}
132
133func TestModifyAttrib(t *testing.T) {
134	node := h5.Anchor("", "")
135	ModifyAttrib("id", "bar")(node)
136	assertEqual(t, node.Attr[0].Val, "bar")
137	ModifyAttrib("class", "baz")(node)
138	assertEqual(t, node.Attr[1].Key, "class")
139	assertEqual(t, node.Attr[1].Val, "baz")
140}
141
142func TestTransformAttrib(t *testing.T) {
143	node := h5.Anchor("", "")
144	ModifyAttrib("id", "foo")(node)
145	assertEqual(t, node.Attr[0].Val, "foo")
146	TransformAttrib("id", func(s string) string { return "bar" })(node)
147	assertEqual(t, node.Attr[0].Val, "bar")
148}
149
150func TestDoAll(t *testing.T) {
151	tree, _ := h5.NewFromString("<div id=\"foo\">foo</div><")
152	node := tree.Top()
153	preNode := h5.Text("pre node")
154	postNode := h5.Text("post node")
155	f := DoAll(AppendChildren(postNode),
156		PrependChildren(preNode))
157	f(node)
158	assertEqual(t, h5.Data(node.FirstChild), h5.Data(preNode))
159	assertEqual(t, h5.Data(node.LastChild), h5.Data(postNode))
160}
161
162func TestCopyAnd(t *testing.T) {
163	defer func() {
164		if err := recover(); err != nil {
165			t.Errorf("TestCopyAnd paniced %s", err)
166		}
167	}()
168	node := h5.Div("", nil, h5.Div("", nil, h5.Text("foo")))
169	assertEqual(t, h5.NewTree(node).String(),
170		"<div><div>foo</div></div>")
171	CopyAnd(
172		AppendChildren(h5.Text("bar")),
173		ReplaceChildren(h5.Text("baz")),
174	)(node.FirstChild)
175	assertEqual(t, h5.NewTree(node).String(),
176		"<div><div>foobar</div><div>baz</div></div>")
177}
178
179func TestTransformSubtransforms(t *testing.T) {
180	defer func() {
181		if err := recover(); err != nil {
182			t.Errorf("TestTransformSubtransforms paniced %s", err)
183		}
184	}()
185	tree, _ := h5.NewFromString("<html><body><ul><li>foo</ul></body></html>")
186
187	f, _ := Subtransform(CopyAnd(
188		ReplaceChildren(h5.Text("bar")),
189		ReplaceChildren(h5.Text("baz"), h5.Text("quux")),
190	), "li")
191	tf := New(tree)
192	t1, _ := Trans(f, "ul")
193	tf.ApplyAll(t1)
194	assertEqual(t, tf.String(),
195		"<html><head></head><body><ul><li>bar</li><li>bazquux</li></ul></body></html>")
196
197}
198
199// TODO(jwall): benchmarking tests
200func BenchmarkTransformApply(b *testing.B) {
201	for i := 0; i < b.N; i++ {
202		tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html")
203		tf := New(tree)
204		tf.Apply(AppendChildren(h5.Text("")), "body")
205		tf.Apply(TransformAttrib("id", func(val string) string {
206			return "bar"
207		}),
208			"div")
209		tf.Doc()
210	}
211}