Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add specs for Nokogiri core_ext

  • Loading branch information...
commit 240a61cfbe6b30332173ccf667f57bebf2b777d9 1 parent b808d47
@benlangfeld authored
Showing with 83 additions and 0 deletions.
  1. +83 −0 spec/niceogiri/core_ext/nokogiri_spec.rb
View
83 spec/niceogiri/core_ext/nokogiri_spec.rb
@@ -0,0 +1,83 @@
+require 'spec_helper'
+
+describe 'Nokogiri::XML::Node' do
+ before { @doc = Nokogiri::XML::Document.new }
+
+ it 'aliases #name to #element_name' do
+ node = Nokogiri::XML::Node.new 'foo', @doc
+ node.must_respond_to :element_name
+ node.element_name.must_equal node.name
+ end
+
+ it 'aliases #name= to #element_name=' do
+ node = Nokogiri::XML::Node.new 'foo', @doc
+ node.must_respond_to :element_name=
+ node.element_name.must_equal node.name
+ node.element_name = 'bar'
+ node.element_name.must_equal 'bar'
+ end
+
+ it 'allows symbols as hash keys for attributes' do
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
+ attrs['foo'] = 'bar'
+
+ attrs['foo'].must_equal 'bar'
+ attrs[:foo].must_equal 'bar'
+ end
+
+ it 'ensures a string is passed to the attribute setter' do
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
+ attrs[:foo] = 1
+ attrs[:foo].must_equal '1'
+
+ attrs[:some_attr] = [:bah, :boo]
+ attrs[:some_attr].must_equal 'bahboo'
+ end
+
+ it 'removes an attribute when set to nil' do
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
+ attrs['foo'] = 'bar'
+
+ attrs['foo'].must_equal 'bar'
+ attrs['foo'] = nil
+ attrs['foo'].must_be_nil
+ end
+
+ it 'allows attribute values to change' do
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
+ attrs['foo'] = 'bar'
+
+ attrs['foo'].must_equal 'bar'
+ attrs['foo'] = 'baz'
+ attrs['foo'].must_equal 'baz'
+ end
+
+ it 'allows symbols as the path in #xpath' do
+ node = Nokogiri::XML::Node.new('foo', @doc)
+ node.must_respond_to :find
+ @doc.root = node
+ @doc.xpath(:foo).first.wont_be_nil
+ @doc.xpath(:foo).first.must_equal @doc.xpath('/foo').first
+ end
+
+ it 'allows symbols as namespace names in #xpath' do
+ node = Nokogiri::XML::Node.new('foo', @doc)
+ node.namespace = node.add_namespace('bar', 'baz')
+ @doc.root = node
+ node.xpath('/bar:foo', :bar => 'baz').first.wont_be_nil
+ end
+
+ it 'aliases #xpath to #find' do
+ node = Nokogiri::XML::Node.new('foo', @doc)
+ node.must_respond_to :find
+ @doc.root = node
+ node.find('/foo').first.wont_be_nil
+ end
+
+ it 'has a helper function #find_first' do
+ node = Nokogiri::XML::Node.new('foo', @doc)
+ node.must_respond_to :find
+ @doc.root = node
+ node.find_first('/foo').must_equal node.find('/foo').first
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.