require 'hpricot_matchers'
require 'spec'

describe "test xpath matcher" do
  before(:each) do
    @xml= <<-EOFXML
    <?xml version='1.0'?>
      <claims>
        <chatterbox>
          <day>
            <rank order='1' value='0' userid='26' alias='user25'/>
            <rank order='2' value='0' userid='93' alias='user92'/>
            <rank order='3' value='0' userid='55' alias='user54'/>
            <sometext id='123'>this is text</sometext>
            <sometext id='234'>this is some other text</sometext>
          </day>
        </chatterbox>
      </claims>
    EOFXML

    @doc= Hpricot.XML(@xml)

  end

  it "should test have_xpath" do
    @xml.should have_xpath("/claims/chatterbox/day/rank[@order='1']")
    @xml.should_not have_xpath("/claims/chatterbox/day/rank[@order='10']")
    @doc.should have_xpath("/claims/chatterbox/day/rank[@order='1']")
    @doc.should_not have_xpath("/claims/chatterbox/day/rank[@order='10']")
    @doc.should have_xpath("/claims/chatterbox/day/rank[1][@order='1']")
    @doc.should have_xpath("/claims/chatterbox/day/rank[1][@userid='26']")
    @doc.should have_xpath("/claims/chatterbox/day/rank[2][@value='0']")
    @doc.should have_xpath("/claims/chatterbox/day/rank[3][@alias='user54']")
    @doc.should have_xpath("/claims/chatterbox/day/rank[@order='1'][@userid='26']")
  end

  it "should test have_xpath with count" do
    @doc.should have_xpath("/claims", :count => 1)
    @doc.should have_xpath("/xxclaims", :count => 0)
    @doc.should have_xpath("/claims/chatterbox/day/rank", :count => 3)
    @doc.should have_xpath("/claims/chatterbox/day/sometext[@id='123']", :count => 1)
    @doc.should have_xpath("/claims/chatterbox/day/sometext[@id='234']", :count => 1)
  end

  it "should test have_xpath with content" do
    @doc.should have_xpath("/claims/chatterbox/day/sometext[1]", "this is text")
    @doc.should have_xpath("/claims/chatterbox/day/sometext[2]", "this is some other text")
    @doc.should have_xpath("/claims/chatterbox/day/sometext[@id='123']", "this is text")
    @doc.should have_xpath("/claims/chatterbox/day/sometext[@id='234']", "this is some other text")
  end

end