module RSpec::Matchers

def form_tag_options form_tag_type, form_tag_name, form_tag_value=nil

form_tag in method name name mean smth. like input, submit, tags that should appear in a form
def form_tag_options form_tag_type, form_tag_name, form_tag_value=nil
  options = { :with => { :name => form_tag_name, :type => form_tag_type } }
  # .to_s if value is a digit or smth. else, see issue#10
  options[:with].merge!(:value => form_tag_value.to_s) if form_tag_value
  return options
end

def have_form action_url, method, options={}, &block

Other tags:
    See: #have_tag -

Other tags:
    Yield: - block with with_, see below
def have_form action_url, method, options={}, &block
  options[:with] ||= {}
  id = options[:with].delete(:id)
  tag = 'form'; tag << '#'+id if id
  options[:with].merge!(:action => action_url)
  options[:with].merge!(:method => method.to_s)
  have_tag tag, options, &block
end

def have_tag tag, options={}, &block

Options Hash: (**options)
  • :text (String/Regexp) -- to match tag content, could be either String or Regexp
  • :max (Fixnum) -- same as :maximum
  • :maximum (Fixnum) -- maximum count of elements to match
  • :min (Fixnum) -- same as :minimum
  • :minimum (Fixnum) -- minimum count of elements to match
  • :count (Range) -- not strict tag count matching, count of tags should be in specified range
  • :count (Fixnum) -- for tag count matching(*ATTENTION:* do not use :count with :minimum(:min) or :maximum(:max))
  • :with (Hash) -- hash with html attributes, within this, *:class* option have special meaning, you may specify it as array of expected classes or string of classes separated by spaces, order does not matter

Parameters:
  • options (Hash) -- options hash(see below)
  • tag (String) -- css selector for tag you want to match, e.g. 'div', 'section#my', 'article.red'

Other tags:
    Yield: - block where you should put with_tag, without_tag and/or other matchers
def have_tag tag, options={}, &block
  # for backwards compatibility with rpecs have tag:
  options = { :text => options } if options.kind_of? String
  @__current_scope_for_nokogiri_matcher = NokogiriMatcher.new(tag, options, &block)
end

def should_have_input(options)

def should_have_input(options)
  @__current_scope_for_nokogiri_matcher.should have_tag('input', options)
end

def should_not_have_input(options)

def should_not_have_input(options)
  @__current_scope_for_nokogiri_matcher.should_not have_tag('input', options)
end

def with_button text, value=nil, options={}

def with_button text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  options[:with].merge!(:value => value.to_s) if value
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should have_tag('button', options)
end

def with_checkbox name, value=nil

def with_checkbox name, value=nil
  options = form_tag_options('checkbox',name,value)
  should_have_input(options)
end

def with_date_field date_field_type, name=nil, options={}

def with_date_field date_field_type, name=nil, options={}
  date_field_type = date_field_type.to_s
  raise "unknown type `#{date_field_type}` for date picker" unless DATE_FIELD_TYPES.include?(date_field_type)
  options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with)||{}) }
  options[:with].merge!(:name => name.to_s) if name
  should_have_input(options)
end

def with_email_field name, value=nil

def with_email_field name, value=nil
  options = form_tag_options('email',name,value)
  should_have_input(options)
end

def with_file_field name, value=nil

def with_file_field name, value=nil
  options = form_tag_options('file',name,value)
  should_have_input(options)
end

def with_hidden_field name, value=nil

def with_hidden_field name, value=nil
  options = form_tag_options('hidden',name,value)
  should_have_input(options)
end

def with_number_field name, value=nil

def with_number_field name, value=nil
  options = form_tag_options('number',name,value)
  should_have_input(options)
end

def with_option text, value=nil, options={}

def with_option text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  tag='option'
  options[:with].merge!(:value => value.to_s) if value
  if options[:selected]
    options[:with].merge!(:selected => "selected")
  end
  options.delete(:selected)
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, options)
end

def with_password_field name, value=nil

TODO add ability to explicitly say that value should be empty
def with_password_field name, value=nil
  options = form_tag_options('password',name,value)
  should_have_input(options)
end

def with_radio_button name, value

def with_radio_button name, value
  options = form_tag_options('radio',name,value)
  should_have_input(options)
end

def with_range_field name, min, max, options={}

def with_range_field name, min, max, options={}
  options = { :with => { :name => name, :type => 'range', :min => min.to_s, :max => max.to_s }.merge(options.delete(:with)||{}) }
  should_have_input(options)
end

def with_select name, options={}, &block

def with_select name, options={}, &block
  options[:with] ||= {}
  id = options[:with].delete(:id)
  tag='select'; tag << '#'+id if id
  options[:with].merge!(:name => name)
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, options, &block)
end

def with_submit value

def with_submit value
  options = { :with => { :type => 'submit', :value => value } }
  #options = form_tag_options('text',name,value)
  should_have_input(options)
end

def with_tag tag, options={}, &block

