Extra type validation for XML::Element options.
This commit is contained in:
parent
b1388ff84a
commit
f002061aaa
|
@ -17,7 +17,6 @@ end
|
|||
#:nocov:
|
||||
|
||||
require_relative 'oga/xml/node'
|
||||
require_relative 'oga/xml/element'
|
||||
require_relative 'oga/xml/document'
|
||||
require_relative 'oga/xml/text'
|
||||
require_relative 'oga/xml/comment'
|
||||
|
@ -26,6 +25,7 @@ require_relative 'oga/xml/xml_declaration'
|
|||
require_relative 'oga/xml/doctype'
|
||||
require_relative 'oga/xml/attribute'
|
||||
require_relative 'oga/xml/namespace'
|
||||
require_relative 'oga/xml/element'
|
||||
require_relative 'oga/xml/node_set'
|
||||
|
||||
require_relative 'oga/html/parser'
|
||||
|
|
|
@ -19,6 +19,17 @@ module Oga
|
|||
class Element < Node
|
||||
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
|
||||
#
|
||||
|
@ -31,12 +42,7 @@ module Oga
|
|||
# of the element as an Array.
|
||||
#
|
||||
def initialize(options = {})
|
||||
if options[:namespace] and !options[:namespace].is_a?(Namespace)
|
||||
raise(
|
||||
TypeError,
|
||||
':namespace must be an instance of Oga::XML::Namespace'
|
||||
)
|
||||
end
|
||||
validate_option_types!(options)
|
||||
|
||||
super
|
||||
|
||||
|
@ -141,6 +147,21 @@ module Oga
|
|||
|
||||
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
|
||||
# @return [Array]
|
||||
|
|
|
@ -6,12 +6,18 @@ describe Oga::XML::Element do
|
|||
described_class.new(:name => 'p').name.should == 'p'
|
||||
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.should raise_error(TypeError)
|
||||
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
|
||||
instance = described_class.new
|
||||
instance.name = 'p'
|
||||
|
|
Loading…
Reference in New Issue