/pdfkit/ruby/1.8/gems/hpricot-0.8.4/test/test_alter.rb

https://github.com/itguy51/Feed-Aggregator · Ruby · 96 lines · 79 code · 15 blank · 2 comment · 4 complexity · 9ff06534118ae5400d1307fefc608043 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env ruby
  3. require 'test/unit'
  4. require 'hpricot'
  5. require 'load_files'
  6. class TestAlter < Test::Unit::TestCase
  7. def setup
  8. @basic = Hpricot.parse(TestFiles::BASIC)
  9. end
  10. def test_before
  11. test0 = "<link rel='stylesheet' href='test0.css' />"
  12. @basic.at("link").before(test0)
  13. assert_equal 'test0.css', @basic.at("link").attributes['href']
  14. end
  15. def test_after
  16. test_inf = "<link rel='stylesheet' href='test_inf.css' />"
  17. @basic.search("link")[-1].after(test_inf)
  18. assert_equal 'test_inf.css', @basic.search("link")[-1].attributes['href']
  19. end
  20. def test_wrap
  21. ohmy = (@basic/"p.ohmy").wrap("<div id='wrapper'></div>")
  22. assert_equal 'wrapper', ohmy[0].parent['id']
  23. assert_equal 'ohmy', Hpricot(@basic.to_html).at("#wrapper").children[0]['class']
  24. end
  25. def test_add_class
  26. first_p = (@basic/"p:first").add_class("testing123")
  27. assert first_p[0].get_attribute("class").split(" ").include?("testing123")
  28. assert (Hpricot(@basic.to_html)/"p:first")[0].attributes["class"].split(" ").include?("testing123")
  29. assert !(Hpricot(@basic.to_html)/"p:gt(0)")[0].attributes["class"].split(" ").include?("testing123")
  30. end
  31. def test_change_attributes
  32. all_ps = (@basic/"p").attr("title", "Some Title & Etc…")
  33. all_as = (@basic/"a").attr("href", "http://my_new_href.com")
  34. all_lb = (@basic/"link").attr("href") { |e| e.name }
  35. assert_changed(@basic, "p", all_ps) {|p| p.raw_attributes["title"] == "Some Title &amp; Etc&#8230;"}
  36. assert_changed(@basic, "a", all_as) {|a| a.attributes["href"] == "http://my_new_href.com"}
  37. assert_changed(@basic, "link", all_lb) {|a| a.attributes["href"] == "link" }
  38. end
  39. def test_change_attributes2
  40. all_as = (@basic%"a").attributes["href"] = "http://my_new_href.com"
  41. all_ps = (@basic%"p").attributes["title"] = "Some Title & Etc…"
  42. assert_equal (@basic%"a").raw_attributes["href"], "http://my_new_href.com"
  43. assert_equal (@basic%"p").raw_attributes["title"], "Some Title &amp; Etc&#8230;"
  44. assert_equal (@basic%"p").attributes["title"], "Some Title & Etc…"
  45. end
  46. def test_remove_attr
  47. all_rl = (@basic/"link").remove_attr("href")
  48. assert_changed(@basic, "link", all_rl) { |link| link['href'].nil? }
  49. end
  50. def test_remove_class
  51. all_c1 = (@basic/"p[@class*='last']").remove_class("last")
  52. assert_changed(@basic, "p[@class*='last']", all_c1) { |p| p['class'] == 'final' }
  53. end
  54. def test_remove_all_classes
  55. all_c2 = (@basic/"p[@class]").remove_class
  56. assert_changed(@basic, "p[@class]", all_c2) { |p| p['class'].nil? }
  57. end
  58. def test_xml_casing
  59. doc = Hpricot.XML("<root><wildCat>text</wildCat></root>")
  60. (doc/:root/:wildCat).after("<beanPole>gravity</beanPole>")
  61. assert_equal doc.to_s, "<root><wildCat>text</wildCat><beanPole>gravity</beanPole></root>"
  62. frag = Hpricot.XML do
  63. b { i "A bit of HTML" }
  64. end
  65. (frag/:b).after("<beanPole>gravity</beanPole>")
  66. assert_equal frag.to_s, "<b><i>A bit of HTML</i></b><beanPole>gravity</beanPole>"
  67. end
  68. def test_reparent_empty_nodes
  69. doc = Hpricot("<div/>")
  70. doc.root.inner_html = "foo"
  71. assert_equal doc.root.inner_html, "foo"
  72. doc.root.inner_html = ""
  73. assert_equal doc.root.inner_html, ""
  74. doc.root.swap { b "test" }
  75. assert_equal doc.root.inner_html, "test"
  76. end
  77. def assert_changed original, selector, set, &block
  78. assert set.all?(&block)
  79. assert Hpricot(original.to_html).search(selector).all?(&block)
  80. end
  81. end