/html/transform/transform_test.go

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