# -*- coding: utf-8 -*- #!/usr/bin/env ruby require 'test/unit' require 'hpricot' require 'load_files' class TestParser < Test::Unit::TestCase def test_set_attr @basic = Hpricot.parse(TestFiles::BASIC) @basic.search('//p').set('class', 'para') assert_equal 4, @basic.search('//p').length assert_equal 4, @basic.search('//p').find_all { |x| x['class'] == 'para' }.length end # Test creating a new element def test_new_element elem = Hpricot::Elem.new('form') assert_not_nil(elem) assert_not_nil(elem.attributes) end def test_scan_text assert_equal 'FOO', Hpricot.make("FOO").children.first.content end def test_filter_by_attr @boingboing = Hpricot.parse(TestFiles::BOINGBOING) # this link is escaped in the doc link = 'http://www.youtube.com/watch?v=TvSNXyNw26g&search=chris%20ware' assert_equal link, @boingboing.at("a[@href='#{link}']")['href'] end def test_filter_contains @basic = Hpricot.parse(TestFiles::BASIC) assert_equal '
First
Second
one
two
one
", h.search("//div/p:eq(0)").to_s assert_equal "one
", h.search("//div/p:first").to_s assert_equal "one
", h.search("//div/p:first()").to_s end def test_pace doc = Hpricot(TestFiles::PACE_APPLICATION) assert_equal 'get', doc.at('form[@name=frmSect11]')['method'] # assert_equal '2', doc.at('#hdnSpouse')['value'] end def test_scan_boingboing @boingboing = Hpricot.parse(TestFiles::BOINGBOING) assert_equal 60, (@boingboing/'p.posted').length assert_equal 1, @boingboing.search("//a[@name='027906']").length assert_equal 10, @boingboing.search("script comment()").length assert_equal 3, @boingboing.search("a[text()*='Boing']").length assert_equal 1, @boingboing.search("h3[text()='College kids reportedly taking more smart drugs']").length assert_equal 0, @boingboing.search("h3[text()='College']").length assert_equal 60, @boingboing.search("h3").length assert_equal 59, @boingboing.search("h3[text()!='College kids reportedly taking more smart drugs']").length assert_equal 17, @boingboing.search("h3[text()$='s']").length assert_equal 116, @boingboing.search("p[text()]").length assert_equal 211, @boingboing.search("p").length end def test_reparent doc = Hpricot(%{}) div1 = doc.search('#blurb_1') div1.before('') div0 = doc.search('#blurb_0') div0.before('') assert_equal 'div', doc.at('#blurb_1').name end def test_siblings @basic = Hpricot.parse(TestFiles::BASIC) t = @basic.at(:title) e = t.next_sibling assert_equal 'test1.css', e['href'] assert_equal 'title', e.previous_sibling.name end def test_css_negation @basic = Hpricot.parse(TestFiles::BASIC) assert_equal 3, (@basic/'p:not(.final)').length end def test_remove_attribute @basic = Hpricot.parse(TestFiles::BASIC) (@basic/:p).each { |ele| ele.remove_attribute('class') } assert_equal 0, (@basic/'p[@class]').length end def test_abs_xpath @boingboing = Hpricot.parse(TestFiles::BOINGBOING) assert_equal 60, @boingboing.search("/html/body//p[@class='posted']").length assert_equal 60, @boingboing.search("/*/body//p[@class='posted']").length assert_equal 18, @boingboing.search("//script").length divs = @boingboing.search("//script/../div") assert_equal 2, divs.length imgs = @boingboing.search('//div/p/a/img') assert_equal 16, imgs.length assert_equal 16, @boingboing.search('//div').search('p/a/img').length assert imgs.all? { |x| x.name == 'img' } end def test_predicates @boingboing = Hpricot.parse(TestFiles::BOINGBOING) assert_equal 2, @boingboing.search('//link[@rel="alternate"]').length p_imgs = @boingboing.search('//div/p[/a/img]') assert_equal 16, p_imgs.length assert p_imgs.all? { |x| x.name == 'p' } p_imgs = @boingboing.search('//div/p[a/img]') assert_equal 16, p_imgs.length assert p_imgs.all? { |x| x.name == 'p' } assert_equal 1, @boingboing.search('//input[@checked]').length end def test_tag_case @tenderlove = Hpricot.parse(TestFiles::TENDERLOVE) assert_equal 2, @tenderlove.search('//a').length assert_equal 3, @tenderlove.search('//area').length assert_equal 2, @tenderlove.search('//meta').length end def test_alt_predicates @boingboing = Hpricot.parse(TestFiles::BOINGBOING) assert_equal 1, @boingboing.search('//table/tr:last').length @basic = Hpricot.parse(TestFiles::BASIC) assert_equal "The third paragraph
", @basic.search('p:eq(2)').to_html assert_equal 'THE FINAL PARAGRAPH
', @basic.search('p:last').to_html assert_equal 'last final', @basic.search('//p:last-of-type').first.get_attribute('class') end def test_insert_after # ticket #63 doc = Hpricot('') (doc/'div').each do |element| element.after('Paragraph 1
Paragraph 2
') end assert_equal doc.to_html, 'Paragraph 1
Paragraph 2
' end def test_insert_before # ticket #61 doc = Hpricot('') (doc/'div').each do |element| element.before('Paragraph 1
Paragraph 2
') end assert_equal doc.to_html, 'Paragraph 1
Paragraph 2
' end def test_many_paths @boingboing = Hpricot.parse(TestFiles::BOINGBOING) assert_equal 62, @boingboing.search('p.posted, link[@rel="alternate"]').length assert_equal 18, @boingboing.search('//div/p[a/img]|//link[@rel="alternate"]').length end def test_stacked_search @boingboing = Hpricot.parse(TestFiles::BOINGBOING) assert_kind_of Hpricot::Elements, @boingboing.search('//div/p').search('a img') end def test_attr_casing doc = Hpricot("A simple test string.") assert_equal (doc % :a)[:href], "a" assert_equal (doc % :a)[:HREF], nil assert_equal (doc % :a)['href'], "a" assert_equal (doc % :a)['HREF'], nil end def test_class_search # test case sent by Chih-Chao Lam doc = Hpricot("