Extra type validation for XML::Element options.
This commit is contained in:
parent
b1388ff84a
commit
f002061aaa
|
@ -17,7 +17,6 @@ end
|
||||||
#:nocov:
|
#:nocov:
|
||||||
|
|
||||||
require_relative 'oga/xml/node'
|
require_relative 'oga/xml/node'
|
||||||
require_relative 'oga/xml/element'
|
|
||||||
require_relative 'oga/xml/document'
|
require_relative 'oga/xml/document'
|
||||||
require_relative 'oga/xml/text'
|
require_relative 'oga/xml/text'
|
||||||
require_relative 'oga/xml/comment'
|
require_relative 'oga/xml/comment'
|
||||||
|
@ -26,6 +25,7 @@ require_relative 'oga/xml/xml_declaration'
|
||||||
require_relative 'oga/xml/doctype'
|
require_relative 'oga/xml/doctype'
|
||||||
require_relative 'oga/xml/attribute'
|
require_relative 'oga/xml/attribute'
|
||||||
require_relative 'oga/xml/namespace'
|
require_relative 'oga/xml/namespace'
|
||||||
|
require_relative 'oga/xml/element'
|
||||||
require_relative 'oga/xml/node_set'
|
require_relative 'oga/xml/node_set'
|
||||||
|
|
||||||
require_relative 'oga/html/parser'
|
require_relative 'oga/html/parser'
|
||||||
|
|
|
@ -19,6 +19,17 @@ module Oga
|
||||||
class Element < Node
|
class Element < Node
|
||||||
attr_accessor :name, :namespace, :attributes
|
attr_accessor :name, :namespace, :attributes
|
||||||
|
|
||||||
|
##
|
||||||
|
# List of options that can be passed to the constructor and the required
|
||||||
|
# types of their values.
|
||||||
|
#
|
||||||
|
# @return [Hash]
|
||||||
|
#
|
||||||
|
OPTION_TYPES = {
|
||||||
|
:namespace => Namespace,
|
||||||
|
:attributes => Array
|
||||||
|
}
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
#
|
#
|
||||||
|
@ -31,12 +42,7 @@ module Oga
|
||||||
# of the element as an Array.
|
# of the element as an Array.
|
||||||
#
|
#
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
if options[:namespace] and !options[:namespace].is_a?(Namespace)
|
validate_option_types!(options)
|
||||||
raise(
|
|
||||||
TypeError,
|
|
||||||
':namespace must be an instance of Oga::XML::Namespace'
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
super
|
super
|
||||||
|
|
||||||
|
@ -141,6 +147,21 @@ module Oga
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
##
|
||||||
|
# @param [Hash] options
|
||||||
|
# @raise [TypeError]
|
||||||
|
#
|
||||||
|
def validate_option_types!(options)
|
||||||
|
OPTION_TYPES.each do |key, type|
|
||||||
|
if options[key] and !options[key].is_a?(type)
|
||||||
|
raise(
|
||||||
|
TypeError,
|
||||||
|
"#{key.inspect} must be an instance of #{type}"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [String] name
|
# @param [String] name
|
||||||
# @return [Array]
|
# @return [Array]
|
||||||
|
|
|
@ -6,12 +6,18 @@ describe Oga::XML::Element do
|
||||||
described_class.new(:name => 'p').name.should == 'p'
|
described_class.new(:name => 'p').name.should == 'p'
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'raise TypeError when the namespace is a String' do
|
example 'raise TypeError when the namespace is not a Namespace' do
|
||||||
block = lambda { described_class.new(:namespace => 'x') }
|
block = lambda { described_class.new(:namespace => 'x') }
|
||||||
|
|
||||||
block.should raise_error(TypeError)
|
block.should raise_error(TypeError)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
example 'raise TypeError when the attributes are not an Array' do
|
||||||
|
block = lambda { described_class.new(:attributes => 'foo') }
|
||||||
|
|
||||||
|
block.should raise_error(TypeError)
|
||||||
|
end
|
||||||
|
|
||||||
example 'set the name via a setter' do
|
example 'set the name via a setter' do
|
||||||
instance = described_class.new
|
instance = described_class.new
|
||||||
instance.name = 'p'
|
instance.name = 'p'
|
||||||
|
|
Loading…
Reference in New Issue