Skip to content

Commit

Permalink
merge locatables and attributes hashes in Element#initialize signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Feb 14, 2010
1 parent 4a2ff20 commit 2ac792b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
11 changes: 11 additions & 0 deletions lib/core_ext/hash/except.rb
@@ -0,0 +1,11 @@
class Hash
def except!(*keys)
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
keys.each { |key| delete(key) }
self
end

def except(*keys)
dup.except!(*keys)
end
end unless Hash.method_defined?(:slice)
14 changes: 14 additions & 0 deletions lib/core_ext/hash/slice.rb
@@ -0,0 +1,14 @@
class Hash
def slice!(*keys)
omit = slice(*self.keys - keys)
hash = slice(*keys)
replace(hash)
omit
end

def slice(*keys)
hash = self.class.new
keys.each { |k| hash[k] = self[k] if has_key?(k) }
hash
end
end unless Hash.method_defined?(:slice)
9 changes: 6 additions & 3 deletions lib/locator/element.rb
@@ -1,3 +1,6 @@
require 'core_ext/hash/except'
require 'core_ext/hash/slice'

module Locator
class Element
autoload :Area, 'locator/element/area'
Expand All @@ -20,10 +23,10 @@ class Element

attr_reader :name, :css, :locatables, :attributes

def initialize(name = nil, locatables = nil, attributes = nil)
def initialize(name = nil, options = {})
@name = name
@locatables = { :equals => :id, :matches => :content }.merge(locatables || {})
@attributes = attributes || {}
@attributes = options.slice!(:equals, :matches)
@locatables = { :equals => :id, :matches => :content }.merge(options || {})
end

def locate(*args)
Expand Down
2 changes: 1 addition & 1 deletion lib/locator/element/button.rb
Expand Up @@ -2,7 +2,7 @@ module Locator
class Element
class Button < ElementsList
def initialize
input = Element.new(:input, { :equals => [:id, :name], :matches => [:value] }, :type => %w(submit button image))
input = Element.new(:input, :equals => [:id, :name], :matches => [:value], :type => %w(submit button image))
button = Element.new(:button, :equals => [:id, :name], :matches => [:content])
super(input, button)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/locator/element/input.rb
Expand Up @@ -2,9 +2,7 @@ module Locator
class Element
class Input < FormElement
def initialize(attributes = {})
attributes = { :type => [:text, :password , :email, :url, :search, :tel, :color] }.merge(attributes)
matchables = { :equals => [:id, :name] }
super(:input, { :equals => [:id, :name] }, attributes)
super(:input, :equals => [:id, :name], :type => [:text, :password , :email, :url, :search, :tel, :color])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/locator/element/link.rb
Expand Up @@ -2,7 +2,7 @@ module Locator
class Element
class Link < Element
def initialize
super(:a, nil, :href => true)
super(:a, :href => true)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/locator/element_test.rb
Expand Up @@ -15,12 +15,12 @@ class LocatorElementTest < Test::Unit::TestCase
end

test "xpath with attributes" do
xpath = Element.new(nil, nil, :type => 'type', :class => 'class').xpath
xpath = Element.new(nil, :type => 'type', :class => 'class').xpath
assert_equal ".//*[@type=\"type\"][contains(concat(' ', @class, ' '), concat(' ', \"class\", ' '))]", xpath
end

test "xpath with node name and attributes" do
xpath = Element.new(:div, nil, :type => 'type', :class => 'class').xpath
xpath = Element.new(:div, :type => 'type', :class => 'class').xpath
assert_equal ".//div[@type=\"type\"][contains(concat(' ', @class, ' '), concat(' ', \"class\", ' '))]", xpath
end

Expand All @@ -40,7 +40,7 @@ class LocatorElementTest < Test::Unit::TestCase

test "all selects all nodes with attribute given to initialize" do
html = '<a class="foo"></a><p class="bar"></p>'
elements = Element.new(nil, nil, :class => 'foo').all(html)
elements = Element.new(nil, :class => 'foo').all(html)
assert_equal %w(a), elements.map { |element| element.tag_name }
end

Expand Down

0 comments on commit 2ac792b

Please sign in to comment.