Other tags:
    Note: - this should be used within block of have_tag matcher

Other tags:
    See: #have_tag -

Other tags:
    Yield: - block where you should put other with_tag or without_tag
def with_tag tag, options={}, &block
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, options, &block)
end

def with_text text

def with_text text
  raise StandardError, 'this matcher should be used inside "have_tag" matcher block' unless defined?(@__current_scope_for_nokogiri_matcher)
  raise ArgumentError, 'this matcher does not accept block' if block_given?
  tag = @__current_scope_for_nokogiri_matcher.instance_variable_get(:@tag)
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, :text => text)
end

def with_text_area name#TODO, text=nil

TODO, text=nil
def with_text_area name#TODO, text=nil
  #options = form_tag_options('text',name,value)
  options = { :with => { :name => name } }
  @__current_scope_for_nokogiri_matcher.should have_tag('textarea', options)
end

def with_text_field name, value=nil

def with_text_field name, value=nil
  options = form_tag_options('text',name,value)
  should_have_input(options)
end

def with_url_field name, value=nil

def with_url_field name, value=nil
  options = form_tag_options('url',name,value)
  should_have_input(options)
end

def without_button text, value=nil, options={}

def without_button text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  options[:with].merge!(:value => value.to_s) if value
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should_not have_tag('button', options)
end

def without_checkbox name, value=nil

def without_checkbox name, value=nil
  options = form_tag_options('checkbox',name,value)
  should_not_have_input(options)
end

def without_date_field date_field_type, name=nil, options={}

def without_date_field date_field_type, name=nil, options={}
  date_field_type = date_field_type.to_s
  raise "unknown type `#{date_field_type}` for date picker" unless DATE_FIELD_TYPES.include?(date_field_type)
  options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with)||{}) }
  options[:with].merge!(:name => name.to_s) if name
  should_not_have_input(options)
end

def without_email_field name, value=nil

def without_email_field name, value=nil
  options = form_tag_options('email',name,value)
  should_not_have_input(options)
end

def without_file_field name, value=nil

def without_file_field name, value=nil
  options = form_tag_options('file',name,value)
  should_not_have_input(options)
end

def without_hidden_field name, value=nil

def without_hidden_field name, value=nil
  options = form_tag_options('hidden',name,value)
  should_not_have_input(options)
end

def without_number_field name, value=nil

def without_number_field name, value=nil
  options = form_tag_options('number',name,value)
  should_not_have_input(options)
end

def without_option text, value=nil, options={}

def without_option text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  tag='option'
  options[:with].merge!(:value => value.to_s) if value
  if options[:selected]
    options[:with].merge!(:selected => "selected")
  end
  options.delete(:selected)
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, options)
end

def without_password_field name, value=nil

def without_password_field name, value=nil
  options = form_tag_options('password',name,value)
  should_not_have_input(options)
end

def without_radio_button name, value

def without_radio_button name, value
  options = form_tag_options('radio',name,value)
  should_not_have_input(options)
end

def without_range_field name, min=nil, max=nil, options={}

def without_range_field name, min=nil, max=nil, options={}
  options = { :with => { :name => name, :type => 'range' }.merge(options.delete(:with)||{}) }
  options[:with].merge!(:min => min.to_s) if min
  options[:with].merge!(:max => max.to_s) if max
  should_not_have_input(options)
end

def without_select name, options={}, &block

def without_select name, options={}, &block
  options[:with] ||= {}
  id = options[:with].delete(:id)
  tag='select'; tag << '#'+id if id
  options[:with].merge!(:name => name)
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, options, &block)
end

def without_submit value

def without_submit value
  #options = form_tag_options('text',name,value)
  options = { :with => { :type => 'submit', :value => value } }
  should_not_have_input(options)
end

def without_tag tag, options={}, &block

Other tags:
    Note: - this should be used within block of have_tag matcher

Other tags:
    See: #have_tag -

Other tags:
    Yield: - block where you should put other with_tag or without_tag
def without_tag tag, options={}, &block
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, options, &block)
end

def without_text text

def without_text text
  raise StandardError, 'this matcher should be used inside "have_tag" matcher block' unless defined?(@__current_scope_for_nokogiri_matcher)
  raise ArgumentError, 'this matcher does not accept block' if block_given?
  tag = @__current_scope_for_nokogiri_matcher.instance_variable_get(:@tag)
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, :text => text)
end

def without_text_area name#TODO, text=nil

TODO, text=nil
def without_text_area name#TODO, text=nil
  #options = form_tag_options('text',name,value)
  options = { :with => { :name => name } }
  @__current_scope_for_nokogiri_matcher.should_not have_tag('textarea', options)
end

def without_text_field name, value=nil

def without_text_field name, value=nil
  options = form_tag_options('text',name,value)
  should_not_have_input(options)
end

def without_url_field name, value=nil

def without_url_field name, value=nil
  options = form_tag_options('url',name,value)
  should_not_have_input(options)
end