Class: RSpecHtmlMatchers::HaveTag

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-html-matchers/have_tag.rb

Constant Summary collapse

DESCRIPTIONS =

rubocop:disable Metrics/ClassLength

{
  :have_at_least_1 => %(have at least 1 element matching "%s"),
  :have_n          => %(have %i element(s) matching "%s"),
}.freeze
MESSAGES =
{
  :expected_tag         => %(expected following:\n%s\nto #{DESCRIPTIONS[:have_at_least_1]}, found 0.),
  :unexpected_tag       => %(expected following:\n%s\nto NOT have element matching "%s", found %s.),

  :expected_count       => %(expected following:\n%s\nto #{DESCRIPTIONS[:have_n]}, found %s.),
  :unexpected_count     => %(expected following:\n%s\nto NOT have %i element(s) matching "%s", but found.),

  :expected_btw_count   => %(expected following:\n%s\nto have at least %i and at most %i element(s) matching "%s", found %i.),
  :unexpected_btw_count => %(expected following:\n%s\nto NOT have at least %i and at most %i element(s) matching "%s", but found %i.),

  :expected_at_most     => %(expected following:\n%s\nto have at most %i element(s) matching "%s", found %i.),
  :unexpected_at_most   => %(expected following:\n%s\nto NOT have at most %i element(s) matching "%s", but found %i.),

  :expected_at_least    => %(expected following:\n%s\nto have at least %i element(s) matching "%s", found %i.),
  :unexpected_at_least  => %(expected following:\n%s\nto NOT have at least %i element(s) matching "%s", but found %i.),

  :expected_blank       => %(expected following template to contain empty tag %s:\n%s),
  :unexpected_blank     => %(expected following template to contain tag %s with other tags:\n%s),

  :expected_regexp      => %(%s regexp expected within "%s" in following template:\n%s),
  :unexpected_regexp    => %(%s regexp unexpected within "%s" in following template:\n%s\nbut was found.),

  :expected_text        => %("%s" expected within "%s" in following template:\n%s),
  :unexpected_text      => %("%s" unexpected within "%s" in following template:\n%s\nbut was found.),

  :wrong_count_error    => %(:count with :minimum or :maximum has no sence!),
  :min_max_error        => %(:minimum should be less than :maximum!),
  :bad_range_error      => %(Your :count range(%s) has no sence!),
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, options = {}, &block) ⇒ HaveTag

Returns a new instance of HaveTag.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rspec-html-matchers/have_tag.rb', line 45

def initialize tag, options = {}, &block
  @tag = tag.to_s
  @options = options
  @block = block

  if with_attrs = @options.delete(:with)
    if classes = with_attrs.delete(:class)
      @tag += '.' + classes_to_selector(classes)
    end
    selector = with_attrs.inject('') do |html_attrs_string, (k, v)|
      html_attrs_string += "[#{k}='#{v}']"
      html_attrs_string
    end
    @tag += selector
  end

  if without_attrs = @options.delete(:without)
    if classes = without_attrs.delete(:class)
      @tag += ":not(.#{classes_to_selector(classes)})"
    end
  end

  validate_options!
  organize_options!
end

Instance Attribute Details

#current_scopeObject (readonly)



73
74
75
# File 'lib/rspec-html-matchers/have_tag.rb', line 73

def current_scope
  @current_scope
end

#failure_messageObject (readonly)



71
72
73
# File 'lib/rspec-html-matchers/have_tag.rb', line 71

def failure_message
  @failure_message
end

#failure_message_when_negatedObject (readonly)



72
73
74
# File 'lib/rspec-html-matchers/have_tag.rb', line 72

def failure_message_when_negated
  @failure_message_when_negated
end

Instance Method Details

#descriptionObject



104
105
106
107
108
109
110
111
# File 'lib/rspec-html-matchers/have_tag.rb', line 104

def description
  # TODO: should it be more complicated?
  if options.key?(:count)
    format(DESCRIPTIONS[:have_n], options[:count], tag)
  else
    DESCRIPTIONS[:have_at_least_1] % tag
  end
end

#matches?(src, &block) ⇒ Boolean



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rspec-html-matchers/have_tag.rb', line 75

def matches? src, &block
  @block = block if block

  src = src.html if defined?(Capybara::Session) && src.is_a?(Capybara::Session)

  case src
  when String
    parent_scope = Nokogiri::HTML(src)
    @document    = src
  else
    parent_scope  = src.current_scope
    @document     = parent_scope.to_html
  end

  @current_scope = begin
                     parent_scope.css(tag)
                     # on jruby this produce exception if css was not found:
                     # undefined method `decorate' for nil:NilClass
                   rescue NoMethodError
                     Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
                   end
  if tag_presents? && proper_content? && count_right?
    @block.call(self) if @block
    true
  else
    false
  end
end